JS 解密 Laravel 加密字符串

shujat132

我必须laravel 6用javascript解密加密的字符串。输入laravel.env文件

APP_KEY=base64:Rva4FZFTACUe94+k+opcvMdTfr9X5OTfzK3KJHIoXyQ=

并在config/app.php文件密码设置为以下...

'cipher' => 'AES-256-CBC',

到目前为止我尝试过的内容如下......

Laravel 代码

$test = 'this is test';
$encrypted = Crypt::encrypt($test);

HTML 和 Javascript 代码

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>

var encrypted = 'eyJpdiI6IlB4NG0ra2F6SE9PZmVcL0lpUEFIeVlnPT0iLCJ2YWx1ZSI6IlVMQWJyVjcrcUVWZE1jQ25LbG5NTGRla0ZIOUE2MFNFXC9Ed2pOaWJJaXIwPSIsIm1hYyI6IjVhYmJmZDBkMzAwYzMzYzAzY2UzNzY2';
var key = 'Rva4FZFTACUe94+k+opcvMdTfr9X5OTfzK3KJHIoXyQ='; // this is laravel key in .env file
var decrypted = CryptoJS.AES.decrypt(encrypted, key); 
console.log(decrypted);

上面代码的控制台输出如下截图所示......

在此处输入图片说明

我已经尝试了很多来自谷歌和堆栈溢出的其他 JS 代码片段,但没有运气。

更新

这是在单独的离线系统中解密字符串的要求。我不会在实时网站上使用 javascript 进行 dcrypt。而是使用 java 脚本解密将在离线系统上完成。

疯诗人

这就是你如何使用 AES-256-CBC 作为密码解密用 Laravel 编码的 JavaScript 文本。

使用 CryptoJS 4.0...

// Created using Crypt::encryptString('Hello world.') on Laravel.
// If Crypt::encrypt is used the value is PHP serialized so you'll 
// need to "unserialize" it in JS at the end.
var encrypted = 'eyJpdiI6ImRIN3QvRGh5UjhQNVM4Q3lnN21JNFE9PSIsInZhbHVlIjoiYlEvNzQzMnpVZ1dTdG9ETTROdnkyUT09IiwibWFjIjoiM2I4YTg5ZmNhOTgyMzgxYjcyNjY4ZGFkNTc4MDdiZTcyOTIyZjRkY2M5MTM5NTBjMmMyZGMyNTNkMzMwYzY3OCJ9';

// The APP_KEY in .env file. Note that it is base64 encoded binary
var key = 'E2nRP0COW2ohd23+iAW4Xzpk3mFFiPuC8/G2PLPiYDg=';

// Laravel creates a JSON to store iv, value and a mac and base64 encodes it.
// So let's base64 decode the string to get them.
encrypted = atob(encrypted);
encrypted = JSON.parse(encrypted);

console.log('Laravel encryption result', encrypted);



// IV is base64 encoded in Laravel, expected as word array in cryptojs
const iv = CryptoJS.enc.Base64.parse(encrypted.iv);

// Value (chipher text) is also base64 encoded in Laravel, same in cryptojs
const value = encrypted.value;


// Key is base64 encoded in Laravel, word array expected in cryptojs
key = CryptoJS.enc.Base64.parse(key);

// Decrypt the value, providing the IV. 
var decrypted = CryptoJS.AES.decrypt(value, key, {
  iv: iv
});

// CryptoJS returns a word array which can be 
// converted to string like this
decrypted = decrypted.toString(CryptoJS.enc.Utf8);

console.log(decrypted); // Voilà! Prints "Hello world!"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章