Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode TripleDESCryptoService string in php?

the following code decrypts a string in VB:

Public Function Desencriptar(ByVal Input As String) As String

        Dim IV() As Byte = ASCIIEncoding.ASCII.GetBytes("abcdefgh") 
        Dim EncryptionKey() As Byte = Convert.FromBase64String("hereGoesTheKey") 
        Dim buffer() As Byte = Convert.FromBase64String(Input)
        Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
        des.Key = EncryptionKey
        des.IV = IV
        Return Encoding.UTF8.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length()))

    End Function

I would like to know how to duplicate this process into a php script for a mobile app service. Thanks.

like image 999
Karlo A. López Avatar asked Mar 30 '16 17:03

Karlo A. López


1 Answers

this code might help.
 <?php
    $key = "123456";
    function pkcs7_pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    $input = pkcs7_pad("test", 16);//16byte
   // $key = md5(utf8_encode($key), true);
    $td = mcrypt_module_open('tripledes', '', 'ecb', '');//ecb mode
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted_data = mcrypt_generic($td, $input);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);

    echo base64_encode($encrypted_data);
?>

and please look on this link for more detail 3DES

like image 197
Samir Karmacharya Avatar answered Oct 24 '22 06:10

Samir Karmacharya