【发布时间】:2014-09-02 02:08:42
【问题描述】:
我已经为此工作了一段时间,但找不到解决问题的方法。希望你们中的一个人能告诉我我错过了什么。
我正在使用 NSURLConnection 下载包含 AES128 加密数据的 base64 编码数据。我所拥有的是密钥,见代码,以及加密数据的前 16 个字符是 IV 的知识。我想要的是解码数据,然后使用提取的密钥和 iv 对其进行解密。这是我目前所拥有的:
- (void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(@"Succeeded! Downloaded %d bytes of data", downloadData.length);
NSData *decoded_EncryptedData = [downloadData base64EncodedDataWithOptions:0];
NSString *decoded_EncryptedString = [[NSString alloc] initWithData: decoded_EncryptedData encoding:NSUTF8StringEncoding];
const void *key = @"0000000000000000000000000000000"; // key of length 32 char -> i know standard format for AES128 encryption is 16, maybe this requires 256 AES decryption
const void *iv = (__bridge const void *)([decoded_EncryptedString substringWithRange:NSMakeRange(0,16)]);
NSString *encryptedString = [decoded_EncryptedString substringWithRange:NSMakeRange(16, decoded_EncryptedString.length-16)];
// Now I have no idea what needs to happen, but from online research I found it should be something like this:
NSData encryptedData = [encryptedString dataUsingEncoding:NSUTF8StringEncoding]; // Writing it back into a data file
// Find size of returned data
size_t Size = encryptedData.length + kCCBlockSizeAES128;
// Initialise returned data
NSMutableData *decryptedData = [NSMutableData dataWithLength:Size];
// allocate variable to numBytesDecrypted
size_t numBytesDecrypted;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, 0, KCCKeySizeAES128, iv,
[encryptedData bytes], [encryptedData length], [decryptedData bytes], [decryptedData length],
&numBytesDecrypted);
// Now I test whether the decryption process was successful:
if (cryptStatus == kCCSuccess) {
NSLog(@"Successfully decrypted);
NSString *decryptedString = [[NSString alloc] initWithData:decryptedData encoding: NSUTF8StringEncoding];
}
}
上面的代码确实显示成功解密,但是字符串返回 null 并且大小为 0。有人可以帮我解决这个问题吗?我将不胜感激。
亲切的问候, 伦纳特
【问题讨论】:
-
有几个错别字: NSData encryptedData -> NSData *encryptedData, KCCKeySizeAES128 -> kCCKeySizeAES128,
[decryptedData bytes]->[decryptedData mutableBytes]NSLog(@"解密成功) -> NSLog(@"解密成功" ) 最后关键参数是调用CCCrypt的任务。很明显,这段代码从未编译过,请提供至少已经尝试过的代码。
标签: objective-c encryption base64 rijndael