TripleDES Encrypting in C# and PHP not coming out the same (PKCS7, ECB)?

后端 未结 4 1990
眼角桃花
眼角桃花 2021-01-14 11:56

I\'ve spent a couple hours now trying to figure this out, but I just can\'t get it to work. I\'ve got a C# encryption routine that I need to match in php. I can\'t change

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 12:38

    The padding length in your PHP version is based on the length of the password. This is incorrect. It should be based on the length of your message instead.

    Try replacing strlen($password) with strlen($data).


    The second problem is that the mcrypt library requires 24-byte keys. Triple DES applies regular DES three times, so you can call the 8-byte key used in each round of DES K1, K2, and K3. There are different ways to choose these keys. The most secure is to choose three distinct keys. Another way is to set K3 equal to K1. The least secure method (equivalent to DES) is to make K1 = K2 = K3.

    Most libraries are "smart" enough to interpret a 16-byte 3DES key as the second option above: K3 = K1. The .NET implementation is doing this for you, but the mcrypt library is not; instead, it's setting K3 = 0. You'll need to fix this yourself, and pass mcrypt a 24-byte key.

    After computing the MD5 hash, take the first 8 bytes of $key, and append them to the end of $key, so that you have a 24-byte value to pass to mcrypt_encrypt().

提交回复
热议问题