Change the error message to:
NSLog(@"Failed DES decrypt, status: %d", ccStatus);
You will see the status -4300
and look at that in CommonCryptoError.h
to find:
kCCParamError = -4300
@constant kCCParamError Illegal parameter value.
Status errors can be your friend if you do not ignore them. π
You specify 3DES, which should have a key length of 24 bytes, you supply 16 bytes. You should probably upgrade to kCCAlgorithmDES
and kCCBlockSizeDES
(see next paragraph). But the key can be encoded in hexadecimal and must be decoded up to 8 bytes.
In the call, the 5th parameter is size_t keyLength
, but you send kCCBlockSize3DES
, which is 8 bytes. The key and block sizes are not necessarily the same size.
By default, there is no indentation, and this means that the data that must be encrypted must be an exact multiple of the block size (8 bytes). Either add another byte to the input, or specify kCCOptionPKCS7Padding
as an option.
In the general case, it is impossible to express the encryption result directly in the character string, in particular, not in the UTF-8 representation - there are byte values ββthat are not displayed. For this reason, if you need character encoding, then Base64 or hexadecimal are usually used.
Note. It is possible for a key to be 16-byte, and two-element 3DES is needed, in which case duplicate and add the first 8 bytes to the key to make it a 24-byte 3DES key. You need to understand the algorithm, key and parameters.
This sample code works, but is neither optimal nor safe, but the starting point for you:
You can change this to 3DES by setting a 24-byte key and changing kCCAlgorithmDES
to kCCAlgorithm3DES
and kCCKeySizeDES
to kCCKeySize3DES
NSString* plainText = @"My Text-"; NSString* keyText = @"cf6f1ed3"; NSData *plainData = [plainText dataUsingEncoding:NSUTF8StringEncoding]; NSData *keyData = [keyText dataUsingEncoding:NSUTF8StringEncoding]; size_t bufferSize = plainData.length + kCCBlockSizeDES; NSMutableData *cypherData = [NSMutableData dataWithLength:bufferSize]; size_t movedBytes = 0; CCCryptorStatus ccStatus; ccStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES, kCCOptionECBMode, keyData.bytes, kCCKeySizeDES, NULL, plainData.bytes, plainData.length, cypherData.mutableBytes, cypherData.length, &movedBytes); cypherData.length = movedBytes; if( ccStatus == kCCSuccess ) { NSLog(@"Data: %@"encoded,cypherData); } else { NSLog(@"Failed DES decrypt, status: %d", ccStatus); }
But for security reasons, use AES with a random IV, if possible use RNCryptor , it will take care of all the unpleasant but important details for you.