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

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

Encrypt the message with the key

问题

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

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

  1. package main
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/sha256"
  6. "crypto/x509"
  7. "encoding/pem"
  8. "errors"
  9. "log"
  10. "os"
  11. )
  12. func main() {
  13. publicKeyBytes, err := os.ReadFile("publicik.key")
  14. if err != nil {
  15. return
  16. }
  17. publicKey, err := decodePublicKey(publicKeyBytes)
  18. if err != nil {
  19. return
  20. }
  21. plaintext := []byte("Message")
  22. ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, plaintext, nil)
  23. if err != nil {
  24. return
  25. }
  26. log.Printf("%x", ciphertext)
  27. privateKeyBytes, err := os.ReadFile("private.key")
  28. if err != nil {
  29. return
  30. }
  31. privateKey, err := decodePrivateKey(privateKeyBytes)
  32. if err != nil {
  33. return
  34. }
  35. decryptedtext, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, ciphertext, nil)
  36. if err != nil {
  37. return
  38. }
  39. log.Printf("%s", decryptedtext)
  40. }
  41. func decodePublicKey(key []byte) (*rsa.PublicKey, error) {
  42. block, _ := pem.Decode(key)
  43. if block == nil {
  44. return nil, errors.New("can't decode pem block")
  45. }
  46. publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return publicKey, nil
  51. }
  52. func decodePrivateKey(key []byte) (*rsa.PrivateKey, error) {
  53. block, _ := pem.Decode(key)
  54. if block == nil {
  55. return nil, errors.New("can't decode pem block")
  56. }
  57. privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return privateKey, nil
  62. }

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

英文:

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:

  1. package main
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/sha256"
  6. "crypto/x509"
  7. "encoding/pem"
  8. "errors"
  9. "log"
  10. "os"
  11. )
  12. func main() {
  13. publicKeyBytes, err := os.ReadFile("publicik.key")
  14. if err!= nil {
  15. return
  16. }
  17. publicKey, err := decodepublicKey(publicKeyBytes)
  18. if err != nil {
  19. return
  20. }
  21. plaintext := []byte("Message")
  22. ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, plaintext, nil)
  23. if err != nil {
  24. return
  25. }
  26. log.Printf( "%x", ciphertext)
  27. privateKeyBytes, err := os.ReadFile("private.key")
  28. if err != nil {
  29. return
  30. }
  31. privateKey, err := decodePrivateKey(privateKeyBytes)
  32. if err != nil {
  33. return
  34. }
  35. decryptedtext, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey,ciphertext, nil)
  36. if err != nil {
  37. return
  38. }
  39. log.Printf("%s", decryptedtext)
  40. }
  41. func decodepublicKey(key []byte) (*rsa.PublicKey, error) {
  42. block, _:= pem.Decode(key)
  43. if block == nil {
  44. return nil, errors.New("can't decode pem block")
  45. }
  46. publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return publicKey, nil
  51. }
  52. func decodePrivateKey(key []byte) (*rsa.PrivateKey,error) { block, _ := pem.Decode(key)
  53. if block == nil {
  54. return nil, errors.New("can't decode pem block")
  55. }
  56. privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return privateKey, nil
  61. }

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:

确定