尝试在 Android 和 iOS 中使用带有 CBC 和 PKCS7 填充的 AES128 算法加密样本数据,但结果不同:(
安卓代码:
private static final byte[] KEY = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
int srcBuffSiz = 1024;
byte[] srcBuff = new byte[srcBuffSiz];
Arrays.fill(srcBuff, (byte)0x01);
SecretKeySpec skeySpec = new SecretKeySpec(KEY, "AES");
Cipher ecipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
ecipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] dstBuff = ecipher.doFinal(srcBuff);
int bytesEncrypted = dstBuff.length;
iOS 代码:
// Source buffer
size_t srcBuffSiz = 1024;
unsigned char* srcBuff = new unsigned char[srcBuffSiz];
memset(srcBuff, 0x01, srcBuffSiz);
// Destination buffer
size_t dstBuffSiz = srcBuffSiz + 128;
unsigned char* dstBuff = new unsigned char[dstBuffSiz];
memset(dstBuff, 0x00, dstBuffSiz);
unsigned char keyPtr[kCCKeySizeAES128] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
size_t bytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES128,
NULL /* initialization vector (optional) */,
srcBuff, srcBuffSiz, /* input */
dstBuff, dstBuffSiz, /* output */
&bytesEncrypted);
因此,在这两种情况下,我都尝试使用预定义的样本密钥加密样本 1024 字节缓冲区(以前填充 0x01 值)。
iOS 中第一个和最后 6 个字节的加密缓冲区:
ED CC 64 27 A8 99 ... 0C 44 9F EC 34 FC
Android中加密缓冲区的第一个和最后6个字节:
AE 65 A9 F7 7F 0E ... 1F BD AE 8B 85 ED
任何的想法?
如果我将 Cipher.getInstance("AES/CBC/PKCS7Padding") 替换为 Cipher.getInstance("AES"),那么加密缓冲区的前几个字节将是相同的,但从第 17 个字节开始......
IOS:
ED CC 64 27 A8 99 DA 83 D5 4A B0 03 0F E7 DD A7 35 F2 50 5C 49 47 CC 3B 2F AB D1 61 05
安卓:
ED CC 64 27 A8 99 DA 83 D5 4A B0 03 0F E7 DD A7 ED CC 64 27 A8 99 DA 83 D5 4A B0 03 0F