1. 程式人生 > 其它 >Go 語言學習的必要性(對於運維工程師)

Go 語言學習的必要性(對於運維工程師)

生成RSA證書:#

openssl方式生成#

  • 生成私鑰
openssl genrsa -out rsa_private_key.pem 1024
  • 生成公鑰
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem

Go程式碼方式生成

package main

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

//生成RSA私鑰和公鑰,儲存到檔案中
// bits 證書大小
func GenerateRSAKey(bits int) {
   //GenerateKey函式使用隨機資料生成器random生成一對具有指定字位數的RSA金鑰
   //Reader是一個全域性、共享的密碼用強隨機數生成器
   privateKey, err := rsa.GenerateKey(rand.Reader, bits)
   if err != nil {
      panic(err)
   }
   //儲存私鑰
   //通過x509標準將得到的ras私鑰序列化為ASN.1 的 DER編碼字串
   X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
   //使用pem格式對x509輸出的內容進行編碼
   //建立檔案儲存私鑰
   privateFile, err := os.Create("private.pem")
   if err != nil {
      panic(err)
   }
   defer privateFile.Close()
   //構建一個pem.Block結構體物件
   privateBlock := pem.Block{Type: "RSA Private Key", Bytes: X509PrivateKey}
   //將資料儲存到檔案
   pem.Encode(privateFile, &privateBlock)

   //儲存公鑰
   //獲取公鑰的資料
   publicKey := privateKey.PublicKey
   //X509對公鑰編碼
   X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
   if err != nil {
      panic(err)
   }
   //pem格式編碼
   //建立用於儲存公鑰的檔案
   publicFile, err := os.Create("public.pem")
   if err != nil {
      panic(err)
   }
   defer publicFile.Close()
   //建立一個pem.Block結構體物件
   publicBlock := pem.Block{Type: "RSA Public Key", Bytes: X509PublicKey}
   //儲存到檔案
   pem.Encode(publicFile, &publicBlock)
}

func main() {
   //生成金鑰對,儲存到檔案
   GenerateRSAKey(2048)
}

RSA資料加/解密

package main

import (
   "crypto/rand"
   "crypto/rsa"
   "crypto/x509"
   "encoding/pem"
   "fmt"
   "os"
)

//RSA加密
// plainText 要加密的資料
// path 公鑰匙檔案地址
func RSA_Encrypt(plainText []byte, path string) []byte {
   //開啟檔案
   file, err := os.Open(path)
   if err != nil {
      panic(err)
   }
   defer file.Close()
   //讀取檔案的內容
   info, _ := file.Stat()
   buf := make([]byte, info.Size())
   file.Read(buf)
   //pem解碼
   block, _ := pem.Decode(buf)
   //x509解碼

   publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
   if err != nil {
      panic(err)
   }
   //型別斷言
   publicKey := publicKeyInterface.(*rsa.PublicKey)
   //對明文進行加密
   cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)
   if err != nil {
      panic(err)
   }
   //返回密文
   return cipherText
}

//RSA解密
// cipherText 需要解密的byte資料
// path 私鑰檔案路徑
func RSA_Decrypt(cipherText []byte,path string) []byte{
   //開啟檔案
   file,err:=os.Open(path)
   if err!=nil{
      panic(err)
   }
   defer file.Close()
   //獲取檔案內容
   info, _ := file.Stat()
   buf:=make([]byte,info.Size())
   file.Read(buf)
   //pem解碼
   block, _ := pem.Decode(buf)
   //X509解碼
   privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
   if err!=nil{
      panic(err)
   }
   //對密文進行解密
   plainText,_:=rsa.DecryptPKCS1v15(rand.Reader,privateKey,cipherText)
   //返回明文
   return plainText
}

func main() {
   //加密
   data := []byte("hello world")
   encrypt := RSA_Encrypt(data, "public.pem")
   fmt.Println(string(encrypt))

   // 解密
   decrypt := RSA_Decrypt(encrypt, "private.pem")
   fmt.Println(string(decrypt))
}