3DES encryption issues cut or expand string

I am using this code to encrypt a string with 3DES in IOS and I want to decrypt it with php but it generates the 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 characters to the string how can i fix this?

this is how i decrypt using 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);

      

where i call the function to 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];

      

this 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 details

which is the result, I get decryption with php (at the end of the line, it adds strong square characters)

test string with random words

      

that's what i want to get

test string with random words

      

this is the Base64 code generated NSString *base64tring = [[NSString alloc] initWithData:encryptBase64 encoding:NSUTF8StringEncoding];

JuelOxhG5rmLZ32/HNQjxqSPGovPv+lupUz/u0/ryXU=

      

+3


source to share


1 answer


Without this testing, I am guessing that the reason for your "strange bytes" is the PKCS # 5 padding you add to the string on the iOS side. It is not supported by the mcrypt PHP extension, so you will have to uninstall it yourself. For example, using the function from the comment to 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);
} 

      

+2


source







All Articles