admin管理员组

文章数量:1391836

Is there any JavaScript libs that lets you encrypt and decrypt 256 bit AES the way you do it with mcrypt in PHP (and get the same result of course)? I want to give it a variable-length message and a 32 chars key. All libs I find wants fixed-length blocks of cleartext and byte-arrays of keys.

This is how it's done in PHP:

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
echo mcrypt_encrypt(MCRYPT_RIJNDAEL_256, "32 CHARS THAT REPRESENT MY KEY!!", "hello", MCRYPT_MODE_ECB, $iv);    

Is there any JavaScript libs that lets you encrypt and decrypt 256 bit AES the way you do it with mcrypt in PHP (and get the same result of course)? I want to give it a variable-length message and a 32 chars key. All libs I find wants fixed-length blocks of cleartext and byte-arrays of keys.

This is how it's done in PHP:

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
echo mcrypt_encrypt(MCRYPT_RIJNDAEL_256, "32 CHARS THAT REPRESENT MY KEY!!", "hello", MCRYPT_MODE_ECB, $iv);    
Share Improve this question edited Oct 26, 2012 at 19:13 Peter O. 32.9k14 gold badges85 silver badges97 bronze badges asked Aug 26, 2010 at 16:33 MartinMartin 5,30711 gold badges48 silver badges60 bronze badges 7
  • 1 why would you want to encrypt using javascript? so that your encryption key could be seen by the world? – codeandcloud Commented Aug 26, 2010 at 16:36
  • 2 ah don't worry about that, key is sent through https. – Martin Commented Aug 26, 2010 at 16:43
  • 1 HTTPS or not, the users can still see the key in your JavaScript. So if I for instance visited your site with a HTTPS connection, I could see still the encryption key by just viewing at the source or using a tool such as Firebug. – Freyja Commented Aug 26, 2010 at 16:45
  • I know i know. But it doesn't matter if the user can see it as long as noone else can. – Martin Commented Aug 26, 2010 at 16:51
  • if you trust your user and the connection so much, why not letting HTTPS handle the security? – dcestari Commented Mar 2, 2011 at 21:46
 |  Show 2 more ments

1 Answer 1

Reset to default 6

Yes! I made (the beginnings of) mcrypt for javascript. It doesn't have the exact same interface but it's similar. https://code.google./p/js-mcrypt/

本文标签: AES in JavaScript that matches PHP39s mcryptStack Overflow