Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

I'm having trouble with basic encryption/decryption. I've looked all around for a working example but haven't quite found a working example.

-I will be encrypting in php, decrypting with cryptojs for a small layer of security

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js">
<?
$text = "this is the text here";
$key = "encryptionkey";
$msgEncrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));
$msgBase64 = trim(base64_encode($msgEncrypted));
echo "<h2>PHP</h2>";
echo "<p>Encrypted:</p>";
echo $msgEncrypted;
echo "<p>Base64:</p>";
echo $msgBase64;
 ?>
 
<p>AES Decrypt</p>
<script> 
 var key = 'encryptionkey';
 var encrypted = "<?php echo $msgBase64 ?>";
 //tried var base64decode = CryptoJS.enc.Base64.parse(encrypted); 
 var decrypted = CryptoJS.AES.decrypt(encrypted, key);
 console.log( decrypted.toString(CryptoJS.enc.Utf8) );
</script>

Which step am I missing?

Thanks much!

I'm having trouble with basic encryption/decryption. I've looked all around for a working example but haven't quite found a working example.

-I will be encrypting in php, decrypting with cryptojs for a small layer of security

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js">
<?
$text = "this is the text here";
$key = "encryptionkey";
$msgEncrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));
$msgBase64 = trim(base64_encode($msgEncrypted));
echo "<h2>PHP</h2>";
echo "<p>Encrypted:</p>";
echo $msgEncrypted;
echo "<p>Base64:</p>";
echo $msgBase64;
 ?>
 
<p>AES Decrypt</p>
<script> 
 var key = 'encryptionkey';
 var encrypted = "<?php echo $msgBase64 ?>";
 //tried var base64decode = CryptoJS.enc.Base64.parse(encrypted); 
 var decrypted = CryptoJS.AES.decrypt(encrypted, key);
 console.log( decrypted.toString(CryptoJS.enc.Utf8) );
</script>

Which step am I missing?

Thanks much!

I'm having trouble with basic encryption/decryption. I've looked all around for a working example but haven't quite found a working example.

-I will be encrypting in php, decrypting with cryptojs for a small layer of security

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js">
<?
$text = "this is the text here";
$key = "encryptionkey";
$msgEncrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));
$msgBase64 = trim(base64_encode($msgEncrypted));
echo "<h2>PHP</h2>";
echo "<p>Encrypted:</p>";
echo $msgEncrypted;
echo "<p>Base64:</p>";
echo $msgBase64;
 ?>
 
<p>AES Decrypt</p>
<script> 
 var key = 'encryptionkey';
 var encrypted = "<?php echo $msgBase64 ?>";
 //tried var base64decode = CryptoJS.enc.Base64.parse(encrypted); 
 var decrypted = CryptoJS.AES.decrypt(encrypted, key);
 console.log( decrypted.toString(CryptoJS.enc.Utf8) );
</script>

Which step am I missing?

Question Protected by Community Bot
edited tags
Link
user2769
  • 469
  • 1
  • 5
  • 3
Source Link
user2769
  • 469
  • 1
  • 5
  • 3

Encrypt with PHP, Decrypt with Javascript (cryptojs)

I'm having trouble with basic encryption/decryption. I've looked all around for a working example but haven't quite found a working example.

-I will be encrypting in php, decrypting with cryptojs for a small layer of security

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js">
<?
$text = "this is the text here";
$key = "encryptionkey";
$msgEncrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));
$msgBase64 = trim(base64_encode($msgEncrypted));
echo "<h2>PHP</h2>";
echo "<p>Encrypted:</p>";
echo $msgEncrypted;
echo "<p>Base64:</p>";
echo $msgBase64;
 ?>
 
<p>AES Decrypt</p>
<script> 
 var key = 'encryptionkey';
 var encrypted = "<?php echo $msgBase64 ?>";
 //tried var base64decode = CryptoJS.enc.Base64.parse(encrypted); 
 var decrypted = CryptoJS.AES.decrypt(encrypted, key);
 console.log( decrypted.toString(CryptoJS.enc.Utf8) );
</script>

Which step am I missing?

Thanks much!

default

AltStyle によって変換されたページ (->オリジナル) /