开发者

AES decryption not working please help

I am doing AES Encryption and decryption in program. I am not able to get the plain text when i decrypt. My code ins as follows...

- (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)   // f开发者_运维技巧etch 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; 
} 


The key must be same when you encrypt or decrypt data. How are you calling the decrypt method, can you share the code??


You are not specifying the encryption mode. Use CBC or CTR. Do not use ECD as it is insecure. You are specifying a null IV. That may mean that the system is supplying a random (or zero) IV. Much better to specify an IV explicitly, and to ensure that the same IV is use for both encryption and decryption.

Another common source of errors is trying to treat bytes as character data (and vice versa). Be sure to treat bytes as bytes and characters as characters and know which one you are dealing with at all times.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜