2015-02-06 64 views
1

我的客戶端代碼:如何破譯節點JS字符串,它是加密的js在javascript加密

data.username = CryptoJS.AES.encrypt(user.username, "password"); 
data.password = CryptoJS.AES.encrypt(user.password, "password"); 

然後我送「數據」服務器是express.js

var user = req.body; 
var decipher = crypto.createDecipher('aes256', "password"); 
var decrypted = decipher.update(user.username, 'hex', 'utf-8'); 
decrypted += decipher.final('utf-8'); 

我收到此錯誤:

Error: DecipherInit error 
at new Decipher (crypto.js:368:17) 
at Object.Decipher (crypto.js:365:12) 
+0

下面是一些類似的問題和答案:[解密AES256與node.js返回錯誤的最終塊長度](https://stackoverflow.com/q/21292142/608639),[Nodejs解密使用加密錯誤錯誤的最終塊長度](https://stackoverflow.com/q/23111388/608639),[在解密AES256時獲取錯誤的最終塊長度](https://stackoverflow.com/q/32038267/608639),[Decrypt file in Node。 js使用OpenSSL加密](https://stackoverflow.com/q/44482151/608639),[node.js加密解密有什麼問題?](https://stackoverflow.com/q/12219499/608639) – jww 2017-06-11 10:19:21

回答

0

CryptoJS」 encrypt功能使用密碼使用相同的EVP_BytesToKey功能的node.js' createCipher,與CryptoJS使用隨機鹽推導而node does not(重點煤礦)的重要區別:

Note: createCipher derives keys with the OpenSSL function EVP_BytesToKey with the digest algorithm set to MD5, one iteration, and no salt.

要麼你直接使用CryptoJS中這是可能的節點,因爲CryptoJS不有任何依賴關係,或者您自己在兩端執行密鑰派生並使用crypto.createCipheriv。如果您使用前者,則必須另外將用戶名和密碼加密的鹽傳遞給節點。

請注意,data.username是包含salt和IV的CryptoJS cipherParams對象,但是當您將其轉換爲字符串data.username.toString()時,不再包含鹽,但是IV是。這不是您要放入node.js函數的data。改爲發送data.username.ciphertext

+1

[OpenSSL 1.1.0c更改了某些內部組件中使用的摘要算法](http://stackoverflow.com/q/39637388/608639)。以前使用MD5,1.1.0切換到SHA256。請注意,這些更改不會影響'EVP_BytesToKey'和像'openssl enc'這樣的命令。 – jww 2017-01-26 16:30:35