我刚开始学习Bouncy Castle for AES加密/解密。我使用的是256位密钥的AES/CBC/PKCS7PADDING。
BC可以成功地加密和解密文本,但是在解密之后,我注意到总是有一些null (0x00)填充,因此我的散列比较失败。例如,假设原始输入字符串为“1234567890”,则解密后的字节数组始终为:
{0x49,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x48,0x00,0x00,0x00,0x00,0x00,0x00}为什么填充不是0x06,0x06,0x06,0x06,0x06,0x06?有没有办法在加密后确定填充长度(可能是0),这样在加密之前我就可以得到完全相同的字符串?
发布于 2015-02-19 07:03:39
当您指定PKCS7时,BC将在加密前将填充添加到数据中,并在解密时再次删除它。带有AES块大小的PKCS7将始终添加至少1字节的填充,并将添加足够的数据以使输入成为块大小的倍数。当解密填充时,验证是正确的,并且在PKCS7的情况下,还用作最后解密数据块中有多少是填充的指示符,以及有多少是真实数据。
如果您尝试在解密步骤中不指定PKCS7的情况下解密加密和填充的数据,则填充仍将存在于解密的数据中。
编辑:
为了说明我的观点..下面是一些使用AES/CBC/PKCS7,加密"1234567890“的Java代码,然后在使用和不使用PKCS7填充的情况下再次解密:
public class BCTest {
public static void doTest() throws Exception {
Security.addProvider(new BouncyCastleProvider());
byte[] clearData = "1234567890".getBytes();
SecretKey secretKey = new SecretKeySpec("0123456789ABCDEF".getBytes(), "AES");
AlgorithmParameterSpec IVspec = new IvParameterSpec("0123456789ABCDEF".getBytes());
// encrypt with PKCS7 padding
Cipher encrypterWithPad = Cipher.getInstance("AES/CBC/PKCS7PADDING", "BC");
encrypterWithPad.init(Cipher.ENCRYPT_MODE, secretKey, IVspec);
byte[] encryptedData = encrypterWithPad.doFinal(clearData);
System.out.println("Encryped data (" + encryptedData.length + " bytes): \t" + toHexString(encryptedData));
// decrypt with PKCS7 pad
Cipher decrypterWithPad = Cipher.getInstance("AES/CBC/PKCS7PADDING", "BC");
decrypterWithPad.init(Cipher.DECRYPT_MODE, secretKey, IVspec);
byte[] buffer1 = new byte[encryptedData.length];
int decryptLen1 = decrypterWithPad.doFinal(encryptedData, 0, encryptedData.length, buffer1);
System.out.println("Decrypted with Pad (" + decryptLen1 + " bytes): \t" + toHexString(buffer1));
// decrypt without PKCS7 pad
Cipher decrypterWithoutPad = Cipher.getInstance("AES/CBC/NOPADDING", "BC");
decrypterWithoutPad.init(Cipher.DECRYPT_MODE, secretKey, IVspec);
byte[] buffer2 = new byte[encryptedData.length];
int decryptLen2 = decrypterWithoutPad.doFinal(encryptedData, 0, encryptedData.length, buffer2);
System.out.println("Decrypted without Pad (" + decryptLen2 + " bytes):\t" + toHexString(buffer2));
}
private static String toHexString(byte[] bytes) {
return javax.xml.bind.DatatypeConverter.printHexBinary(bytes);
}
public static void main(String[] args) throws Exception {
BCTest.doTest();
}
}输出:
Encryped data (16 bytes): 602CAE14358D0AC5C96E2D46D17E58E3
Decrypted with Pad (10 bytes): 31323334353637383930000000000000
Decrypted without Pad (16 bytes): 31323334353637383930060606060606当使用padding选项进行解密时,输出中的填充已被剥离-并且密码指示解密数据为10字节-缓冲区的其余部分被0填充。如果不使用padding选项进行解密,则会导致padding现在成为解密数据的一部分。
Edit2:
现在看到原始代码,证实了我的预感。方法GetOutputSize不返回解密字符串的输出大小,而只返回输出缓冲区中所需的最大空间。该方法在BC代码中包含以下文档:
/**
* return the size of the output buffer required for an update plus a
* doFinal with an input of len bytes.
*
* @param len the length of the input.
* @return the space required to accommodate a call to update and doFinal
* with len bytes of input.
*/DoFinal返回放入缓冲区的解密数据的实际长度。
所以在
byte[] plainTextBuffer = new byte[cipher.GetOutputSize(data.Length - IV_LENGTH)];
int length = cipher.DoFinal(data, iv.Length, data.Length - iv.Length, plainTextBuffer, 0);plainTextBuffer将略大于实际解密的数据-数据的实际长度将以length为单位。
发布于 2015-02-20 08:41:07
我使用的是bouncycastle的c#。在我看来,这可能是bouncycastle的一个错误,或者至少bouncycastle的c#实现并不完全遵循pkcs7规范。
我的解决方案是去掉未包含在DoFinal返回长度中的尾部字节。仍然不太确定为什么会有0x00的填充,正如所说的根本不应该存在。
下面是代码。我使用AES/CBC/PKCS7PADDING进行加密和解密。
加密->
ICipherParameters keyParams = ParameterUtilities.CreateKeyParameter("AES", keyByte);
ICipherParameters aesIVKeyParam = new ParametersWithIV(keyParams, StringToByteArray(IV_STRING));
byte[] iv = ((ParametersWithIV) aesIVKeyParam).GetIV();
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7PADDING");
cipher.Init(true, aesIVKeyParam);
byte[] cipherText = new byte[iv.Length + cipher.GetOutputSize(data.Length)];
Array.Copy(iv, 0, cipherText, 0, iv.Length);
int length = cipher.DoFinal(data, 0, data.Length, cipherText, iv.Length);解密->
ICipherParameters keyParams = ParameterUtilities.CreateKeyParameter("AES", keyByte);
byte[] iv = new byte[IV_LENGTH];
Array.Copy(data, 0, iv, 0, IV_LENGTH);
ICipherParameters aesIVKeyParam = new ParametersWithIV(keyParams, iv);
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7PADDING");
cipher.Init(false, aesIVKeyParam);
byte[] plainTextBuffer = new byte[cipher.GetOutputSize(data.Length - IV_LENGTH)];
int length = cipher.DoFinal(data, iv.Length, data.Length - iv.Length, plainTextBuffer, 0);https://stackoverflow.com/questions/28592989
复制相似问题