Che*_*eng 11 java security encryption android aes
我想知道,如果我使用和不使用IvParameterSpec启动AES密码,有什么区别吗?
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
Run Code Online (Sandbox Code Playgroud)
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
Run Code Online (Sandbox Code Playgroud)
我测试了一些样本测试数据,他们的加密和解密结果相同.
但是,由于我不是安全专家,我不想错过任何东西,并创建一个潜在的安全循环漏洞.我在想,这是正确的方法吗?
小智 20
一点背景(如果您已经知道这一点,我很抱歉,确保我们使用相同的术语是值得的):
(关于分组密码模式的维基百科 - http://en.wikipedia.org/wiki/Block_cipher_mode - 非常好,并且清楚地说明了为什么需要IV.)
不同的块模式对IV选择过程提出了不同的要求,但它们都有一个共同点:
您绝不能使用相同的IV和密钥加密两个不同的消息. 如果你这样做,攻击者通常可以获得你的明文,有时候你的密钥(或等效的数据).
CBC强加了一个额外的约束,即IV必须是攻击者无法预测的 - 因此artjom-b建议使用a SecureRandom
来生成它是一个很好的约束.
此外,正如artjob-b所指出的那样,CBC只会给你机密性.这在实践中意味着您的数据是保密的,但不能保证它是一体的.理想情况下,您应该使用经过身份验证的模式,例如GCM,CCM或EAX.
使用这些模式之一是一个非常非常好的主意.即使对于专家来说,加密 - 然后MAC也很笨拙; 如果可以,请避免使用它.(如果必须这样做,请记住必须使用不同的密钥进行加密和MAC.)
当没有提供 IvParameterSpec 时,密码应该初始化一个随机的 IV 本身,但在你的情况下,它似乎没有这样做(new byte[16]
是一个填充了 0x00 字节的数组)。似乎密码实现已损坏。在这种情况下,您应该始终提供一个新的随机 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));
Run Code Online (Sandbox Code Playgroud)
当你发送或存储密文时,你应该在它前面加上 IV。在解密过程中,您只需要从密文前面切下 IV 即可使用它。它不需要保密,但它应该是独一无二的。
请注意,仅 CBC 模式只能为您提供保密性。如果任何类型的密文操作(恶意或非恶意)都是可能的,那么您应该使用像 GCM 或 EAX 这样的认证模式。除了保密性之外,这些还将为您提供完整性。如果您无权访问这些(SpongyCastle 拥有它们),您可以在先加密后 MAC 方案中使用消息身份验证代码 (MAC),但正确实施要困难得多。
默认情况下,当您加密时 - 您的密码将生成一个随机 IV。当您解密该数据时,您必须完全使用该特定的 IV。
好消息是 IV 不是秘密 - 您可以将其存储在公共场合。主要思想是使每个加密 - 解密操作都不同。
大多数情况下,您需要对各种数据进行加密解密,并为每条数据存储每个 IV 是一件痛苦的事情。这就是为什么 IV 通常与加密数据一起存储在单个字符串中,作为固定大小的前缀。因此,当您解密字符串时 - 您肯定知道前 16 个字节(在我的情况下)是您的 IV,其余字节是加密数据,您需要对其进行解密。
您的有效负载(用于存储或发送)将具有以下结构:
[{IV fixed length not encrypted}{encrypted data with secret key}]
让我分享我的加密和解密方法,我使用的是 AES、256 位密钥、16 位 IV、CBC 模式和 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"
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;
}
Run Code Online (Sandbox Code Playgroud)
这是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;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。