我必须用javascript解密laravel 6加密的字符串。.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);下面的屏幕截图给出了上述代码的控制台输出。

我尝试过这么多来自google和堆栈溢出的JS代码,但是没有运气。
更新
这是在单独的脱机系统中解密字符串的要求。我不打算在直播网站上使用javascript。相反,使用java脚本进行解密将在脱机系统上完成。
发布于 2020-07-24 14:12:04
这就是如何使用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!"发布于 2020-07-24 12:25:52
就像@Dusan Malusev已经提到:
您不应该在前端代码中使用Laravel APP_KEY。永远不要,Laravel使用APP_KEY加密所有东西,包括cookie (会话cookie和csrf cookie)。
您的应用程序可能会被黑客攻击,如果它在您的html代码!要回答您的问题:在应用程序的服务器端(在Laravel中)使用Crypt::decrypt($encrypted)。
发布于 2020-12-31 10:11:54
不要在前面使用你的Laravel APP_KEY。这是一个严重的软件漏洞。
您应该在设置和获取数据之前创建一个加密和解密数据的特性。
使用Laravel添加Encryptable.php
<?PHP
namespace App;
use Illuminate\Support\Facades\Crypt;
trait Encryptable
{
public function getAttribute($key)
{
$value = parent::getAttribute($key);
if (in_array($key, $this->encryptable)) {
$value = Crypt::decrypt($value);
}
return $value;
}
public function setAttribute($key, $value)
{
if (in_array($key, $this->encryptable)) {
$value = Crypt::encrypt($value);
}
return parent::setAttribute($key, $value);
}
}在那之后,你可以在你的模型中使用这个特性。添加一个$encryptable属性。要加密和解密的列数组。
class User extends Model
{
use Encryptable;
protected $encryptable = [
'column',
'anotherColumn',
];
}在此之后,您可以像以前一样使用模型。
https://stackoverflow.com/questions/63072938
复制相似问题