java aes key iv_java – AES / CBC真的需要IV参数吗?

我正在编写一个简单的应用程序来使用AES / CBC(模式)加密我的消息.由于我的理解CBC模式需要IV参数,但我不知道为什么我的代码工作没有使用IV参数.有谁能解释为什么?谢谢.

打印的加密消息:T9KdWxVZ5xStaisXn6llfg ==无例外.

public class TestAES {

public static void main(String[] args) {

try {

byte[] salt = new byte[8];

new SecureRandom().nextBytes(salt);

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

KeySpec keySpec = new PBEKeySpec("myPassword".toCharArray(), salt, 100, 128);

SecretKey tmp = keyFactory.generateSecret(keySpec);

SecretKeySpec key = new SecretKeySpec(tmp.getEncoded(), "AES");

Cipher enCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

enCipher.init(Cipher.ENCRYPT_MODE, key);

// enCipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));

byte[] cipherBytes = enCipher.doFinal("myMessage".getBytes());

String cipherMsg = BaseEncoding.base64().encode(cipherBytes);

System.out.println("Encrypted message: " + cipherMsg);

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于Java AES加解密,需要使用到密钥和初始化向量(IV)。下面是一个简单的示例代码: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AesUtil { private static final String ALGORITHM = "AES/CBC/PKCS5Padding"; // 加密算法 private static final String CHARSET = "UTF-8"; // 编码方式 /** * 加密方法 * * @param content 待加密字符串 * @param key 密钥 * @param iv 初始化向量 * @return 加密后的字符串 * @throws Exception */ public static String aesEncrypt(String content, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(CHARSET)); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); byte[] encrypted = cipher.doFinal(content.getBytes(CHARSET)); return Base64.getEncoder().encodeToString(encrypted); } /** * 解密方法 * * @param content 待解密字符串 * @param key 密钥 * @param iv 初始化向量 * @return 解密后的字符串 * @throws Exception */ public static String aesDecrypt(String content, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(CHARSET)); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(content)); return new String(decrypted, CHARSET); } } ``` 其中,`aesEncrypt` 方法用于加密,`aesDecrypt` 方法用于解密。`key` 和 `iv` 参数分别为密钥和初始化向量,需要使用相同的 `key` 和 `iv` 才能成功解密。 使用示例: ```java public static void main(String[] args) throws Exception { String content = "Hello, AES!"; String key = "1234567890123456"; // 密钥长度必须为16/24/32位 String iv = "1234567890123456"; // 初始化向量长度必须为16位 String encrypted = AesUtil.aesEncrypt(content, key, iv); System.out.println("加密后:" + encrypted); String decrypted = AesUtil.aesDecrypt(encrypted, key, iv); System.out.println("解密后:" + decrypted); } ``` 输出结果: ``` 加密后:dS7U2yvJg3GRVTzB+TcHAA== 解密后:Hello, AES! ``` 注意,密钥长度必须为16/24/32位,初始化向量长度必须为16位。此外,加密算法使用的是`AES/CBC/PKCS5Padding`,这是一种安全可靠的加密方式,能够保证数据的机密性和完整性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值