第1步:pbkdf2
var key = crypto.pbkdf2Sync('prancypoodle', 'sherylcrowe', 10000, 32, 'sha512');
創建的密碼,prancy poodle
,鹽,它sherylcrowe
一鍵創建鍵,重複10000次,出把一個32字節長的密鑰( AES-256-CBC需要這個長度)。AES-256-CBC壞解密
第2步:加密的東西
var cipher = crypto.createCipheriv('aes-256-cbc', key, 'dogsarefun'.toString("binary"));
var crypted = cipher.update('wherearemysocks?');
crypted = Buffer.concat([crypted, cipher.final()]);
第3步:解密&失敗
var decipher = crypto.createDecipheriv('aes-256-cbc', key, 'dogsarefun'.toString('binary'));
var decrypted = decipher.update(crypted);
decrypted = Buffer.concat([decrypted, decipher.final()]);
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Error (native)
at Decipher.Cipher.final (crypto.js:150:26)
at repl:1:48
at REPLServer.defaultEval (repl.js:272:27)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:441:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:188:7)
at REPLServer.Interface._onLine (readline.js:219:10)
我在做什麼錯?這看起來很正確,但卻是錯誤的。
是否有什麼長度的密鑰一個很好的借鑑和IVS不同的算法需要什麼? – Breedly
好點,[Wikipedia](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard)提供了密鑰大小和塊大小,但沒有提及IV大小與塊大小相同,假設得很多。有幾本很好的書,如[*應用密碼學手冊*](http://cacr.uwaterloo.ca/hac/index.html)參見免費下載 *應用密碼學* Bruce Schneier *實用密碼術* Niels Ferguson和Bruce Schneier *加密解密*由HX Mel和Doris M. Baker(我真的很喜歡開局很好)。 – zaph