TripleDES - phpseclib equivalent of C# code

289 views Asked by At

I'm using phpseclib for doing the Triple DES Encryption.

This is what I have for encryption.

<?php

use phpseclib\Crypt\TripleDES;

class Foo
{

    public function encrypt()
    {
        $encryptMe = new TripleDES();
        $encryptMe->setKey(md5('cEvu4MHkqz7mQgeqmB6mQEXi'));
        $encryptMe->setIV('jvz8bUAx');
        $encryptMe->paddable = false;

        $cipherText = $encryptMe->encrypt("{phrase:'user123',time:'9/11/2017 02:27:19 PM',username:'user1'}");

        return rawurlencode(base64_encode($cipherText));
        // result : Uru1%2BUpN7oelxj7ngPkkPoK%2FoIw8SoizR8fbDVX4Wicxi8DvY4kcau1fsUVAkNf1oLVr1g4RG0yD2LyL%2FNU7zDWv4cGxVQ%2FL
    }

}

It works but it's not having the same result of my C# code here: http://rextester.com/KNJGP9315

// C# result : Hq3BJmupqU6KuvJQ3aoPvQfopOoAaD0RGoFCIDj6U9uIR%2BJloaS2X7klHSfwhptEuoEz5iPeRNbJnwZmOfM1Aw%3D%3D

EDIT:

My C# Triple DES setting is as follows:

Mode = CBC

Padding = Zeros

I think I missed some setting or something. Any help will do!

2

There are 2 answers

0
Wee Zel On BEST ANSWER

update your php setKey() line to:

$encryptMe->setKey(md5('cEvu4MHkqz7mQgeqmB6mQEXi', true));

and the result is:

Hq3BJmupqU6KuvJQ3aoPvQfopOoAaD0RGoFCIDj6U9uIR%2BJloaS2X7klHSfwhptEuoEz5iPeRNbJnwZmOfM1Aw%3D%3D

0
neubert On

A few observations.

$encryptMe->setKey(md5('cEvu4MHkqz7mQgeqmB6mQEXi'));

PHP's md5() function returns, by default, a hex encoded string. eg. the only characters in the string are 0-F and the string length isn't 16 but rather 32 (since it takes two 0-F characters to represent every byte instead of just a single character).

$encryptMe->paddable = false;

That doesn't do anything. You're probably wanting to do $encryptMe->disablePadding();.