轉自:
http://www.java265.com/JavaJingYan/202206/16559759223818.html
下文筆者講述java代碼實作的AES加密和解密的示例分享,如下所示
AES加密簡介
AES簡介: Advanced Encryption Standard 是一個高級加密標準,目前已經被廣泛應用 AES可使用128、192、和256位密鑰,并且用128位分組加密和解密資料 由于密碼長度大,所以無法在短時間內破解
AES應用常見
AES目前被廣泛應用于
金融財務、在線交易、無線通信、數字存盤等領域
已經受到了長久的驗證
下文筆者講述AES的示例分享,如下所示:
AES加密工具類 測驗
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AESUtil {
/**
* AES加密字串
*
* @param content
* 需要被加密的字串
* @param password
* 加密需要的密碼
* @return 密文
*/
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");// 創建AES的Key生產者
kgen.init(128, new SecureRandom(password.getBytes()));// 利用用戶密碼作為亂數初始化出
//加密沒關系,SecureRandom是生成安全亂數序列,password.getBytes()是種子,只要種子相同,序列就一樣,所以解密只要有password就行
SecretKey secretKey = kgen.generateKey();// 根據用戶密碼,生成一個密鑰
byte[] enCodeFormat = secretKey.getEncoded();// 回傳基本編碼格式的密鑰,如果此密鑰不支持編碼,則回傳
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 轉換為AES專用密鑰
Cipher cipher = Cipher.getInstance("AES");// 創建密碼器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化為加密模式的密碼器
byte[] result = cipher.doFinal(byteContent);// 加密
return result;
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* 解密AES加密過的字串
*
* @param content
* AES加密過過的內容
* @param password
* 加密時的密碼
* @return 明文
*/
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");// 創建AES的Key生產者
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();// 根據用戶密碼,生成一個密鑰
byte[] enCodeFormat = secretKey.getEncoded();// 回傳基本編碼格式的密鑰
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 轉換為AES專用密鑰
Cipher cipher = Cipher.getInstance("AES");// 創建密碼器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化為解密模式的密碼器
byte[] result = cipher.doFinal(content);
return result; // 明文
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception {
String content = "待加密的內容";
String password = "加密密碼99999";
System.out.println("需要加密的內容:" + content);
byte[] encrypt = encrypt(content, password);
System.out.println("加密后的2進制密文:" + new String(encrypt));
String hexStr = ParseSystemUtil.parseByte2HexStr(encrypt);
System.out.println("加密后的16進制密文:" + hexStr);
byte[] byte2 = ParseSystemUtil.parseHexStr2Byte(hexStr);
System.out.println("加密后的2進制密文:" + new String(byte2));
byte[] decrypt = decrypt(byte2, password);
System.out.println("解密后的內容:" + new String(decrypt,"utf-8"));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/509088.html
標籤:其他
