1

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.

Tyilo
  • 28,998
  • 40
  • 113
  • 198
  • can [this](http://www.tero.co.uk/des/) help you? they have some [examples](http://www.tero.co.uk/des/show.php) – vladkras Jul 09 '13 at 01:00
  • @vladkras I don't get the same result as above. – Tyilo Jul 09 '13 at 01:07
  • Steal the `descrypt` function from this site: http://dmr.ath.cx/misc/pwd/pwd.js – Blender Jul 09 '13 at 01:14
  • @Blender `descrypt('test', 'SO')` -> `TypeError: Cannot read property '116' of undefined` – Tyilo Jul 09 '13 at 01:28
  • @Blender ah, `des_init(); descrypt('test', 'SO');` works perfectly. Could you post this as an answer and I will accept it? – Tyilo Jul 09 '13 at 01:35
  • @Tyilo: It doesn't feel like a decent solution, as it's not portable and pretty messy. You're basically looking for a crypt(3) implementation in JS. – Blender Jul 09 '13 at 01:52
  • @Blender well here it is without the `md5` and other stuff: https://gist.github.com/Tyilo/5954072 – Tyilo Jul 09 '13 at 01:58

1 Answers1

3

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.

Tyilo
  • 28,998
  • 40
  • 113
  • 198