iphone - AES 解密不起作用,请帮忙

标签 iphone ios encryption aes cryptoapi

我正在程序中进行AES加密和解密。解密时我无法获取纯文本。我的代码如下...

- (NSData *)aesEncrypt:(NSString *)key data:(NSData *)data
{  
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise  
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)  
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)   // fetch key data  
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];   
    NSUInteger dataLength = [data length];   
    //See the doc: For block ciphers, the output size will always be less than or equal to the input size plus the size of one block.  //That's why we need to add the size of one block here  
    size_t bufferSize = dataLength + kCCBlockSizeAES128;  
    void *buffer = malloc(bufferSize);   
    size_t numBytesEncrypted = 0;     
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, 
                                          kCCAlgorithmAES128, 
                                          kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,             
                                          [data bytes], 
                                          dataLength, /* input */             
                                          buffer, bufferSize, /* output */             &
                                          numBytesEncrypted);  
    if (cryptStatus == kCCSuccess) 
    {   
        //the returned NSData takes ownership of the buffer and will free it on deallocation   
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];  
    }   
    free(buffer); //free the buffer;  
    return nil; 
} 

- (NSData *)aesDecrypt:(NSString *)key data:(NSData *)data
{  
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise  
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)  
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)   // fetch key data  
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];   
    NSUInteger dataLength = [data length];   
    //See the doc: For block ciphers, the output size will always be less than or equal to the input size plus the size of one block.  //That's why we need to add the size of one block here  
    size_t bufferSize = dataLength + kCCBlockSizeAES128;  
    void *buffer = malloc(bufferSize);   
    size_t numBytesEncrypted = 0;     
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, 
                                          kCCAlgorithmAES128, 
                                          kCCOptionPKCS7Padding,
                                          keyPtr, 
                                          kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,             
                                          [data bytes], 
                                          dataLength, /* input */             
                                          buffer, 
                                          bufferSize, /* output */             
                                          &numBytesEncrypted);  
    if (cryptStatus == kCCSuccess) 
    {   
        //the returned NSData takes ownership of the buffer and will free it on deallocation   
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];  
    }   
    free(buffer); //free the buffer;  
    return nil; 
} 

最佳答案

加密或解密数据时的 key 必须相同。你是如何调用解密方法的,你能分享一下代码吗?

关于iphone - AES 解密不起作用,请帮忙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7480677/

相关文章:

c# - 在不使用 IDataProtector 的情况下,在 ASP.NET Core 中加密 cookie 的简单/体面的方法是什么?

java - AES 算法安全吗?

iphone - 工作线程 iOS

ios - 球击球时改变角度

c# - 使用 Rijndael 进行跨平台(php 到 C# .NET)加密/解密

ios - 为什么 "print into console"比 ".text into label "快得多

ios - 通过导航 Controller 取消 View 后应用程序崩溃?

iphone - UIImagePicker 在 ios7/iphone 4s 上崩溃

iphone - 如何从 3.875 小时中分离和存储小时和分钟

ios - 对来自 URL 请求的 iOS 字符串使用 SSL