2011-09-20 54 views
0

我在做AES加密和解密的程序。我解密时无法获得纯文本。我的代码如下: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; 
} 
+0

在二进制数据上使用终止符没有意义。二进制数据,特别是如果加密密钥,可能会在某个时候包含您的终止符字符。你只需要跟踪缓冲区中有多少数据。 – indiv

回答

0

您没有指定加密模式。使用CBC或CTR。做不是使用ECD,因为它是不安全的。你正在指定一个空IV。这可能意味着系统提供了一个随机(或零)IV。更好地明确指定IV,并确保同一个IV用于加密和解密。

另一个常见的错误来源是试图将字节视为字符数据(反之亦然)。务必将字节视为字符和字符作为字符,并且始终知道您正在处理哪一个字节。

+0

谢谢!它帮助。 –

1

当您加密或解密数据时,密钥必须相同。你如何调用解密方法,你能分享代码吗?