LoginSignup
19
22

More than 5 years have passed since last update.

[JavaScript]CryptoJSでAES暗号のsaltとパスフレーズからkeyを求める

Last updated at Posted at 2015-02-11

CryptoJSはクライアントサイドで暗号化や復号化ができる。
AESの鍵長は128bit以上だが、128bitより短いパスフレーズでもCryptoJSなら暗号化できる。
正確にはパスフレーズとsaltから鍵を内部で生成している。

var encrypted = CryptoJS.AES.encrypt('message', 'pass');
console.log(encrypted.salt.toString());  // 467c08b2364309e8
console.log(encrypted.key.toString());   // 6e475a9672994638c4231bd750ab7aceeb9004229f0a26c3622a2045deb789e9

上記のスクリプトのencrypted.keyが実際に使用される鍵である。

AESで暗号化したデータを復号化するときに進捗状況を確認したいのでCryptoJS.algo.AES.createDecryptorを使用しようとしたときに問題がおこった。
サンプルは以下のように書いてあったが、saltpassしかわからない状況では使用することができない。(keyには実際の鍵を入れる必要がある)

var aesDecryptor = CryptoJS.algo.AES.createDecryptor(key, { iv: iv });

var plaintextPart1 = aesDecryptor.process(ciphertextPart1);
var plaintextPart2 = aesDecryptor.process(ciphertextPart2);
var plaintextPart3 = aesDecryptor.process(ciphertextPart3);
var plaintextPart4 = aesDecryptor.process(ciphertextPart4);
var plaintextPart5 = aesDecryptor.finalize();

公式のトップを見ても解決方法がわからなかったのでソースをのぞいてみると以下の式で鍵を生成できることがわかった。

var key = CryptoJS.kdf.OpenSSL.execute(pass, 8, 4, salt).key;

もっといい方法があるかもしれない。

19
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
22