Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the 3des encryption to send and receive data to and from the server. The server side is written in PHP. But some times when trying to decrypting the data from the server it shows the Buffer Too Small. I am attaching the encryption and decryption code

Objective-C
 + (NSData*)decryptedWith3DESUsingKey:(NSData*)text
{
    NSString *key = @"123456789012345678901234";//123456789012345678901234
    NSString *initVec = @"12345678";//init Vec
    NSLog(@"text------%@",text);

    NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
    NSData *iv = [initVec dataUsingEncoding:NSUTF8StringEncoding];
   // NSData *input = [text dataUsingEncoding:NSUTF8StringEncoding];
    size_t dataMoved;
    NSMutableData *decryptedData =
    [NSMutableData dataWithLength:text.length + kCCBlockSize3DES];
    CCCryptorStatus result = CCCrypt(kCCDecrypt,
                                     kCCAlgorithm3DES,
                                     kCCOptionPKCS7Padding, // CBC Padding
                                     keyData.bytes,
                                     keyData.length+1,
                                     iv.bytes,
                                     text.bytes,
                                     text.length,
                                     decryptedData.mutableBytes,//data out
                                     decryptedData.length,
                                     &dataMoved); // total data moved


    if (result == kCCSuccess)
    {
        decryptedData.length = dataMoved;
        return decryptedData;
    }
    else if (result == kCCBufferTooSmall)
        NSLog(@"-BUFFER TOO SMALL");
    else if (result == kCCMemoryFailure)
        NSLog(@"MEMORY FAILURE");
    else if (result == kCCAlignmentError)
        NSLog(@"ALIGNMENT");

    else if (result == kCCDecodeError)
        NSLog(@"DECODE ERROR");
    else if (result == kCCUnimplemented)
        NSLog(@"UNIMPLEMENTED");

    return nil;
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900