Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triple DES decryption in iOS

Tags:

ios

encryption

I'm currently using the following for Triple DES decryption on iOS:

    NSString* plainText = @"My Text";
    NSString* keyText = @"cf6f1ed3bf0a156e";

    NSData *plainData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSData *keyData = [keyText dataUsingEncoding:NSUTF8StringEncoding];

    size_t bufferSize = plainData.length + kCCBlockSize3DES;
    NSMutableData *cypherData = [NSMutableData dataWithLength:bufferSize];
    size_t movedBytes = 0;

    CCCryptorStatus ccStatus;
    ccStatus = CCCrypt(kCCDecrypt,
            kCCAlgorithm3DES,
            kCCOptionECBMode,
            keyData.bytes,
            kCCBlockSize3DES,
            NULL,
            plainData.bytes,
            plainData.length,
            cypherData.mutableBytes,
            cypherData.length,
            &movedBytes);

    cypherData.length = movedBytes;

    if( ccStatus == kCCSuccess ) {
        NSLog(@"Data: %@",cypherData);
        NSLog(@"Data encoded string: %@",[NSString stringWithUTF8String:[cypherData bytes]]);
        NSLog(@"Data encoded: %@",[[NSString alloc] initWithData:cypherData encoding:NSUTF8StringEncoding]);
    } else {
        NSLog(@"Failed DES decrypt ...");
        return nil;
    }

However, I keep getting the following in the console:

Data: Data encoded string:(null) Data encoded: (null)

Any ideas as to why this is happening? Can anyone see any possible issues with this code?

like image 704
user481610 Avatar asked Dec 19 '16 18:12

user481610


People also ask

Can 3DES be decrypted?

In terms of rank, 3DES is not the most secure algorithm, but that doesn't mean that it's going to be easy for you to break. While 3DES can be broken via bruteforce, it's still not necessarily trivial to do.

What is encryptor and decryptor?

Encryption is the process by which a readable message is converted to an unreadable form to prevent unauthorized parties from reading it. Decryption is the process of converting an encrypted message back to its original (readable) format.

What is the actual key length of 3tdes Triple DES?

Part of what Triple DES does is to protect against brute force attacks. The original DES symmetric encryption algorithm specified the use of 56-bit keys -- not enough, by 1999, to protect against practical brute force attacks. Triple DES specifies the use of three distinct DES keys, for a total key length of 168 bits.

Is a message before it has been encrypted or after it has been decrypted?

Messages and files are encrypted before they leave the phone or computer and aren't decrypted until they reach their destination. Hackers can't access data on the server because they don't have the private keys required to decrypt the data. Instead, secret keys are stored on the individual user's device.


1 Answers

Your key is 16 bytes long. 3DES takes a key that is 24 bytes long (thanks Zaph for correcting; also noting you're only reading 8 bytes of that). This may not be causing this error, but means the key isn't what you think it is.

A series of hex digits in a string are just UTF-8 values. "00" is not 0x00, 0x00. It's 0x30, 0x30.

The reason you're getting (null) here is this:

NSLog(@"Data encoded string: %@",[NSString stringWithUTF8String:[cypherData bytes]]);

Cryptographic output is very unlikely to be a legal UTF-8 string. If you want to encode random data as a string, you need an encoding like hex encoding or Base64 encoding. Base64 is built in and you can use [NSData base64EncodedStringWithOptions:] to encode it.

like image 196
Rob Napier Avatar answered Oct 03 '22 02:10

Rob Napier