iphone - 设备上的 iPhone kccparamerror 中的 AES 加密

标签 iphone ios cryptography aes security-framework

我正在加密像图像文件这样的大文件(可能有任何大小——从 KB 到大 MB)。 我正在使用以下代码进行加密,它在 iPhone Simulator 5.1 上运行良好:

+ (NSData *)encryptedDataForData:(NSData *)data key:(NSData *)key error:(NSError **)error {
   size_t outLength;
   NSMutableData * cipherData = [NSMutableData dataWithLength:data.length + kCCBlockSizeAES128];
   CCCryptorStatus result = CCCrypt(kCCEncrypt,
                 kCCAlgorithmAES128, 
                 kCCOptionPKCS7Padding, 
                 key.bytes, 
                 key.length, 
                 NULL,
                 data.bytes, 
                 data.length, 
                 cipherData.mutableBytes,
                 cipherData.length, 
                 &outLength); 

   if (result == kCCSuccess) {
       cipherData.length = outLength;
   } 
   else {

    NSLog(@"errorcode: %d", result);


    return nil;
}

   return cipherData;
}

但是,当我使用相同的代码加密设备上的某些图像时 - iPhone 5.1.1,

这个加密给我一个 kCCParamError (-4300)。我在模拟器中有相同的值 - 但它在那里工作正常。 有什么帮助吗?

最佳答案

我用错了 key 。

我的 key 由 42 个字符组成。

相反,它应该有 24 个字符。 (使用 key = 123456789012345678901234 有效)。

关于iphone - 设备上的 iPhone kccparamerror 中的 AES 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12406010/

相关文章:

ios - 如何使用分段控件更改按钮操作

c# - 在 Windows Phone 上进行加密

iphone - 在应用程序中实现 iPhone 应用程序

swift - PHPhotoLibrary 出错,无法保存视频

iphone - 向后或向前滑动时检查 UIPageViewController

ios - CVMetalTextureCacheCreate 参数

ios - 基于图像的核心数据模型关系是否正确?

iphone - 知道由于 Dismissal(不是 segue)而出现 ViewController

c# - 使用 SHA256withRSA 的带有私钥签名数据的 X509Certificate2

java - 使用 RSA 解密时收到错误