MyBB Community Forums

Full Version: Need help with PHP Cryptography
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ok, i'm writing a registration key script (a library to allow secure product registration) in vb.NET. I need the web element of it (providing the hash to compare to) to be written in PHP for the server side component.

This is the code in vb.NET used to encrypt the string (the opposite is used to decrypt):

myKey = Validate.securityKey
            cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey))
            cryptDES3.Mode = CipherMode.ECB
            Dim desdencrypt As ICryptoTransform = cryptDES3.CreateEncryptor()
            Dim MyASCIIEncoding = New ASCIIEncoding()
            Dim buff() As Byte = ASCIIEncoding.ASCII.GetBytes(myString)
            Encrypt = Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length))

Breakdown:
myKey = Validate.securityKey
This gets the security key (like a password for - this is used by the decrypt function to decrypt the string)

cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey))
This converts the key to MD5, in order to make sure it is 32 (?) bytes (I think).

 cryptDES3.Mode = CipherMode.ECB
This sets the cipher mode to ECB, not the most secure I know but its quickest for this purpose.

 Dim desdencrypt As ICryptoTransform = cryptDES3.CreateEncryptor()
This basically sets up the encryption algorithm.

   Dim MyASCIIEncoding = New ASCIIEncoding()
Not entirely sure if this is needed or no in the vb.NET code Toungue

Dim buff() As Byte = ASCIIEncoding.ASCII.GetBytes(myString)
Converts the string to encrypt to a byte array, so the program can process it one byte (or block) at a time

Encrypt = Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length))
Finally, convert it to Base64, one byte at a time.


Can anyone help? Or does anyone know to use TripleDES-ECB in PHP? I can provide a test string, with key if you need to test it Smile (or provide me with a hash created by your code and I'll test it with my decryptor Toungue)


Thanks guys Smile
There's $10 PayPal in this for whoever creates the first working code Wink
Unfortunately, none of those provide the same hash as my vb.NET function Sad
Try this function:

<?PHP
function encryptNET3DES($key, $vector, $text){
    $td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');

    // Complete the key
    $key_add = 24-strlen($key);
    $key .= substr($key,0,$key_add);

    // Padding the text
    $text_add = strlen($text)%8;
    for($i=$text_add; $i<8; $i++){
        $text .= chr(8-$text_add);
    }

    mcrypt_generic_init ($td, $key, $vector);
    $encrypt64 = mcrypt_generic ($td, $text);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);

     // Return the encrypt text in 64 bits code
    return $encrypt64;
}
?>

(code not written by me)

Another article on the issue: http://mishu666.wordpress.com/2007/08/20...eprovider/

If that still doesn't work, could you just use SHA1? http://www.dreamincode.net/code/snippet1635.htm

and

http://php.net/manual/en/function.sha1.php

It will return exactly 40 bytes. If you need shorter just truncate it.
That's not the same Toungue

I'm using ECB, not CBC
I edited and updated the post, that article seems to be pretty comprehensive.

Perhaps you could post some sample hash values so we could try it out and see if we can get the same results in PHP?
Also, the process has to be reversible, so SHA1 is out of the question Smile
I think I'll switch to CBC, as there are a lot more examples to look at Toungue