我使用Node.js的Crypto库进行加密/解密,如下所示:
encrypt = function(text, passPhrase){
var cipher = crypto.createCipher('AES-128-CBC-HMAC-SHA1', passPhrase);
var crypted = cipher.update(text,'utf8','hex');
crypted += cipher.final('hex');
return crypted;
} ,
decrypt = function(text, passPhrase){
var decipher = crypto.createDecipher('AES-128-CBC-HMAC-SHA1', passPhrase)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
Run Code Online (Sandbox Code Playgroud)
加密部分没有问题.如果我发送正确的passPhrase进行解密,也没有问题.我的问题是,如果我发送'错误'的passPhrase进行解密,代码中断并抛出错误:
TypeError: Bad input string
at Decipher.Cipher.update (crypto.js:279:27)
at module.exports.decrypt (/xxxx/yyyyy/jjj/ssss/encryptionService.js:19:28)
at Object.module.exports.passwordDecryptor (/xxxx/yyyyy/jjj/ssss/encryptionService.js:59:56)
at Object.<anonymous> (/xxxx/yyyyy/jjj/ssss/test.js:32:33)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
Run Code Online (Sandbox Code Playgroud)
我不希望它发生.我想要例如decrypt函数返回'Passpharse是错误的'句子.根据文档输入链接描述,这里 createDecipher …
使用以下节点js:
var crypto = require('crypto');
var encrypt = function (input, password, callback) {
var m = crypto.createHash('md5');
m.update(password);
var key = m.digest('hex');
m = crypto.createHash('md5');
m.update(password + key);
var iv = m.digest('hex');
console.log(iv);
var data = new Buffer(input, 'utf8').toString('binary');
var cipher = crypto.createCipheriv('aes-256-cbc', key, iv.slice(0,16));
var encrypted = cipher.update(data, 'binary') + cipher.final('binary');
var encoded = new Buffer(encrypted, 'binary').toString('base64');
callback(encoded);
};
var decrypt = function (input, password, callback) {
// Convert urlsafe base64 to normal base64
input = input.replace(/\-/g, '+').replace(/_/g, '/'); …Run Code Online (Sandbox Code Playgroud) 由于某种原因,我调用的函数显然不是函数。
Welcome to Node.js v14.15.1.
Type ".help" for more information.
> const crypto = require("crypto");
undefined
> x = crypto.randomBytes(32).toString("hex")
Uncaught TypeError: crypto.randomBytes is not a function
Run Code Online (Sandbox Code Playgroud)
randomBytes()的文档。
有什么我不明白的吗?
我对NodeJ很新,并试图弄清楚如何使用"加密"模块.在玩它时,我注意到NodeJs中的"crypto"模块和crypto-js之间的区别:
有了crypto-js,我有:
function SHA256Hash(password, salt, iteration) {
var saltedpassword = salt + password;
var sha256 = CryptoJS.algo.SHA256.create();
for(var i = 0; i < iteration; i++) {
alert("saltedpassword = " + saltedpassword);
sha256.update(saltedpassword);
var saltedpassword = sha256.finalize();
sha256.reset();
}
return saltedpassword.toString(CryptoJS.enc.Base64);
}
Run Code Online (Sandbox Code Playgroud)
然后打电话:
var hashedPassword = SHA256Hash("123456789", "ASIN", 3)
Run Code Online (Sandbox Code Playgroud)
并收到:
saltedpassword = ASIN123456789
saltedpassword = 3362d80b757d14bfe18c01f6a003ed38a3a4a3dcab0417efb457b71740e21411
saltedpassword = 6020c992a9b7cd3ca9e95b9a3e21b64911edb7983b3dd77bdcecda19f2756987
Run Code Online (Sandbox Code Playgroud)
使用"加密"模块,我写道:
function SHA256Hash(password, salt, iteration) {
var saltedpassword = salt + password;
for(var i = 0; i < iteration-1; i++) {
console.log("saltedpassword …Run Code Online (Sandbox Code Playgroud) 我一直在查看Node中加密模块的文档,我正在试图弄清楚在进行对称加密时如何设置填充.我正在尝试使用带有PKCS5填充的AES-128-ECB.
我无法看到它允许您指定填充的任何地方.我当然希望这可以使用这个库.如何在加密模块中为对称加密指定填充?
我一直在使用 Node 中的加密库来进行公钥交换。到目前为止,我刚刚.getDiffieHellman('modp5')为每个新连接生成一个新的公钥/私钥。此方法非常适合计算用于 AES 加密的秘密。
但是,我希望能够将私钥保存到文件中,然后在下次执行时加载它。似乎执行此操作的方法是.getPrivateKey()then.setPrivateKey()但.setPrivateKey()在crypto.createDiffieHellman由 生成的类上使用.getDiffieHellman('modp5')不起作用。这实际上在文档中有说明:
返回的对象模仿上面 crypto.createDiffieHellman() 创建的对象的接口,但不允许更改密钥(例如使用 diffieHellman.setPublicKey() )。
知道这一点,如果我使用以下代码生成了私钥:
var crypto = require('crypto');
var Key = crypto.getDiffieHellman('modp5');
Key.generateKeys();
var PrvKey = Key.getPrivateKey();
save_to_file(PrvKey);
Run Code Online (Sandbox Code Playgroud)
我如何在以后加载相同的私钥?
var crypto = require('crypto');
var PrvKey = load_from_file();
var Key = crypto.createDiffieHellman(prime, [encoding]);
Key.setPrivateKey( PrvKey )
Run Code Online (Sandbox Code Playgroud)
我查看了加密文档中指定的RFC2412,发现第 5 组的素数列出为:
2^1536 - 2^1472 - 1 + 2^64 * { [2^1406 圆周率] + 741804}
241031242692103258855207602219756607485695054850245994265411 694195810883168261222889009385826134161467322714147790401219 6 50364895705058263194273070680500922306273474534107340669624 601458936165977404102716924945320037872943417032584377865919 8143 …
javascript cryptography public-key-encryption node.js node-crypto
我想加密一个对象然后解密它。加密效果很好,但解密失败。下面是我的代码:
加密扩展.js
const crypto = require("crypto")
const password = "shared_key"
const algorithm = "aes256"
export const encrypt = (text) => {
if(!text) return ''
const cipher = crypto.createCipher(algorithm, password);
let crypted = cipher.update(text, 'utf-8', 'base64');
crypted += cipher.final('base64');
return crypted;
}
export const decrypt = (text) => {
if(!text) return ''
const decipher = crypto.createDecipher(algorithm, password);
let decrypted = decipher.update(text, 'base64', 'utf-8');
decrypted += decipher.final('utf-8');
return decrypted;
}
Run Code Online (Sandbox Code Playgroud)
在我的test.js中,我有:
import {encrypt, decrypt} from './crypto_ext.js'
let test = {key1: …Run Code Online (Sandbox Code Playgroud) 我正在尝试加密/解密。加密工作正常,并将加密数据写入文件。解密时我遇到长度错误问题。我使用了“utf-8”格式,但错误仍然存在。
/ A decrypt function
function decrypt(file) {
let data = JSON.parse(fs.readFileSync(file));
let iv = Buffer.from(data.iv, 'hex');
let encryptedText =
Buffer.from(data.encryptedData, 'hex');
// Creating Decipher
let decipher = crypto.createDecipheriv(
algorithm, Buffer.from(key), iv);
// Updating encrypted text
let decrypted = decipher.update(encryptedText);
let decrypted = Buffer.concat([decrypted, decipher.final()]);
// // returns data after decryption
return decrypted.toString();
}
Run Code Online (Sandbox Code Playgroud)
//run
// Decrypts output
console.log(decrypt('./file.json.enc'));
Run Code Online (Sandbox Code Playgroud)
Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
at Decipheriv.final (internal/crypto/cipher.js:170:29)
at decrypt (/Users/chandrasekarareddy/Documents/projects/encrypt/final.js:48:22)
at Object.<anonymous> (/Users/chandrasekarareddy/Documents/projects/encrypt/final.js:64:13)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js …Run Code Online (Sandbox Code Playgroud) 我使用NodeJS Crypto模块在后端使用 RSA 进行加密和解密,并在前端使用JSencrypt进行前端 RSA 加密和解密
但问题是,每当我使用公钥在前端加密时,我的后端都会抛出此错误(PS:我在 NuxtJS 中使用它,因此使用导入函数。)
const { JSEncrypt } = await import('jsencrypt')
const rsa = new JSEncrypt({ default_key_size: 1024 })
rsa.setPublicKey(store.state.publicKey)
const xKey = rsa.encrypt(store.state.ticket)
Run Code Online (Sandbox Code Playgroud)
然后每当我尝试在后端使用这段代码进行解码时,它就会抛出这个
Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
Run Code Online (Sandbox Code Playgroud)
这是我使用 privateKey 进行 RSA 解码的后端代码
Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
Run Code Online (Sandbox Code Playgroud) 如何将以下 Node 内置的crypto 模块加密转换为CryptoJS?
const crypto = require('crypto');
const pass = 'some,password:)with>spec(chars*'
const cipher1 = crypto.createCipher('aes-256-cbc', pass)
const c1 = cipher1.update(input, 'utf8', 'hex') + cipher1.final('hex')
Run Code Online (Sandbox Code Playgroud)
我尝试了类似的方法,但结果并不相同:
const CryptoJS = require('crypto-js');
const pass = 'some,password:)with>spec(chars*'
const cipher2 = CryptoJS.AES.encrypt(input, pass, {
mode: CryptoJS.mode.CBC,
});
const c2 = cipher2.ciphertext.toString(CryptoJS.enc.Hex);
Run Code Online (Sandbox Code Playgroud)
我需要将此用作 Postman 预请求脚本,因为它不支持 Node 的crypto, 但是crypto-js.
node-crypto ×10
node.js ×10
cryptography ×4
encryption ×4
javascript ×4
cryptojs ×3
aes ×1
jsencrypt ×1
openssl ×1
rsa ×1