AES加密 JAVA實現

2021-09-24 20:20:25 字數 3353 閱讀 7649

aes是常用的對稱加密技術,比des有更高的安全性。

在寫cp-abe系統的時間使用aes加密密文檔案,abe加密了乙個element(jpbc庫),屬於常見的加密體制。

下面**的aes,乙個是以檔案流的形式加密檔案,乙個是直接加密字串

public class aes

destfile.createnewfile();

inputstream in = new fileinputstream(sourcefile);

outputstream out = new fileoutputstream(destfile);

// key k = tokey(base64.decode(key));

//byte raw = k.getencoded(); 

keygenerator kgen = keygenerator.getinstance("aes");

kgen.init(128, new securerandom(m.tobytes()));

secretkey secretkey = kgen.generatekey();

byte encodeformat = secretkey.getencoded();

secretkeyspec key1 = new secretkeyspec(encodeformat, "aes");

cipher cipher = cipher.getinstance("aes");// 建立密碼器

//byte bytecontent = content.getbytes("utf-8");

cipher.init(cipher.encrypt_mode, key1);// 初始化

//byte result = cipher.dofinal(bytecontent);

//secretkeyspec secretkeyspec = new secretkeyspec(key,"aes"); 

//cipher cipher = cipher.getinstance("aes"); 

cipher.init(cipher.encrypt_mode, secretkey);

cipherinputstream cin = new cipherinputstream(in, cipher);

byte cache = new byte[cache_size];

int nread = 0;

while ((nread = cin.read(cache)) != -1)

out.close();

cin.close();

in.close();}}

/***

* 解密

* * 

* @param data

* @param key

* @return

* @throws exception

*//**

* * 檔案解密

* * 

* @param key

* @param sourcefilepath

* @param destfilepath

* @throws exception

*/public static void decryptfile(element m, string sourcefilepath, string destfilepath) throws exception

destfile.createnewfile();

fileinputstream in = new fileinputstream(sourcefile);

fileoutputstream out = new fileoutputstream(destfile);

//key k = tokey(base64.decode(key));

// byte raw = k.getencoded(); 

keygenerator kgen = keygenerator.getinstance("aes");

kgen.init(128, new securerandom(m.tobytes()));

secretkey secretkey = kgen.generatekey();

byte encodeformat = secretkey.getencoded();

secretkeyspec key1 = new secretkeyspec(encodeformat, "aes");

cipher cipher = cipher.getinstance("aes");// 建立密碼器

cipher.init(cipher.decrypt_mode, key1);// 初始化

//byte result = cipher.dofinal(content);

//secretkeyspec secretkeyspec = new secretkeyspec(raw, "aes"); 

//cipher cipher = cipher.getinstance("aes"); 

secretkeyspec);

cipheroutputstream cout = new cipheroutputstream(out, cipher);

byte cache = new byte[cache_size];

int nread = 0;

while ((nread = in.read(cache)) != -1)

cout.close();

out.close();

in.close();}}

public static byte encrypt(string content, element m) catch (nosuchalgorithmexception e) catch (nosuchpaddingexception e) catch (invalidkeyexception e) catch (illegalblocksizeexception e) catch (badpaddingexception e)

return null;

}public static byte decrypt(byte content, element m) catch (nosuchalgorithmexception e) catch (nosuchpaddingexception e) catch (invalidkeyexception e) catch (illegalblocksizeexception e) catch (badpaddingexception e)

return null;

}**如上,看到一些部落格說aes加密時間和解密時間相比應該是差不多的,但是博主測試aes加密時間為300ms作用,解密時間卻只有20ms,感覺可能是jar包的問題。

AES對稱加密解密JAVA實現

話不多說,直接上 public class aesutil catch exception e 如果有錯就返加nulll return null 解密 解密過程 1.同加密1 4步 2.將加密後的字串反紡成byte陣列 3.將加密內容解密 public static string decode st...

java實現對稱加密AES和DES的加密 解密

目前主流的加密方式有 1 對稱加密 aes des 2 非對稱加密 rsa dsa。cipher cipher cipher.getinstance des cbc pkcs5padding cipher.init cipher.encrypt mode,key,zeroiv 其中,des是採用的演...

python encrypt 實現AES加密

aes加密方式有五種 ecb,cbc,ctr,cfb,ofb 從安全性角度推薦cbc演算法 windows 下安裝 pip install pycryptodome linux 下安裝 pip install pycrypto cbc加密需要乙個十六位的key 和乙個十六位的iv 偏移量 ecb加密...