gpt4 book ai didi

javascript - 如何使用 openpgp.js 加密字符串?

转载 作者:行者123 更新时间:2023-11-28 17:16:07 25 4
gpt4 key购买 nike

我正在尝试使用 openpgp.js 中的使用 PGP key 加密和解密字符串数据示例,但我很难让它在 Firefox 中工作。 openpgp.js doc

我创建了一个 key 对。

const openpgp = window.openpgp; // use as CommonJS, AMD, ES6 module or via window.openpgp

openpgp.config.compression = openpgp.enums.compression.zlib

var options = {
userIds: [{ name: 'Alicee', email: 'alice@example.com' }],
numBits: 2048,
passphrase: 'secretttoo'
};

var publicKeyAlice;
var privateKeyAlice;

openpgp.generateKey(options).then(key => {
privateKeyAlice = key.privateKeyArmored;
publicKeyAlice = key.publicKeyArmored;
console.log('Key generated');
console.log(privateKeyAlice);
console.log(publicKeyAlice);

});

我得到的 key 用于 openpgp.js 的字符串加密示例

const pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK----- Version: OpenPGP.js v4.1.1'
const privkey = '-----BEGIN PGP PRIVATE KEY BLOCK----- Version: OpenPGP.js v4.1.1'
const passphrase = `secretttoo` //what the privKey is encrypted with


const encryptDecryptFunction = async() => {
const privKeyObj = (await openpgp.key.readArmored(privkey)).keys[0]
await privKeyObj.decrypt(passphrase)

const options = {
message: openpgp.message.fromText('Hello, World!'), // input as Message object
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for encryption
privateKeys: [privKeyObj] // for signing (optional)
}

openpgp.encrypt(options).then(ciphertext => {
encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
return encrypted
})
.then(encrypted => {
const options = {
message: await openpgp.message.readArmored(encrypted), // parse armored message
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for verification (optional)
privateKeys: [privKeyObj] // for decryption
}

openpgp.decrypt(options).then(plaintext => {
console.log(plaintext.data)
return plaintext.data // 'Hello, World!'
})

})
}

encryptDecryptFunction();

我在浏览器控制台中收到以下错误:

语法错误:属性列表后缺少 }[了解更多] openpgp testing.html:153:27 注意:{ 在第 152 行第 24 列打开

如何使用 openpgp.js 对字符串进行简单的 pgp 加密?

最佳答案

要真正回答您关于建议另一个库的问题,解决方法是更改​​语法

message: await openpgp.message.readArmored(encrypted),

message: openpgp.message.readArmored(encrypted),

那么它应该可以工作,因为该方法不再是异步的(不再?)

这里您的示例已针对对称加密进行了修改(这就是为什么我无法按照 Nikola 的建议使用 jsencrypt 的原因:

 <script lang="JavaScript" src="openpgp.js"></script>
<script lang="JavaScript">
const options = {
message : window.openpgp.message.fromText('Hello, World!'),
passwords : ['pw'],
armor : false
}

window.openpgp.encrypt(options).then(ciphertext => {
encrypted = ciphertext.message
return encrypted
}).then(encrypted => {
const options = {
message : encrypted,
passwords : ['pw']
}
window.openpgp.decrypt(options).then(plaintext => {
console.log(plaintext.data)
alert(plaintext.data)
return plaintext.data
})
})
</script>

关于javascript - 如何使用 openpgp.js 加密字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com