Javascriptencryption库不能encryption和解密一个简单的例子程序中的string
我正在尝试学习JavaScript,我正在学习一个教程,我想我input正确的一切,但由于某种原因,我使用cypto-js库encryption的string不能正确encryption。 我没有得到一个错误,但未encryption的string是不正确的。 我正在使用一个macintosh和“crypto-js”:“^ 3.1.5”。
这是我的示例代码。
var crypto = require('crypto-js'); var secretMessage = 'I hid the chips under the couch.'; var secretKey = '123abc'; var encryptedMessage = crypto.AES.encrypt(secretMessage, secretKey); console.log('encryptedMessage: ' + encryptedMessage); var bytes = crypto.AES.decrypt(encryptedMessage, secretKey); var decryptedMessage = bytes.toString(crypto.enc.utf8); console.log('decrpt2: ' + decryptedMessage);
这是我得到的结果
$ node example-encryption.js encryptedMessage: U2FsdGVkX180KTEpMiLEjZDSAkhNkmbBuRa9RXFwCgx6gA/PUFr+KOIv6Gr6TgIYrkfUu3F+OM/kRJ3sTTgsfg== decrpt2: 49206869642074686520636869707320756e6465722074686520636f7563682e
有人可以帮忙吗?
谢谢,Greg
你的代码有几个问题,主要是你需要提供一个正确长度的密钥,而且你还需要传递一个cipherParams对象到decrypt()方法而不是密文本身。
以下是一些可用的代码,对于奖励点来说,它通常与大多数系统上的OpenSSL二进制文件和PHP库兼容:
var CryptoJS = require( 'crypto-js' ); var secretMessage = 'I hid the chips under the couch.'; var secretKey = 'b52b4f45b6e9337b57869d7cb718c693'; var encryptedMessage = CryptoJS.AES.encrypt(secretMessage, CryptoJS.enc.Hex.parse(secretKey), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding }); console.log('encryptedMessage: ' + encryptedMessage.ciphertext); cipherParams = CryptoJS.lib.CipherParams.create( {ciphertext: CryptoJS.enc.Hex.parse(encryptedMessage.ciphertext.toString())}); var bytes = CryptoJS.AES.decrypt(cipherParams,CryptoJS.enc.Hex.parse(secretKey), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding }); console.log( 'Decrypted:' + bytes.toString(CryptoJS.enc.Utf8));
没有Initiatlization Vector,因为我们使用ECB而不是CBC。 如果你想要一些安全的东西,每个消息使用随机四的CBC。
- 使用vb.net AES / CBCencryptionstring,并需要使用JavaScript CryptoJS进行解密
- PassportJS – 在将有效载荷数据传递给passport.authenticate作为请求参数之前获取
- PHP – 计算正确的HMAC签名为nodejs脚本
- 使用RC4的NodeJS Crypto产生空白
- 在NodeJS,Crypto令牌authentication环境中生成独特的令牌
- 在Meteor中正确启用filepicker.io的安全性
- nodejsencryption模块与crypto-js
- Rfc2898DeriveBytes在javascript中的等价物?
- 试图嘲笑github webhook请求,得到:“X-Hub-Signature不匹配blob签名”