我试图用64个有效位来解密RC2数据。
因为我只能有64位,所以我的理解是在调用CCCrypt
之前我必须使用一种方法将密钥减少到这么多位。由于我在Apple的CommonCrypto库中找不到任何此类方法,因此我使用此keyschedule method I found。
这些是方法的参数:
void rc2_keyschedule( unsigned short xkey[64],
const unsigned char *key,
unsigned len,
unsigned bits )
对于实际的解密部分,我正在尝试使用example that uses AES 256。这就是我到目前为止所做的:
// setup the key to send to CCCrypt
unsigned char originalKey[16] = /* derived from some other method */;
unsigned short key[64];
unsigned effectiveBits = 64;
rc2_keyschedule(key, originalKey, 16, effectiveBits);
// key is now 128 bytes, and I manually checked it for accuracy
// setup the cipherText to send to CCCrypt
NSData *cipherText = /* derived from some other method */;
// cipherText was manually checked for accuracy
// setup the buffer to send to CCCrypt
size_t bufferSize = [cipherText length] + kCCBlockSizeRC2;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
// call CCCrypt
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmRC2,
kCCOptionPKCS7Padding,
key, 128,
NULL /* initialization vector (optional) */,
[cipherText bytes], [cipherText length],
buffer, bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer); //free the buffer;
return nil;
运行时,cryptStatus
的值为kCCDecodeError
,记录为:
@constant kCCDecodeError输入数据未正确解码或解密。
我将128
作为keyLength
发送给CCCrypt
的原因是因为我的密钥长度为64个短整数,我相信1个短数等于2个字节。因此,64 * 2=128
。
我不知道我需要发送什么选项。我刚刚使用了来自AES示例的kCCOptionPKCS7Padding
。其他可用选项包括kCCOptionECBMode
和CBC
。当我尝试其他两个选项时,cryptStatus
确实变为kCCSuccess
,但数据始终为null
。我认为它错误地报道了成功。
当我说“手动检查某些内容的准确性”时,我的意思是我将这些点的密钥和密码与成功运行的JavaScript实现进行了比较。
如何使用Apple的库(即CommonCrypt)解密RC2数据?