AES - JAVA

/*
* Jdk 如需要实现 256 位密钥,则需要获取无政策限制权限文件
*/
public class AesEncryptionUtilsByJdk {

private static final SecureRandom RANDOM = new SecureRandom();

private static final String KEY_ALGORITHM = "AES";

private static final String CIPHER_ALGORITHM = "AES/OFB/PKCS5Padding";

private static final int DEFAULT_KEY_LENGTH = 256;

// IV 初始化向量 ,需要随机哈,要与加密共保存。
private static final String IV = "2685aed1945ab91a40d309c7dad00228";

private static final String DEFAULT_KEY = "91c6af915daa99a8ba7eba3a54d489104723e14149ab6c69660da22a0a0ebee3";

/*
* 加密
*
* @param data 待加密字串
* @return 解密后字串
*/
public static String encrypt (String data, String keyHexStr) {
try {
Key key = new SecretKeySpec(hexToByte(keyHexStr), KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(hexToByte(IV)));
byte[] encryptBytes = cipher.doFinal(data.getBytes());
return byteToHex(encryptBytes);
}catch (Exception e){
e.printStackTrace();
return null;
}
}

public static String encrypt (String data) {
return encrypt(data, DEFAULT_KEY);
}

/*
* 解密
*
* @param data 待解密字串
* @return 解密后字串
*/
public static String decrypt (String data, String keyHexStr) {
try {
Key key = new SecretKeySpec(hexToByte(keyHexStr), KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(hexToByte(IV)));
return new String(cipher.doFinal(hexToByte(data)));
}catch (Exception e){
e.printStackTrace();
return null;
}
}

public static String decrypt (String data) {
return decrypt(data, DEFAULT_KEY);
}

/*
* 生成密钥(亦可以在配置文件中配置固定密钥)
*
* @param keyLength 待生成密钥长度
* @return 密钥16进制字串
*/
public static String generateKey (int keyLength) {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
keyGenerator.init(keyLength, RANDOM);
SecretKey secretKey = keyGenerator.generateKey();
return byteToHex(secretKey.getEncoded());
}catch (Exception e){
e.printStackTrace();
return null;
}
}

public static String generateKey () {
return generateKey(DEFAULT_KEY_LENGTH);
}

private static String byteToHex(byte[] bytes){
String strHex = "";
StringBuilder sb = new StringBuilder();
for (byte aByte : bytes) {
strHex = Integer.toHexString(aByte & 0xFF);
sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示,位数不够,高位补0
}
return sb.toString().trim();
}

private static byte[] hexToByte(String hex){
int m = 0, n = 0;
int byteLen = hex.length() / 2;
byte[] ret = new byte[byteLen];
for (int i = 0; i < byteLen; i++) {
m = i * 2 + 1;
n = m + 1;
int intVal = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));
ret[i] = (byte) intVal;
}
return ret;
}

public static String generateIV(){
byte[] ivBytes = new byte[16];
RANDOM.nextBytes(ivBytes);
return byteToHex(ivBytes);
}
}

class Main {
public static void main(String[] args) throws Exception {
String src = "测试待加密数据123456789";
String iv = AesEncryptionUtilsByJdk.generateIV();
System.out.println(iv);
String encryptStr = "ed856e8871798ed42d796ac2965503fffe3405bb1eddb683d160c785e16fbc5c";
String decryptStr = AesEncryptionUtilsByJdk.decrypt(encryptStr);
System.out.println(encryptStr);
System.out.print(decryptStr);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用 AES-128 加密和解密数据的 Java 代码示例: ```java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class AES { private static final String ALGORITHM = "AES"; private static final String TRANSFORMATION = "AES/ECB/PKCS5PADDING"; public static String encrypt(String data, String key) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encrypted = cipher.doFinal(data.getBytes()); return Base64.getEncoder().encodeToString(encrypted); } public static String decrypt(String encryptedData, String key) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedData)); return new String(decrypted); } public static void main(String[] args) throws Exception { String key = "MySecretKey12345"; String data = "Hello, world!"; String encryptedData = encrypt(data, key); String decryptedData = decrypt(encryptedData, key); System.out.println("Original Data: " + data); System.out.println("Encrypted Data: " + encryptedData); System.out.println("Decrypted Data: " + decryptedData); } } ``` 在此示例中,我们使用了密钥 `MySecretKey12345` 来加密和解密数据。请注意,ECB 模式不是最安全的模式,因为它没有使用初始化向量(IV),但是这个示例足以演示 AES 加密和解密的基本原理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值