gpt4 book ai didi

Golang 从 openpgp.js 解密 PGP

转载 作者:行者123 更新时间:2023-12-01 22:16:21 24 4
gpt4 key购买 nike

在 Golang 中需要对称 PGP 解密的帮助,我一直在尝试对 OpenPGP.js 上生成的加密十六进制运行对称解密,不幸的是,在 Golang 中解密没有成功。这是JS中的加密。

const openpgp = require('openpgp')

async function main() {
let options = {
message: openpgp.message.fromBinary(new Uint8Array([0x01, 0x01, 0x01])), // input as Message object
passwords: ['secret stuff'], // multiple passwords possible
armor: false // don't ASCII armor (for Uint8Array output)
}

const cypher_text = await openpgp.encrypt(options)
const encrypted = cypher_text.message.packets.write()

console.log(Buffer.from(encrypted).toString('hex'))

options = {
message: await openpgp.message.read(encrypted), // parse encrypted bytes
passwords: ['secret stuff'], // decrypt with password
format: 'binary' // output as Uint8Array
}

const decrypted = await openpgp.decrypt(options)

console.log(decrypted.data)
}

main()

控制台日志>>
c32e040903088c4db97456263252e0ef4f42627301e0ba3323b141a9ebd0476e5fe848d3c2b6021c8c06581ae2d19f7cd23b011b4b3a68758cb6fb12287db2a9ab6fdfad97670ae995e4deb7ca313d0aa705d264850adefb20353b263fc32ff8dc571f6dce8b722ddbdf40a907

Uint8Array [ 1, 1, 1 ]

我的代码基于以下 GIST https://gist.github.com/jyap808/8250124
package main

import (
"bytes"
"errors"
"io/ioutil"
"log"

"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"

"golang.org/x/crypto/openpgp"
)

func main() {
password := []byte("secret stuff")
packetConfig := &packet.Config{
DefaultCipher: packet.CipherAES256,
}

cypherHex := []byte("c32e040903088c4db97456263252e0ef4f42627301e0ba3323b141a9ebd0476e5fe848d3c2b6021c8c06581ae2d19f7cd23b011b4b3a68758cb6fb12287db2a9ab6fdfad97670ae995e4deb7ca313d0aa705d264850adefb20353b263fc32ff8dc571f6dce8b722ddbdf40a907")

encbuf := bytes.NewBuffer(nil)
w, err := armor.Encode(encbuf, openpgp.SignatureType, nil)
if err != nil {
log.Fatal(err)
}

w.Write(cypherHex)
encbuf.Read(cypherHex)
w.Close()

log.Println(encbuf)

decbuf := bytes.NewBuffer([]byte(encbuf.String()))
armorBlock, err := armor.Decode(decbuf)
if err != nil {
log.Fatalf("Failed on decode %+v\n", err)
}

failed := false
prompt := func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
if failed {
return nil, errors.New("decryption failed")
}
return password, nil
}

md, err := openpgp.ReadMessage(armorBlock.Body, nil, prompt, packetConfig)
if err != nil {
log.Fatalf("Failed on read message %+v\n", err)
}

plaintext, err := ioutil.ReadAll(md.UnverifiedBody)
if err != nil {
log.Fatalf("Failed on read all body %+v\n", err)
}

log.Println(plaintext)
}

我在登录 encbuf 后注意到装甲 key 有点不完整
2020/01/24 22:51:54 jcwYWU5
OTVlNGRlYjdjYTMxM2QwYWE3MDVkMjY0ODUwYWRlZmIyMDM1M2IyNjNmYzMyZmY4
ZGM1NzFmNmRjZThiNzIyZGRiZGY0MGE5MDc=
=9ciH
-----END PGP SIGNATURE-----

更新:如果 EOF 失败,也尝试在没有盔甲的情况下解密

import (
"bytes"
"io/ioutil"
"log"

"golang.org/x/crypto/openpgp"

"golang.org/x/crypto/openpgp/packet"
)

func main() {
password := []byte("secret stuff")
packetConfig := &packet.Config{
DefaultCipher: packet.CipherAES128,
}

cypherHex := []byte("c32e040903088c4db97456263252e0ef4f42627301e0ba3323b141a9ebd0476e5fe848d3c2b6021c8c06581ae2d19f7cd23b011b4b3a68758cb6fb12287db2a9ab6fdfad97670ae995e4deb7ca313d0aa705d264850adefb20353b263fc32ff8dc571f6dce8b722ddbdf40a907")

encbuf := bytes.NewBuffer(nil)
encbuf.Read(cypherHex)

prompt := func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
return password, nil
}

md, err := openpgp.ReadMessage(encbuf, nil, prompt, packetConfig)
if err != nil {
log.Fatalf("Failed on read message %+v\n", err)
}

plaintext, err := ioutil.ReadAll(md.UnverifiedBody)
if err != nil {
log.Fatalf("Failed on read all body %+v\n", err)
}

log.Println(plaintext)
}

最佳答案

首先,您没有解码十六进制编码的密文。使用 encoding/hex 包解码数据:

ct, err := hex.DecodeString(`c32e040903088c4db97456263252e0ef4f42627301e0ba3323b141a9ebd0476e5fe848d3c2b6021c8c06581ae2d19f7cd23b011b4b3a68758cb6fb12287db2a9ab6fdfad97670ae995e4deb7ca313d0aa705d264850adefb20353b263fc32ff8dc571f6dce8b722ddbdf40a907`)
if err != nil {
log.Fatal(err)
}

下一个问题是您错误地创建了 bytes.Buffer 。您没有将数据放入缓冲区,然后调用什么都不做的 Read 方法(并且您确实使用数据对其进行了初始化, Read 无论如何都会在您解密之前“读取”所有数据)。缓冲区可以用数据初始化,或者使用 Write 方法填充——在这种情况下,您只需要一个 io.Reader 并且可以使用 bytes.NewReader
r := bytes.NewReader(ct)

最后,您现在有 3 个 0x01 字节,您可以使用更好的格式更清楚地看到:
d, err := ioutil.ReadAll(md.UnverifiedBody)
if err != nil {
log.Fatal(err)
}

fmt.Printf("%#v\n", d)

https://play.golang.org/p/Y3VqADQvEIH

关于Golang 从 openpgp.js 解密 PGP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59903742/

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