使用密钥对消息进行加密。

huangapple go评论91阅读模式
英文:

Encrypt the message with the key

问题

我需要使用公钥来加密消息"Message",并将其转换为HEX编码的预接收数据,保存为一个名为ciphertext.txt的文件。我不需要生成公钥,我已经有一个现成的公钥。使用这个公钥,你需要加密消息。

以下是我能够完成的部分:

package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/x509"
	"encoding/pem"
	"errors"
	"log"
	"os"
)

func main() {
	publicKeyBytes, err := os.ReadFile("publicik.key")
	if err != nil {
		return
	}
	publicKey, err := decodePublicKey(publicKeyBytes)
	if err != nil {
		return
	}
	plaintext := []byte("Message")
	ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, plaintext, nil)
	if err != nil {
		return
	}
	log.Printf("%x", ciphertext)

	privateKeyBytes, err := os.ReadFile("private.key")
	if err != nil {
		return
	}
	privateKey, err := decodePrivateKey(privateKeyBytes)
	if err != nil {
		return
	}
	decryptedtext, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, ciphertext, nil)
	if err != nil {
		return
	}
	log.Printf("%s", decryptedtext)
}

func decodePublicKey(key []byte) (*rsa.PublicKey, error) {
	block, _ := pem.Decode(key)
	if block == nil {
		return nil, errors.New("can't decode pem block")
	}
	publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
	if err != nil {
		return nil, err
	}
	return publicKey, nil
}

func decodePrivateKey(key []byte) (*rsa.PrivateKey, error) {
	block, _ := pem.Decode(key)
	if block == nil {
		return nil, errors.New("can't decode pem block")
	}
	privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		return nil, err
	}
	return privateKey, nil
}

然后我不知道如何解决这个问题?请帮助解决这个问题。

英文:

I need to use a public key to encrypt the message "Message" and save it as a file ciphertext.txt by converting pre-received data in HEX encoding. I do not need the public key to be generated, I already have a ready-made public key. Using this public key, you need to encrypt the message.
Here's what I was able to do:

package main
import (
"crypto/rand" 
"crypto/rsa"
"crypto/sha256" 
"crypto/x509" 
"encoding/pem" 
"errors"
"log"
"os"
)
func main() {
publicKeyBytes, err := os.ReadFile("publicik.key")
if err!= nil {
return
} 
publicKey, err := decodepublicKey(publicKeyBytes)
if  err != nil {
return
}
plaintext := []byte("Message")
ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, plaintext, nil)
if err != nil {
return
}
log.Printf( "%x", ciphertext)
privateKeyBytes, err := os.ReadFile("private.key")
if err != nil {
return
}
privateKey, err := decodePrivateKey(privateKeyBytes)
if err != nil {
return	
}
decryptedtext, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey,ciphertext, nil)
if  err != nil {
return
}
log.Printf("%s", decryptedtext)
}
func decodepublicKey(key []byte) (*rsa.PublicKey, error) {
block, _:= pem.Decode(key) 
if block == nil {
return nil, errors.New("can't decode pem block")
}
publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes) 
if err != nil {
return nil, err
}
return publicKey, nil
}
func decodePrivateKey(key []byte) (*rsa.PrivateKey,error) { block, _ := pem.Decode(key)
if block ==  nil {
return nil, errors.New("can't decode pem block")
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) 
if err != nil {
return nil, err
}
return privateKey, nil
}

Then I don’t know how to solve this problem?
Please help with solving this problem

答案1

得分: 1

我调试了你的代码并发现了两个潜在问题:

  1. 请仔细检查你是否需要使用 x509.ParsePKCS1PublicKey() 还是 x509.ParsePKIXPublicKey()。这取决于你的公钥格式。更多信息请参考:https://stackoverflow.com/a/54775627/9698467
  2. 你可能需要在 decodepublicKey 函数中进行类型断言来断定公钥的类型:return publicKey.(*rsa.PublicKey), nil
英文:

I debugged your code and found 2 potential issues:

  1. Double check whether you need to use x509.ParsePKCS1PublicKey() or x509.ParsePKIXPublicKey(). This depends on the format of your public key. More here: https://stackoverflow.com/a/54775627/9698467
  2. You may need to type assert the public key in your decodepublicKey function: return publicKey.(*rsa.PublicKey), nil.

huangapple
  • 本文由 发表于 2021年12月30日 18:05:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/70530109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定