加密
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.2 KiB

  1. package crypter
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "encoding/base64"
  7. "encoding/pem"
  8. "errors"
  9. "log"
  10. "os"
  11. )
  12. func RSA_Decode(encryptedData string, privateKey string) (string, error) {
  13. // 解码PEM格式的私钥
  14. block, _ := pem.Decode([]byte(privateKey))
  15. if block == nil {
  16. // fmt.Println("Failed to decode PEM private key")
  17. return "", errors.New("私钥解码失败")
  18. }
  19. // 解析RSA私钥
  20. privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  21. if err != nil {
  22. log.Println("Failed to parse RSA private key", err)
  23. return "", errors.New("私钥解析失败")
  24. }
  25. // 解密Base64编码的数据
  26. encryptedBytes, _ := base64.StdEncoding.DecodeString(encryptedData)
  27. // 使用RSA私钥进行解密
  28. decryptedBytes, err := rsa.DecryptPKCS1v15(rand.Reader, privKey, encryptedBytes)
  29. if err != nil {
  30. // fmt.Println("Failed to decrypt data")
  31. return "", errors.New("解密失败")
  32. }
  33. // 将解密后的字节转换为字符串
  34. return string(decryptedBytes), nil
  35. }
  36. func RSA_Encode(public_key string, msg string) (string, error) {
  37. // publicKeyFile, err := ioutil.ReadFile(public_key)
  38. // if err != nil {
  39. // log.Println("Failed to read public key file:", err)
  40. // return "", errors.New("没有公钥")
  41. // }
  42. block, _ := pem.Decode([]byte(public_key))
  43. if block == nil {
  44. log.Println("Failed to decode public key PEM.")
  45. return "", errors.New("公钥解码失败")
  46. }
  47. publicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
  48. if err != nil {
  49. log.Println("Failed to parse public key:", err)
  50. return "", errors.New("公钥解析失败")
  51. }
  52. rsaPublicKey, ok := publicKey.(*rsa.PublicKey)
  53. if !ok {
  54. log.Println("Failed to get RSA public key.")
  55. return "", errors.New("公钥错误")
  56. }
  57. plainText := []byte(msg)
  58. cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPublicKey, plainText)
  59. if err != nil {
  60. log.Println("Failed to encrypt data:", err)
  61. return "", errors.New("加密失败")
  62. }
  63. return string(cipherText), nil
  64. }
  65. func Create(private_pem_file, public_pem_file string) {
  66. // 生成 RSA 密钥对
  67. privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  68. if err != nil {
  69. log.Println("无法生成私钥:", err)
  70. return
  71. }
  72. // 将私钥保存到文件
  73. privateKeyFile, err := os.Create(private_pem_file)
  74. if err != nil {
  75. log.Println("无法创建私钥文件:", err)
  76. return
  77. }
  78. defer privateKeyFile.Close()
  79. privateKeyBlock := &pem.Block{
  80. Type: "RSA PRIVATE KEY",
  81. Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
  82. }
  83. err = pem.Encode(privateKeyFile, privateKeyBlock)
  84. if err != nil {
  85. log.Println("无法写入私钥文件:", err)
  86. return
  87. }
  88. log.Println("私钥已保存到", private_pem_file)
  89. // 生成公钥
  90. publicKey := &privateKey.PublicKey
  91. // 将公钥保存到文件
  92. publicKeyFile, err := os.Create(public_pem_file)
  93. if err != nil {
  94. log.Println("无法创建公钥文件:", err)
  95. return
  96. }
  97. defer publicKeyFile.Close()
  98. publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
  99. if err != nil {
  100. log.Println("无法编码公钥:", err)
  101. return
  102. }
  103. publicKeyBlock := &pem.Block{
  104. Type: "PUBLIC KEY",
  105. Bytes: publicKeyBytes,
  106. }
  107. err = pem.Encode(publicKeyFile, publicKeyBlock)
  108. if err != nil {
  109. log.Println("无法写入公钥文件:", err)
  110. return
  111. }
  112. log.Println("公钥已保存到", public_pem_file)
  113. }