2

i am fairly new to iOS development and objective c. I am developing an application which will send encrypted data to a server. The server uses 3des with cbc and no padding. I have read most of the related questions in stackoverflow but still unable to get it work. Been working on this for few days but still unable to get it to match with the server encryption.

Here is what i have work out:

NSString* plaintexthex = @"536176696E67204163636F756E747C313233343536000000";
NSData *dTextIn = [self dataFromHexString:plaintexthex]; //my own way of convert hex to data

NSString* keyhex = @"6E7B336FD2051BA165A9362BD9735531";
NSData *_keyData = [self dataFromHexString:keyhex]; //my own way of convert hex to data

CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;

bufferPtrSize = ([dTextIn length] + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x00, bufferPtrSize);

uint8_t iv[kCCBlockSize3DES];
memset((void *) iv, 0x00, (size_t) sizeof(iv));

unsigned char *bytePtr = (unsigned char *)[_keyData bytes];

ccStatus = CCCrypt(kCCEncrypt,              // CCoperation op
                   kCCAlgorithm3DES,        // CCAlgorithm alg
                   kCCModeCBC,              // CCOptions
                   [_keyData bytes],        // const void *key
                   kCCKeySize3DES,          // 3DES key size length 24 bit
                   iv,                      // const void *iv,
                   [dTextIn bytes],         // const void *dataIn 
                   [dTextIn length],        // size_t dataInLength
                   bufferPtr,               // void *dataOut
                   bufferPtrSize,           // size_t dataOutAvailable
                   &movedBytes);            // size_t *dataOutMoved

NSString *result;
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:            (NSUInteger)movedBytes];
result = [self hexStringFromData:myData];

NSLog(@"Data to encrypt %@",dTextIn);
NSLog(@"Encryption key %@",_keyData);
NSLog(@"Bytes of key are %s ", bytePtr);
NSLog(@"Key length %d ",[_keyData length]);
NSLog(@"Encrypted bytes %@", myData);
NSLog(@"Encrypted string %@", result);
NSLog(@"Encrypted string length %d", [result length]);

- (NSData *)dataFromHexString:(NSString *)string 
{   
    NSMutableData *stringData = [[[NSMutableData alloc] init] autorelease];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < [string length] / 2; i++) {
        byte_chars[0] = [string characterAtIndex:i*2];
        byte_chars[1] = [string characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [stringData appendBytes:&whole_byte length:1]; 
    }
    return stringData;
}

I have develop a similar application on the Android platform and it works well with the server. Heres the encryption of the function i used on the Android platform.

public byte[] encrypt(byte[] key, byte[] message) throws Exception {

       byte [] plainTextBytes = message;
       byte[] encryptKey = key;

       SecretKey theKey = new SecretKeySpec(encryptKey, "DESede");
       Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
       IvParameterSpec IvParameters = new IvParameterSpec(new byte[] {(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00});
       cipher.init(Cipher.ENCRYPT_MODE, theKey, IvParameters);
       byte[] encrypted = cipher.doFinal(plainTextBytes);
       return encrypted;
  }

Basically i want to replicate this similar encryption to be used on the iOS platform. Any help will be welcome and thank you in advance.

jaytsen
  • 21
  • 2
  • @owlstead Thanks for reply. The calculation of the buffer size should be correct. I have searched through a lot of other posts, and all of them used the same way. As for the hex conversion, i displayed the converted values in the log and it seems to match the original hex string. If possible, can u point out my mistake? Thank you. – jaytsen Aug 13 '12 at 06:52
  • Sorry, can't compile Common Crypto libs on Linux, so I cannot debug. – Maarten Bodewes Aug 13 '12 at 17:05
  • I'm facing similar problem. Did you solve ? – kinghomer Sep 27 '12 at 10:04

2 Answers2

1

kCCModeCBC is a mode, not an option. The option you want is 0. CBC is the default mode for CCCrypt(). The default is also no padding.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
0

I am an iOS user, not a developer, but as far as I know, iOS no longer supports 3DES. I use an iPad for VPN, and iOS 3 worked fine with 3DES encryption, but as of iOS 4, the minimum encryption level required is AES128.

Hope that helps.