Search code examples
javascriptdescrypt

PHP's crypt function (STD_DES) in javascript


Is there an implementation of the crypt function in PHP written in javascript?

I only need the STD_DES version eg.

PHP:

<?php
    echo crypt('test', 'SO') . "\n";
    // SOVYikZv1wMH.
?>

JS:

console.log(PHP_crypt('test', 'SO'));
// SOVYikZv1wMH.

I have tried to use CryptoJS, but it doesn't seem to work the same way:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/tripledes.js">
</script>
<script>
    var encrypted = CryptoJS.DES.encrypt('test', 'SO');
    console.log(encrypted.toString());
    // U2FsdGVkX1/VopEwWoWNH8SrvmdvM1O9
</script>

Note: I know DES is not secure, I shouldn't use it and X is way more secure than DES.


Solution

  • I have copied the relevant part of the code from http://dmr.ath.cx/misc/pwd/pwd.js, where it is implemented.

    Usage:

    des_init();
    console.log(descrypt('test', 'SO')); // => SOVYikZv1wMH.
    

    The code can be found here.