3DES encrypt result in PHP, JAVA and .NET produces different result from 3DES iOS

假如想象 提交于 2019-12-04 23:27:38
zaph

There are a couple of errors:

First: the PHP version uses CBC mode and the iOS version uses ECB mode. The default for CCCrypt is CBC mode, just remove kCCOptionECBMode. Using a null iv will make the first block insecure, generally a random iv is used and prepended to the encrypted data.

Second: mcrypt does not support PKCS#7 padding, it only supports non-standard insecure null padding. Therefore it is necessary to add the padding to the data prior to encryption.

From this SO Answer:

Add PKCS#7 padding (php):
where $block is the block size in bytes and $str is the data to be encrypted

 $pad = $block - (strlen($str) % $block);
 $str .= str_repeat(chr($pad), $pad);

Remove PKCS#7 padding (php):
where $str is the decrypted data

$len = strlen($str);
$pad = ord($str[$len-1]);
$str = $strsubstr($str, 0, $len - $pad);

Note: if the data is exactly a multiple of the block size an entire block of padding will be added, this is necessary.

See PKCS#7 for more information on padding.

For further debugging provide the hex dumps of all parameters and data in and out of the encryption: secretCode, Ds_Merchant_Order, iv and encrypted output.

Finally: For better security consider using RNCryptor which is available for several platforms and languages. It is well vetted, supports the current best practices and is currently supported.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!