RSA

  1. //生成RSA私钥和公钥,保存到文件中
  2. func GenerateRSAKey(bits int) {
  3. //GenerateKey函数使用随机数据生成器random生成一对具有指定字位数的RSA密钥
  4. //Reader是一个全局、共享的密码用强随机数生成器
  5. privateKey, err := rsa.GenerateKey(rand.Reader, bits)
  6. if err != nil {
  7. panic(err)
  8. }
  9. //保存私钥
  10. //通过x509标准将得到的ras私钥序列化为ASN.1 的 DER编码字符串
  11. X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
  12. //使用pem格式对x509输出的内容进行编码
  13. //创建文件保存私钥
  14. privateFile, err := os.Create("private.pem")
  15. if err != nil {
  16. panic(err)
  17. }
  18. defer privateFile.Close()
  19. //构建一个pem.Block结构体对象
  20. privateBlock := pem.Block{Type: "RSA Private Key", Bytes: X509PrivateKey}
  21. //将数据保存到文件
  22. pem.Encode(privateFile, &privateBlock)
  23. //保存公钥
  24. //获取公钥的数据
  25. publicKey := privateKey.PublicKey
  26. //X509对公钥编码
  27. X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
  28. if err != nil {
  29. panic(err)
  30. }
  31. //pem格式编码
  32. //创建用于保存公钥的文件
  33. publicFile, err := os.Create("public.pem")
  34. if err != nil {
  35. panic(err)
  36. }
  37. defer publicFile.Close()
  38. //创建一个pem.Block结构体对象
  39. publicBlock := pem.Block{Type: "RSA Public Key", Bytes: X509PublicKey}
  40. //保存到文件
  41. pem.Encode(publicFile, &publicBlock)
  42. }
  43. func RsaEncrypt(plainText []byte, path string, encryptSize int) string {
  44. //打开文件
  45. file, err := os.Open(path)
  46. if err != nil {
  47. panic(err)
  48. }
  49. defer file.Close()
  50. //读取文件的内容
  51. info, _ := file.Stat()
  52. buf := make([]byte, info.Size())
  53. file.Read(buf)
  54. //pem解码
  55. block, _ := pem.Decode(buf)
  56. //x509解码
  57. publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
  58. if err != nil {
  59. panic(err)
  60. }
  61. //类型断言
  62. publicKey := publicKeyInterface.(*rsa.PublicKey)
  63. //对明文进行加密,分段加密长度为245个字符(2048/8-11)
  64. start := 0
  65. isEncrypt := false
  66. cipherTextStr := ""
  67. var messageTmp []byte
  68. for k, _ := range plainText {
  69. if k%encryptSize == 0 && k != 0 {
  70. messageTmp = plainText[start:k]
  71. start = k
  72. isEncrypt = true
  73. } else if k == len(plainText)-1 {
  74. messageTmp = plainText[start : k+1]
  75. isEncrypt = true
  76. }
  77. if isEncrypt {
  78. isEncrypt = false
  79. cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, messageTmp)
  80. if err != nil {
  81. panic(err)
  82. }
  83. cipherTextStr += string(cipherText)
  84. }
  85. }
  86. //返回密文
  87. return cipherTextStr
  88. }
  89. //RSA解密
  90. func RsaDecrypt(cipherText []byte, path string, decryptSize int) string {
  91. //打开文件
  92. file, err := os.Open(path)
  93. if err != nil {
  94. panic(err)
  95. }
  96. defer file.Close()
  97. //获取文件内容
  98. info, _ := file.Stat()
  99. buf := make([]byte, info.Size())
  100. file.Read(buf)
  101. //pem解码
  102. block, _ := pem.Decode(buf)
  103. //X509解码
  104. privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  105. if err != nil {
  106. panic(err)
  107. }
  108. //对密文进行解密
  109. start := 0
  110. isEncrypt := false
  111. cipherTextStr := ""
  112. var messageTmp []byte
  113. for k, _ := range cipherText {
  114. if k%decryptSize == 0 && k != 0 {
  115. messageTmp = cipherText[start:k]
  116. start = k
  117. isEncrypt = true
  118. } else if k == len(cipherText)-1 {
  119. messageTmp = cipherText[start : k+1]
  120. isEncrypt = true
  121. }
  122. if isEncrypt {
  123. isEncrypt = false
  124. cipherText, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, messageTmp)
  125. if err != nil {
  126. panic(err)
  127. }
  128. cipherTextStr += string(cipherText)
  129. }
  130. }
  131. //返回明文
  132. return cipherTextStr
  133. }

常用签名算法

正序,左右加apiSecret,然后md5转大写

  1. func Sign(data map[string]string, apiSecret string) string {
  2. dataStr := ""
  3. //拼接
  4. for _, v := range sortMap(data) {
  5. dataStr += v + data[v]
  6. }
  7. dataStr = apiSecret + dataStr + apiSecret
  8. m := md5.Sum([]byte(dataStr))
  9. dataStrMd5 := hex.EncodeToString(m[:])
  10. return strings.ToUpper(dataStrMd5)
  11. }
  12. //返回已排序的键(key)数组
  13. func sortMap(data map[string]string) []string {
  14. var keys []string
  15. for k, _ := range data {
  16. keys = append(keys, k)
  17. }
  18. sort.Strings(keys)
  19. return keys
  20. }