当前位置: 首页 > news >正文

固定IV在AES加密中的致命隐患

使用固定的IV(初始化向量)在AES加密中是一个严重的安全漏洞。​

下面我将详细解释为什么这是问题,以及如何正确修复它。

为什么固定IV是危险的?

以最常用的CBC模式为例:

  1. 破坏语义安全性​:相同的明文会生成相同的密文,攻击者可以通过分析密文模式获取信息。

  2. 重放攻击​:攻击者可以记录和重放有效的密文块。

  3. 部分明文恢复​:在已知部分明文的情况下,可能推导出其他明文内容。

安全的IV使用原则

IV必须是:​

  • 随机且不可预测​:每次加密都生成新的随机IV

  • 唯一性​:在相同密钥下,IV绝不能重复使用

  • 不需要保密​:IV可以随密文一起存储/传输

修复方案

方案1:随机IV(推荐)

运行

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.SecureRandom;
import java.util.Base64;public class SecureAESUtil {private static final String ALGORITHM = "AES/CBC/PKCS5Padding";private static final int IV_LENGTH = 16; // 128 bits for AES// 加密:生成随机IVpublic static String encrypt(String plaintext, SecretKey key) throws Exception {byte[] iv = new byte[IV_LENGTH];SecureRandom random = new SecureRandom();random.nextBytes(iv); // 生成随机IVCipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));byte[] ciphertext = cipher.doFinal(plaintext.getBytes());// 将IV和密文组合在一起:IV + 密文byte[] encryptedData = new byte[IV_LENGTH + ciphertext.length];System.arraycopy(iv, 0, encryptedData, 0, IV_LENGTH);System.arraycopy(ciphertext, 0, encryptedData, IV_LENGTH, ciphertext.length);return Base64.getEncoder().encodeToString(encryptedData);}// 解密:从密文中提取IVpublic static String decrypt(String encryptedData, SecretKey key) throws Exception {byte[] data = Base64.getDecoder().decode(encryptedData);// 提取前16字节作为IVbyte[] iv = new byte[IV_LENGTH];byte[] ciphertext = new byte[data.length - IV_LENGTH];System.arraycopy(data, 0, iv, 0, IV_LENGTH);System.arraycopy(data, IV_LENGTH, ciphertext, 0, ciphertext.length);Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));byte[] plaintext = cipher.doFinal(ciphertext);return new String(plaintext);}
}

方案2:使用GCM模式(更推荐)

GCM模式提供了认证功能,更安全:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import java.security.SecureRandom;public class SecureAESGCMUtil {private static final String ALGORITHM = "AES/GCM/NoPadding";private static final int IV_LENGTH = 12; // 96 bits recommended for GCMprivate static final int TAG_LENGTH = 128; // 128-bit authentication tagpublic static String encryptGCM(String plaintext, SecretKey key) throws Exception {byte[] iv = new byte[IV_LENGTH];new SecureRandom().nextBytes(iv);Cipher cipher = Cipher.getInstance(ALGORITHM);GCMParameterSpec parameterSpec = new GCMParameterSpec(TAG_LENGTH, iv);cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);byte[] ciphertext = cipher.doFinal(plaintext.getBytes());// 组合:IV + 密文byte[] encryptedData = new byte[IV_LENGTH + ciphertext.length];System.arraycopy(iv, 0, encryptedData, 0, IV_LENGTH);System.arraycopy(ciphertext, 0, encryptedData, IV_LENGTH, ciphertext.length);return Base64.getEncoder().encodeToString(encryptedData);}public static String decryptGCM(String encryptedData, SecretKey key) throws Exception {byte[] data = Base64.getDecoder().decode(encryptedData);byte[] iv = new byte[IV_LENGTH];byte[] ciphertext = new byte[data.length - IV_LENGTH];System.arraycopy(data, 0, iv, 0, IV_LENGTH);System.arraycopy(data, IV_LENGTH, ciphertext, 0, ciphertext.length);Cipher cipher = Cipher.getInstance(ALGORITHM);GCMParameterSpec parameterSpec = new GCMParameterSpec(TAG_LENGTH, iv);cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);byte[] plaintext = cipher.doFinal(ciphertext);return new String(plaintext);}
}

如何迁移现有系统?

如果已有使用固定IV加密的数据,需要:

  1. 重新加密所有数据​:用新方法(随机IV)重新加密所有现有数据

  2. 版本标识​:在加密数据中加入版本标识,区分新旧加密方式

  3. 逐步迁移​:在解密时根据版本选择相应算法

安全要点总结

  • ✅ ​每次加密都使用随机IV

  • ✅ ​IV随密文一起存储/传输

  • ✅ ​考虑使用GCM模式替代CBC

  • ✅ ​使用安全的随机数生成器(SecureRandom)​

  • ❌ ​绝对不要使用固定IV

  • ❌ ​不要在相同密钥下重复使用IV

修复这个漏洞对于保护数据安全至关重要,建议尽快实施修复方案。

http://www.dtcms.com/a/555333.html

相关文章:

  • jsp 哪些网站网站运行团队建设
  • 做网站完整视频杭州网站建站公司
  • 国贸行业 网站建设备案域名购买完过户简单吗
  • 教做月嫂的网站有吗网站公司成本
  • app的网站域名php做网站后台有哪些框架
  • Linux学习笔记--UART子系统
  • 新闻发布网站如果做wordpress漏洞检测
  • 网站头部设计优化青岛做外贸网站建设
  • 东城专业网站建设公司创意二维码制作网站
  • 网站的用户登录一般怎么做的计算机前端开发要学哪些软件
  • 上海网站建设 虹口做个小程序需要多少钱
  • 自适应响应式网站源码城阳网站开发公司
  • 网站源码建站教程wordpress 最新 调用
  • 静态网站可以做哪些网站开发成本计算
  • 蚌埠网站建设费用郑州网站建设网站
  • 国外直播做游戏视频网站有哪些php做的网站首页是什么文件
  • HttpPrinter是一款专为解决Web打印痛点设计的跨平台打印组件
  • dw制作班级网站wordpress升级提示文件流的目标
  • 建设网站是不是必须要服务器电脑培训学校网站
  • 游戏网站织梦模板平台网站建设需求
  • 怎样做自己的视频网站不需要企业提供
  • 到做任务的网站上面推广粉象生wordpress电商主题
  • ae免费模板网站dedecms农业种植网站模板
  • 农村建设有限公司网站苏州工业园区地图
  • C++中使用cpp-httplib和nlohmann_json库实现http请求获取天气数据
  • 网站首页被k 内页还有广告推广合同范本
  • 案例设计:使用stress工具模拟压力的自动伸缩
  • 嘉兴自助建网站3d在线设计网站
  • 免费做图表的网站网站建设中服务器搭建方式
  • 滨州论坛网站建设开封建设局网站