我想知道,如果我初始化 AES 密码,有或没有 IvParameterSpec,有什么区别吗?
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
我用一些样本测试数据进行了测试,它们的加密和解密结果是相同的。
但是,由于我不是安全专家,我不想错过任何事情,并创建潜在的安全漏洞。我想知道哪个是正确的方法?
一些背景知识(如果您已经知道这一点,我很抱歉,值得确保我们使用相同的术语):
(有关分组密码模式的维基百科 - http://en.wikipedia.org/wiki/Block_cipher_mode - 非常好,并且清楚地说明了为什么您需要 IV。)
不同的区块模式对IV选择过程有不同的要求,但它们都有一个共同点:
您绝不能使用相同的 IV 和密钥加密两条不同的消息。 如果这样做,攻击者通常可以获得您的明文,有时甚至可以获得您的密钥(或等效的有用数据)。
CBC 施加了一个额外的约束,即 IV 必须对攻击者来说是不可预测的 - 因此 artjom-b 使用
SecureRandom
来生成 IV 的建议是一个很好的建议。
此外,正如 artjom-b 指出的那样,CBC 只为您提供保密性。这在实践中意味着您的数据是保密的,但不能保证它会完整到达。理想情况下,您应该使用经过身份验证的模式,例如 GCM、CCM 或 EAX。
使用其中一种模式是一个“非常非常好的主意”。即使对于专家来说,加密然后 MAC 也很笨拙;如果可以的话避免它。 (如果必须这样做,请记住您必须使用不同的密钥进行加密和 MAC。)
好消息是 IV 不是秘密——你可以公开存储它。主要思想是让每个加密解密操作都不同。
大多数时候,您需要加密解密各种数据,并且为每条数据存储每个 IV 是一件痛苦的事情。 这就是为什么 IV 通常与加密数据一起存储在单个字符串中,作为固定大小的前缀。 这样,当您解密字符串时 - 您肯定知道前 16 个字节(在我的例子中)是您的 IV,其余字节 - 是加密数据,您需要解密它。
您的有效负载(用于存储或发送)将具有以下结构:
[{IV fixed length not encrypted}{encrypted data with secret key}]
让我分享一下我的加密和解密方法,我使用的是 AES、256 位密钥、16 位 IV、CBC MODE 和 PKCS7Padding。
正如 Justin King-Lacroix 上面所说,您最好使用 GCM、CCM 或 EAX 块模式。不要使用欧洲央行!
encrypt()
方法的结果是安全的,可以存储在数据库中或发送到任何地方。
注意可以使用自定义 IV 的注释 - 只需将 new SecureRandom() 替换为 new IvParameterSpec(getIV()) (您可以在那里输入静态 IV,但强烈不推荐)
private Key secretAes256Key
是一个带有密钥的类字段,它在构造函数中初始化。
private static final String AES_TRANSFORMATION_MODE = "AES/CBC/PKCS7Padding"
encrypt()
方法:
public String encrypt(String data) {
String encryptedText = "";
if (data == null || secretAes256Key == null)
return encryptedText;
}
try {
Cipher encryptCipher = Cipher.getInstance(AES_TRANSFORMATION_MODE);
encryptCipher.init(Cipher.ENCRYPT_MODE, secretAes256Key, new SecureRandom());//new IvParameterSpec(getIV()) - if you want custom IV
//encrypted data:
byte[] encryptedBytes = encryptCipher.doFinal(data.getBytes("UTF-8"));
//take IV from this cipher
byte[] iv = encryptCipher.getIV();
//append Initiation Vector as a prefix to use it during decryption:
byte[] combinedPayload = new byte[iv.length + encryptedBytes.length];
//populate payload with prefix IV and encrypted data
System.arraycopy(iv, 0, combinedPayload, 0, iv.length);
System.arraycopy(encryptedBytes, 0, combinedPayload, iv.length, encryptedBytes.length);
encryptedText = Base64.encodeToString(combinedPayload, Base64.DEFAULT);
} catch (NoSuchAlgorithmException | BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException | UnsupportedEncodingException | InvalidKeyException e) {
e.printStackTrace();
}
return encryptedText;
}
这是
decrypt()
方法:
public String decrypt(String encryptedString) {
String decryptedText = "";
if (encryptedString == null || secretAes256Key == null)
return decryptedText;
}
try {
//separate prefix with IV from the rest of encrypted data
byte[] encryptedPayload = Base64.decode(encryptedString, Base64.DEFAULT);
byte[] iv = new byte[16];
byte[] encryptedBytes = new byte[encryptedPayload.length - iv.length];
//populate iv with bytes:
System.arraycopy(encryptedPayload, 0, iv, 0, 16);
//populate encryptedBytes with bytes:
System.arraycopy(encryptedPayload, iv.length, encryptedBytes, 0, encryptedBytes.length);
Cipher decryptCipher = Cipher.getInstance(AES_TRANSFORMATION_MODE);
decryptCipher.init(Cipher.DECRYPT_MODE, secretAes256Key, new IvParameterSpec(iv));
byte[] decryptedBytes = decryptCipher.doFinal(encryptedBytes);
decryptedText = new String(decryptedBytes);
} catch (NoSuchAlgorithmException | BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException | InvalidKeyException e) {
e.printStackTrace();
}
return decryptedText;
}
希望这有帮助。
会初始化一个随机 IV 本身,但在你的情况下,它似乎不会这样做(new byte[16]
是一个充满 0x00 字节的数组)。看来 Cipher 实现已被破坏。在这种情况下,您应该始终提供新的随机 IV(语义安全所必需的)。
这通常是这样完成的:
SecureRandom r = new SecureRandom(); // should be the best PRNG
byte[] iv = new byte[16];
r.nextBytes(iv);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv));
当您发送或存储密文时,您应该在其前面添加 IV。解密时,只需将密文前面的 IV 切掉即可使用。它不需要保密,但应该是唯一的。
请注意,CBC 模式本身只能为您提供机密性。如果可能对密文进行任何类型的操作(恶意或非恶意),那么您应该使用 GCM 或 EAX 等身份验证模式。除了保密性之外,这些还可以为您提供诚信。如果您无权访问这些内容(SpongyCastle 有它们),您可以在先加密后 MAC 方案中使用消息身份验证代码 (MAC),但正确实施要困难得多。