简体   繁体   English

使用 CryptoJS 加密字符串 使用 OpenSSL 解密不起作用(对 PBKDF2 使用 Salt 和密码)

[英]Encrypt String with CryptoJS Decrypt with OpenSSL is not working (Using Salt and Password for PBKDF2)

I am using CryptoJS to encrypt the password, here is the example code I am using我正在使用 CryptoJS 加密密码,这是我正在使用的示例代码

  var keySize = 256;
  var ivSize = 128;
  var iterations = 100;
  var message = "Hello World";
  var password = "SecretPassword";
function encrypt (msg, pass) {
  var salt = CryptoJS.lib.WordArray.random(128 / 8); 
  var salt_hex = CryptoJS.enc.Hex.stringify(salt);
  console.log("Salt: "+salt);
  console.log("Salt_hex: ", salt_hex.toUpperCase());
  var key = CryptoJS.PBKDF2(pass, salt, {
      keySize: keySize / 32,
      iterations: iterations
    });
  console.log("Key: "+ key);
  var iv = CryptoJS.lib.WordArray.random(128/8);
  console.log("IV:"+iv);
  var encrypted = CryptoJS.AES.encrypt(msg, key, { 
    iv: iv, 
    padding: CryptoJS.pad.Pkcs7,
    mode: CryptoJS.mode.CBC

  });
  console.log("Enc: ", encrypted.toString());
// salt, iv will be hex 32 in length
  // append them to the ciphertext for use  in decryption
  var transitmessage = salt.toString()+ iv.toString() + encrypted.toString();
  return transitmessage;
}

when the function is executed it is providing the data as follows当执行 function 时,它提供的数据如下

Salt: 923ca1ab6dc88806b05f012c5f7df49f script.js:13:11 Salt_hex: 923CA1AB6DC88806B05F012C5F7DF49F script.js:14:11 Key: 1438927d9bc8a42b9689a93c0fefc8506a5be0819feb2adb03c7b755c92cfd9b script.js:25:11 IV:d739119c45742e4f21eb8c987184b5c9 script.js:27:11 Enc: WUBXNygIgehq90li8p08nw== Salt: 923ca1ab6dc88806b05f012c5f7df49f script.js:13:11 Salt_hex: 923CA1AB6DC88806B05F012C5F7DF49F script.js:14:11 Key: 1438927d9bc8a42b9689a93c0fefc8506a5be0819feb2adb03c7b755c92cfd9b script.js:25:11 IV:d739119c45742e4f21eb8c987184b5c9 script.js:27:11 Enc: WUBXNygIgehq90li8p08nw==

We have to decrypt the generated Encrypted string on the server side using OpenSSL.我们必须使用 OpenSSL 在服务器端解密生成的加密字符串。 So we are doing as follows We are copying the encrypted string to "WUBXNygIgehq90li8p08nw==" cryptenc.txt.enc file and executing the openssl command as follows.所以我们做如下我们将加密的字符串复制到“WUBXNygIgehq90li8p08nw==”cryptenc.txt.enc文件并执行openssl命令如下。

openssl aes-256-cbc -d -pbkdf2 -S 923ca1ab6dc88806b05f012c5f7df49f -iter 100 -k SecretPassword -in cryptenc.txt.enc -out cryptencout.txt openssl aes-256-cbc -d -pbkdf2 -S 923ca1ab6dc88806b05f012c5f7df49f -iter 100 -k SecretPassword -in cryptenc.txt.enc -out cryptencout.txt

The string is not decrypted and getting below error "bad magic number".该字符串未解密并且低于错误“bad magic number”。

Please help me to know what mistake we are doing and what changes to do to make it work请帮助我知道我们在做什么错误以及需要做哪些更改才能使其正常工作

I got it resolved it my self, we have to use the Key and iv generated by Crypto JS to decrypt so the openssl command should be as follows我自己解决了,我们必须使用 Crypto JS 生成的 Key 和 iv 来解密,所以 openssl 命令应该如下

openssl aes-256-cbc -d -pbkdf2 -iter 100 -base64 -K 1438927d9bc8a42b9689a93c0fefc8506a5be0819feb2adb03c7b755c92cfd9b -iv d739119c45742e4f21eb8c987184b5c9 -in cryptoEnc.txt -out crypto.txt openssl aes-256-cbc -d -pbkdf2 -iter 100 -base64 -K 1438927d9bc8a42b9689a93c0fefc8506a5be0819feb2adb03c7b755c92cfd9b -iv d739119c45742e4f21eb8c987184b5c9 -in cryptoEnc.txt -out crypto.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM