JavaScript Web暗号化APIを使用してAES-GCMで暗号化したい文字列があります。私はそれを暗号化することができますが、それを復号化するときに約束が拒否され、さまざまな記述的なエラーメッセージが表示されません。AES-GCM復号化約束を拒否する
function aes_encrypt(key, IV, data){
return new Promise(function(resolve, reject){
window.crypto.subtle.encrypt(
{
name: "AES-GCM",
//Don't re-use initialization vectors!
//Always generate a new iv every time your encrypt!
//Recommended to use 12 bytes length
iv: sta(IV),
//Tag length (optional)
tagLength: 128, //can be 32, 64, 96, 104, 112, 120 or 128 (default)
},
key, //from generateKey or importKey above
sta(data) //ArrayBuffer of data you want to encrypt
)
.then(function(encrypted){
//returns an ArrayBuffer containing the encrypted data
resolve(ats(encrypted));
})
.catch(function(err){
console.error(err);
});
});
}
function aes_decrypt(key, IV, data){
return new Promise(function(resolve, reject){
window.crypto.subtle.decrypt(
{
name: "AES-GCM",
iv: sta(IV), //The initialization vector you used to encrypt
tagLength: 128 //The tagLength you used to encrypt (if any)
},
key, //from generateKey or importKey above
sta(data) //ArrayBuffer of the data
)
.then(function(decrypted){
//returns an ArrayBuffer containing the decrypted data
alert(decrypted);
resolve(ats(new Uint8Array(decrypted)));
//resolve(ats(decrypted));
})
.catch(function(err){
console.error(err);
});
});
}
function ecdh_generate_keypair(){
return new Promise(function(resolve, reject){
window.crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-384" //can be "P-256", "P-384", or "P-521"
},
true, //whether the key is extractable (i.e. can be used in exportKey)
["deriveKey", "deriveBits"] //can be any combination of "deriveKey" and "deriveBits"
)
.then(function(key){
//returns a keypair object
resolve(key);
})
.catch(function(err){
console.error(err);
});
});
}
function ecdh_export(key){
return new Promise(function(resolve, reject){
window.crypto.subtle.exportKey(
"jwk", //can be "jwk" (public or private), "raw" (public only), "spki" (public only), or "pkcs8" (private only)
key //can be a publicKey or privateKey, as long as extractable was true
)
.then(function(keydata){
//returns the exported key data
resolve(keydata);
})
.catch(function(err){
console.error(err);
});
});
}
function ecdh_import(key){
return new Promise(function(resolve, reject){
window.crypto.subtle.importKey(
"jwk", //can be "jwk" (public or private), "raw" (public only), "spki" (public only), or "pkcs8" (private only)
key,
{ //these are the algorithm options
name: "ECDH",
namedCurve: "P-384", //can be "P-256", "P-384", or "P-521"
},
true, //whether the key is extractable (i.e. can be used in exportKey)
["deriveKey", "deriveBits"] //"deriveKey" and/or "deriveBits" for private keys only (just put an empty list if importing a public key)
)
.then(function(privateKey){
//returns a privateKey (or publicKey if you are importing a public key)
resolve(privateKey);
})
.catch(function(err){
console.error(err);
});
});
}
function ecdh_derive_key(pub, priv){
return new Promise(function(resolve, reject){
window.crypto.subtle.deriveKey(
{
name: "ECDH",
namedCurve: "P-384", //can be "P-256", "P-384", or "P-521"
public: pub, //an ECDH public key from generateKey or importKey
},
priv, //your ECDH private key from generateKey or importKey
{ //the key type you want to create based on the derived bits
name: "AES-GCM", //can be any AES algorithm ("AES-CTR", "AES-GCM", "AES-CMAC", "AES-GCM", "AES-CFB", "AES-KW", "ECDH", "DH", or "HMAC")
//the generateKey parameters for that type of algorithm
length: 256, //can be 128, 192, or 256
},
true, //whether the derived key is extractable (i.e. can be used in exportKey)
["encrypt", "decrypt"] //limited to the options in that algorithm's importKey
)
.then(function(keydata){
//returns the exported key data
resolve(keydata);
})
.catch(function(err){
console.error(err);
});
});
}
function random_characters(amount){
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < amount; i++){
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
// string-to-arraybuffer
function sta(data){
var enc = new TextEncoder("utf-8");
return enc.encode(data);
}
// arraybuffer-to-string
function ats(data){
var enc = new TextDecoder();
return enc.decode(data);
}
// JSON into and out of the database for cryptokeys
function json_compress(obj){
var s = JSON.stringify(obj);
s = s.replace(/,/g, "♀");
s = s.replace(/{/g, "☺");
s = s.replace(/}/g, "☻");
return s;
}
function json_decompress(str){
str = str.replace(/♀/g, ",");
str = str.replace(/☺/g, "{");
str = str.replace(/☻/g, "}");
return JSON.parse(str);
}
ecdh_generate_keypair().then(function(key){
ecdh_generate_keypair().then(function(key2){
ecdh_derive_key(key2.publicKey, key.privateKey).then(function(aeskey){
var m = "Hello World!";
aes_encrypt(aeskey, "abcdefghijkl", m).then(function(c){
alert(c);
aes_decrypt(aeskey, "abcdefghijkl", c).then(function(r){
alert(r);
});
});
});
});
});
私はAESのためのIVをハードコーディングすることは、セキュリティ上のリスクがあることを承知しているが、私はちょうどテスト目的のために、この作業を取得しようとしています。これが一日中私を悩ませているので、あなたが提供できるどんな助けも大歓迎です。前もって感謝します!
EDIT:クロームデバッグエラーメッセージを追加する
cryptofunctions.js:48 DOMException
(anonymous) @ cryptofunctions.js:48
Promise rejected (async)
(anonymous) @ cryptofunctions.js:47
aes_decrypt @ cryptofunctions.js:31
(anonymous) @ cryptofunctions.js:184
Promise resolved (async)
(anonymous) @ cryptofunctions.js:182
Promise resolved (async)
(anonymous) @ cryptofunctions.js:180
Promise resolved (async)
(anonymous) @ cryptofunctions.js:179
Promise resolved (async)
(anonymous) @ cryptofunctions.js:178
EDIT 2:それはすべての質問に関連すると思われるとしてファイル全体を投稿することを決めました。以下の例外が に遭遇したときの約束を拒否され
であります非常に説明的な "、あなたはこのエラーメッセージを引用することができますか? 'sta'とは何ですか?あなたは[MCVE]を提供できますか? (** Complete **に重点を置いています) – Kaiido
@Kaiido errは呼び出されたときに関数に渡される変数で、エラーメッセージが出力されると思います。 staとatsはそれぞれstringからarrayBufferとarraybufferに変換します。詳細を提供するための編集。 –
'.importKey()'はどこにありますか? – guest271314