- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
解密使用 php/openssl 工作,我可以获得我的纯数据。这是定义的调用:
<?php
function decryptString($data, $key)
{
return base64_decode(openssl_decrypt(base64_decode($data), "AES-256-CBC", $key, true, "h7oehNIHWGNIHxyN"));
}
function encryptString($data, $key)
{
return base64_encode(openssl_encrypt(base64_encode($data), "AES-256-CBC", $key, true, "h7oehNIHWGNIHxyN"));
}
echo 'encrypted: ' . encryptString('my sample text', '7f7720b911c2ecbb22637ed7adef41e82d44b6a0') . "\n";
echo 'decrypted: ' . decryptString('rFWejB1Pj6W3Gh1bheFqDZPMO9POKbhGPOP6eAH9BSk=', '7f7720b911c2ecbb22637ed7adef41e82d44b6a0') . "\n";
我得到这个输出:
encrypted: rFWejB1Pj6W3Gh1bheFqDZPMO9POKbhGPOP6eAH9BSk=
decrypted: my sample text
我尝试使用
cryptojs 实现相同的功能,但事情不起作用,我有时会收到“格式错误的 UTF-8 数据”,有时会出现其他模棱两可的错误(空字符串/无法读取未定义的属性长度,具体取决于我所做的代码更改)。所以这是迄今为止我做的最好的:(我目前有“格式错误的 UTF-8 数据”错误)
function decryptData(encrypted, pass) {
// encrypted is a base64 encoded string
let data = let data = Buffer.from(encrypted, "base64").toString(); // I tried this also [ let data = crypto.enc.Base64.parse(encrypted); ]
let key = crypto.enc.Utf8.parse(pass);
let decrypted = crypto.AES.decrypt(data, key,
{
iv: crypto.enc.Hex.parse('h7oehNIHWGNIHxyN'), // tried passing it as a simple string like this [ iv: 'h7oehNIHWGNIHxyN', ]
mode: crypto.mode.CBC,
padding: crypto.pad.NoPadding // I tried also with [ crypto.pad.Pkcs7 ]
}
);
let result = decrypted.toString(crypto.enc.Utf8); // tried this also [ let result = decrypted.toString(crypto.enc.Base64) ]
}
这是我得到的错误:
/home/vagrant/PhpstormProjects/untitled3/node_modules/crypto-js/core.js:513
throw new Error('Malformed UTF-8 data');
^
Error: Malformed UTF-8 data
at Object.stringify (/home/vagrant/PhpstormProjects/untitled3/node_modules/crypto-js/core.js:513:24)
at WordArray.init.toString (/home/vagrant/PhpstormProjects/untitled3/node_modules/crypto-js/core.js:268:38)
at decodeBase64String (/home/vagrant/PhpstormProjects/untitled3/decryptor.js:13:25)
at Object.<anonymous> (/home/vagrant/PhpstormProjects/untitled3/decryptor.js:19:1)
这可能看起来很明显,但我感到困惑,似乎没有任何效果。你能帮我一下吗?
最佳答案
AES 仅针对 16/24/32 字节 key 定义。您正在使用 40 字节的 key 。 PHP 隐式地将 key 削减为 32 字节,CryptoJS 不会,但由于错误 ( #293 ) 处理 key 而没有错误消息,当然会产生错误的结果。
此外,密文必须作为 CipherParams
传递object 或 Base64 编码,IV 必须是 Utf8 编码,应该使用 PKCS7 padding,并且解密的数据是一个 base64 字符串(仍然需要 Base64 解码)。
以下 CryptoJS 代码解密示例密文:
function decryptData(encrypted, pass) {
let decryptedWA = CryptoJS.AES.decrypt(
encrypted, // Pass ciphertext Base64 encoded (or as CipherParams object)
CryptoJS.enc.Utf8.parse(pass.substring(0, 32)), // Truncate key to 32 bytes
{
iv: CryptoJS.enc.Utf8.parse('h7oehNIHWGNIHxyN'), // UTF8 encode the IV
mode: CryptoJS.mode.CBC, // default
padding: CryptoJS.pad.Pkcs7 // default // Apply PKCS7 padding
}
);
let decryptedB64 = decryptedWA.toString(CryptoJS.enc.Utf8);
let decrypted = CryptoJS.enc.Base64.parse(decryptedB64).toString(CryptoJS.enc.Utf8); // Base64 decode the decrypted data
return decrypted;
}
var ciphertext = "rFWejB1Pj6W3Gh1bheFqDZPMO9POKbhGPOP6eAH9BSk=";
var key = "7f7720b911c2ecbb22637ed7adef41e82d44b6a0";
console.log(decryptData(ciphertext, key));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
关于node.js - 解密工作使用 PHP (openssl) 而不是使用 javascript (cryptojs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66506206/
我想在我的 AngularJS 应用程序中使用 CryptoJS,但我收到此错误:找不到变量:CryptoJS。 我将其包含在我的 index.html 中: 并尝试加密一些东西: var encr
这是我的 Anguarjs 代码 $httpProvider.defaults.headers.common['key'] = CryptoJS.AES.encrypt('', '=', {
我正在尝试在 CryptoJS 上解密并在 PyCrypto 上加密。 我看到 this 一个很好的答案,就像魅力一样,唯一的问题是它调整 PyCrypto 以与 CryptoJS 一起工作,我更喜欢
我正在尝试使用 CryptoJS AES,如下所示: var msg = "café"; var key = "something"; var c = CryptoJS.AES.encrypt(msg
我需要解密用 CryptoJS 加密的东西。我有用于加密的函数、加密对象的结构和用于加密的数据,但我需要知道该对象的一些值。 函数是: var c = CryptoJS.enc.Utf8.parse(
我正在使用 CryptoJS 手动解密具有一组提供的值的字符串。提供了 secret ,然后采用 SHA256。消息和初始化向量是 base 64 编码的。这就是我正在尝试的,但每次我运行它时,输出都
我有一个包含 SHA1 散列数据的密码表。所以我想用 CryptoJS 进行验证输入。它出错了..但是当我尝试显示它时,结果匹配..我编写了错误的函数吗? 这是我的代码 function checkC
我想使用类似于 SpeakEasy 的 javascript 创建一个 HOTP 客户端 上面的库是为服务器端 javascript 使用而设计的,它使用了 NodeJS。 我想在浏览器的前端 jav
我正在尝试编写一个应用程序,以在浏览器中使用 JS 对文件进行端到端加密。但是我似乎无法正确解密所有文件。 TL;DR 由于整体加密大于 1MB 的文件是不切实际的,因此我尝试逐 block 加密它们
我想连接到蓝牙设备。通信仅通过十六进制字符串。我需要编码一个 16 字节的值。因此,我还期望有一个 16 字节的值。在我的实现中,CryptoJS 总是返回更长的结果。根据文档,不需要 IV。 (“所
我在使用 CryptoJS 时遇到问题。 我尝试加密字符串,但它给出了不同的输出。 字符串:“测试消息” key :“123456” 这是我尝试运行的测试代码 var encrypted =
我正在尝试使用 CryptoJS 加密 32 个字符的字符串: var string = '12345678901234567890123456789012'. 我使用 AES256 的 32 个字符
我需要解密 JavaScript 中的十六进制消息,其结果与用 Java 编写的代码完全相同。然而,使用 CryptoJs 的 Javascript 版本返回一个空结果 Java中的代码: priva
我无法使用 jQuery 访问 encryptedData 变量,但在 alert 中它可以工作!!! $( document ).ready(function() { var te
我已经为 RSASHA256 编写了 javascript,但出现错误 crypto.js 未定义。 function aaa(message,secret) { ; ;
我正在尝试使用 CryptoJS 的 PBKDF2 和盐散列来存储我的密码。这是我对注册中的用户密码进行哈希处理的部分: var passwordInput = document.getElement
我正在学习各种哈希技术,并发现了有趣的库,从 cryptoJs 开始 在文档中,有多个选项定义如下散列哈马卡PBKDF2密码编码器 我理解散列是关于生成密文。 HMAC 的作用是生成消息验证代码。但我
我已经使用以下库很多年了,现在它不可用了。 http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js 有人可以帮忙寻找替代
我已经使用以下库很多年了,现在它不可用了。 http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js 有人可以帮忙寻找替代
我正在尝试将Node.js脚本 转换为Golang。但是,我在解码Base64时总是收到"error: illegal base64 data at input byte 40"错误。有人可以帮我,已
我是一名优秀的程序员,十分优秀!