gpgコマンドでの暗号化データをOpenPGP.jsで復号する
#JavaScript #WebブラウザのJavaScript
gpgコマンドでの暗号化データをOpenPGP.jsで復号する。
以下のencryptedのUint8Arrayは以下のコマンドで生成したバイナリ
$ gpg --cipher-algo AES256 -c hello.txt
hello.txtには"hello, world"と書かれている
パスワードは"1234"
コマンド実行後hello.txt.gpgが生成される
Uint8Arrayで使うバイト配列の取得はRubyのFile.read("hello.txt.gpg").unpack("C*")で取得した。
code:js
// Load JS from URL
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = src;
script.onload = resolve;
document.head.appendChild(script);
});
}
(async ()=>{
await loadScript("https://cdnjs.cloudflare.com/ajax/libs/openpgp/4.6.2/compat/openpgp.min.js");
// Encrypted "hello, world" by password "1234"
const encrypted = new Uint8Array(140, 13, 4, 9, 3, 2, 17, 231, 72, 83, 217, 135, 225, 21, 235, 210, 72, 1, 1, 236, 243, 108, 81, 135, 109, 142, 133, 69, 155, 60, 176, 159, 243, 45, 44, 187, 88, 246, 238, 114, 116, 165, 27, 163, 27, 146, 102, 166, 71, 127, 68, 107, 249, 248, 215, 40, 51, 177, 129, 77, 207, 71, 205, 250, 181, 253, 78, 14, 69, 220, 48, 66, 204, 137, 230, 229, 174, 5, 92, 104, 253, 182, 98, 90, 41, 57, 199, 248, 1)
// Decryption
const raw = (async () => {
const options = {
message: await openpgp.message.read(encrypted), // parse encrypted bytes
passwords: '1234', // decrypt with password
format: 'binary' // output as Uint8Array
};
const plaintext = await openpgp.decrypt(options);
return new TextDecoder("utf-8").decode(plaintext.data);
})();
return raw;
})();
Nipp: