problems with 3DES encryption cut or extends the string
-
26-05-2021 - |
سؤال
i'm using that code for encrypt a string with 3DES in IOS and i want decrypt it with php but it generate a string longer if i use kCCOptionPKCS7Padding ( or shorter if i don't use it ) when i decrypt it with php it adds more or less chars to de string how i can fix that ?
that is how i decrypt with php
$key = "f968f8e82961489a8b14b345";
$encrypted = base64_decode($crypt);
$n = mcrypt_module_open(MCRYPT_3DES, null, MCRYPT_MODE_ECB, null);
$fake_iv = str_repeat(chr(0), mcrypt_enc_get_iv_size($n));
mcrypt_generic_init($n, $key, $fake_iv);
$original = mdecrypt_generic($n, $encrypted);
that is where i call function for encrypt
NSString* str= @"test string with random words";
NSData* body =[str dataUsingEncoding:NSUTF8StringEncoding];
NSData *encrypt3DES = [ViewController TripleDES:body encryptOrDecrypt:kCCEncrypt key:@"f968f8e82961489a8b14b345"];
NSData *encryptBase64 = [GTMBase64 encodeData:encrypt3DES];
that is how i encrypt in ios
+ (NSData*)TripleDES:(NSData*)plainData encryptOrDecrypt:(CCOperation)encryptOrDecrypt key:(NSString*)key {
const void *vplainText;
size_t plainTextBufferSize;
plainTextBufferSize = [plainData length];
vplainText = (const void *)[plainData bytes];
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
// uint8_t ivkCCBlockSize3DES;
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);
// memset((void *) iv, 0x0, (size_t) sizeof(iv));
// NSString *key = @"123456789012345678901234";
NSString *initVec = @"init Vec";
const void *vkey = (const void *) [key UTF8String];
const void *vinitVec = (const void *) [initVec UTF8String];
ccStatus = CCCrypt(encryptOrDecrypt,
kCCAlgorithm3DES,
(kCCOptionPKCS7Padding | kCCOptionECBMode),
vkey, //"123456789012345678901234", //key
kCCKeySize3DES,
vinitVec, //"init Vec", //iv,
vplainText, //"Your Name", //plainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
/* if (ccStatus == kCCSuccess) NSLog(@"SUCCESS");
else if (ccStatus == kCCParamError) NSLog( @"PARAM ERROR");
else if (ccStatus == kCCBufferTooSmall) NSLog( @"BUFFER TOO SMALL");
else if (ccStatus == kCCMemoryFailure) NSLog( @"MEMORY FAILURE");
else if (ccStatus == kCCAlignmentError) NSLog( @"ALIGNMENT");
else if (ccStatus == kCCDecodeError) NSLog( @"DECODE ERROR");
else if (ccStatus == kCCUnimplemented) NSLog( @"UNIMPLEMENTED");
*/
NSData *result = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
return result;
}
more info
that is the output i get decrypting with php (at the end of string it add stange square chars)
test string with random words
that is what i want obtaine
test string with random words
that is the Base64 code generated by NSString *base64tring = [[NSString alloc] initWithData:encryptBase64 encoding:NSUTF8StringEncoding];
JuelOxhG5rmLZ32/HNQjxqSPGovPv+lupUz/u0/ryXU=
المحلول
Without having this tested i guess the reason for your "strange bytes" is the PKCS#5 padding that you add to the string on iOS side. It is not supported by PHPs mcrypt extension, so you would have to remove it yourself. For example, by using a function from the commentary on mcrypt
function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}