2011-09-20 59 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

當您加密或解密數據時,密鑰必須相同。你如何調用解密方法,你能分享代碼嗎?