一段AES的簡(jiǎn)化java代碼:
/**
* 對(duì)數(shù)據(jù)用AES算法加密
* @param plainText 要加密的字節(jié)數(shù)組
* @param key 密鑰
* @return 加密后的字節(jié)數(shù)組
*/
private static byte [] encrypt(byte [] plainText, byte [] key) {
try{
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] decryptText = cipher.doFinal(plainText);
return decryptText;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 對(duì)AES算法加密的數(shù)據(jù)解密
* @param cipherText 要解密的字節(jié)數(shù)組
* @param key 密鑰
* @return 解密后的字節(jié)數(shù)組
*/
private static byte [] decrypt(byte [] cipherText, byte [] key) {
try{
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] plainText = cipher.doFinal(cipherText);
return plainText;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。