简体   繁体   中英

AES128 iphone encryption matches until the very end

I'm trying to encrypt data to be transferred back and fourth between a server but I can't seem to get the right output in objective-c: (full code here: http://pastebin.com/zPdHxShu )

 // 128-bit key, CBC mode
 // ------------------------
 // IV = '1234567890123456'  
 //  (hex: 31323334353637383930313233343536)
 // Key = '1234567890123456'  
 //  (hex: 31323334353637383930313233343536)
 // PlainText:
 //  'The quick brown fox jumped over the lazy dog'
 // CipherText(hex):
 //  f78176ae 8dfe8457 8529208d 30f446bb b29a64dc 388b5c0b 63140a4f 316b3f34 1fe7d3b1 a3cc5113 c81ef8dd 714a1c99 // correct output
 //  f78176ae 8dfe8457 8529208d 30f446bb b29a64dc 388b5c0b 63140a4f 316b3f34 50f18175 f7a3ad06 2d8033cc d092ca6a // my output
    //                                                                       ^^^ start to get different output here
 //  Note: I get this output in php no problem.

The iPhone is using a standard padding scheme, PKCS5 padding. The output you have labelled as "correct" is using zero padding. I'm not familiar with CCCrypt, but I think if replace kCCOptionPKCS7Padding with 0, and do you own padding with binary zeros, you'll get the same answer.

You should change the PHP side to pad the plaintext using PKCS#5 padding - then you should get the same result. You can use this function (passing a $blocksize of 16) on the plaintext before encryption:

function pkcs5_pad ($text, $blocksize)
{
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM