AES 對稱加解密

2022-06-08 15:48:11 字數 1593 閱讀 7362

1.生成aes key

/**

* aes根據密碼生成key

* @param

password

* @return

*/public

static

key createkey(string password)

catch

(exception e)

return

null

; }

2.aes加密(ecb模式)

/**

* aes加密操作,使用ecb模式

* @param

contents

* @param

password

* @return

*/public

static string encryptaes(string contents, string password) throws

exception

3.aes解密

/**

* aes解密操作

* @param

encryptcode

* @param

password

* @return

*/public

static string decryptaes(string encryptcode, string password) throws

exception

工具方法

/**

* 將byte陣列轉換成16進製制字元。乙個byte生成兩個字元,長度對應1:2

* @param

bytes,輸入byte陣列

* @return

16進製制字元

*/public

static string byte2hex(byte

bytes)

stringbuilder builder = new

stringbuilder();

//遍歷byte陣列,將每個byte數字轉換成16進製制字元,再拼接起來成字串

for (int i = 0; i < bytes.length; i++)

return

builder.tostring();

}/*** 將16進製制字元轉換成byte陣列。與byte2hex功能相反。

* @param

string 16進製制字串

* @return

byte陣列

*/public

static

byte

hex2byte(string string)

//因為乙個byte生成兩個字元,長度對應1:2,所以byte陣列長度是字串長度一半

byte bytes = new

byte[string.length() / 2];

//遍歷byte陣列,遍歷次數是字串長度一半

for (int i = 0; i < string.length() / 2; i++)

return

bytes;

}

AES對稱加解密

python實現aes對稱加解密 關於aes對稱加密的概念網上很多,在此不在贅述,直接上 import base64 from crypto.cipher import aes aes加密解密工具類 資料塊128位 key 為16位 iv 為16位 aes加密模式為cbc 填充 pkcs7paddi...

AES加解密(對稱)

aes是一種對稱的加密和解密的演算法,其用法如下 package com.zys.demo.util import lombok.extern.slf4j.slf4j import j ax.crypto.cipher import j ax.crypto.spec.ivparameterspec ...

Golang中,Aes加解密

今天在用golang解析php那邊用aes加密的乙個key。網上大多是用base64將結果編碼一下。而且用到了向量。我php 那邊沒有用到向量。所以golang這邊也是要去掉的。參考 的改了下。能夠和php通用。另外,需要注意的是加密的key只能是16,24,32.分別對應的是aes 128,aes...