的源代碼window.crypto解密:https://github.com/HectorAnadon/Crypto-NodeJS-window.crypto用Node.js的加密和加密與服務的工人
我使用AES-128-GCM從服務器的Node.js發送加密圖片的服務人員(腳本運行在Web瀏覽器背景中),我試圖解密它。通訊效果良好,因爲Web瀏覽器中顯示未加密的圖片。我得到一個異常,這是控制檯爲我打印的內容:解密錯誤:OperationError :(匿名函數)@ service_worker.js錯誤代碼:310
service_worker.js中的行310是:console.error(「Decryption error:」+ err); // Line 310
你對我在做什麼有什麼不瞭解嗎?非常感謝,我非常感謝。
下面是使用加密的Node.js(文件位置:https://nodejs.org/api/crypto.html)加密代碼
// Nodejs encryption with GCM
// Does not work with nodejs v0.10.31
var fs = require('fs');
var crypto = require('crypto');
var algorithm = 'aes-128-gcm';
var key = '3zTvzr3p67VC61jm';
// generate a new iv for each encryption
// As this is for testing I always use the same iv
var iv = '60iP0h6vJoEa';
function encrypt(text) {
var cipher = crypto.createCipheriv(algorithm, key, iv)
var encrypted = cipher.update(text);
return encrypted;
}
var text = fs.readFileSync('mustang_encrypted.jpg');
var hw = encrypt(text);
而這種使用window.crypto是服務工作者解密代碼(文件位置:https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto)
//ArrayBuffer of the data we received
function(bodyArrayBuffer) {
var cryptoObj = crypto;
var cryptoSubtle = cryptoObj.subtle;
/*
* IMPORT KEY
*/
string2ArrayBuffer("3zTvzr3p67VC61jm", function (keyBuffer) {
console.log("keyBuffer length: " + keyBuffer.byteLength);
cryptoSubtle.importKey(
"raw", //can be "jwk" or "raw"
keyBuffer,
{ //this is the algorithm options
name: "AES-GCM",
},
false, //whether the key is extractable (i.e. can be used in exportKey)
["encrypt", "decrypt"] //can be any combination of "encrypt" and "decrypt"
).then(function(key){
//returns the symmetric key
/*
* DECRYPTION
*/
string2ArrayBuffer("60iP0h6vJoEa",function (myIV) {
cryptoSubtle.decrypt(
{
name: "AES-GCM",
iv: myIV, //The initialization vector you used to encrypt
tagLength: 128, //The tagLength you used to encrypt
},
key, //from generateKey or importKey above
bodyArrayBuffer //ArrayBuffer of the data
)
.then(function(decrypted){
//returns an ArrayBuffer containing the decrypted data
console.log(new Uint8Array(decrypted));
//send response
var newResponse = new Response(decrypted, init);
console.log("Returning \"decrypted\" response!");
accept(newResponse);
})
.catch(function(err){
console.error("Decryption error: " + err); //Line 310
});
});
})
.catch(function(err){
console.error(err);
});
});
}
您是否有可用的示例的完整源代碼? –
我將創建一個公共github存儲庫,以便每個人都可以使用它。 – HectorAnadon
這是源代碼:https://github.com/HectorAnadon/Crypto-NodeJS-window.crypto – HectorAnadon