Go語言 如何實現RSA加密解密
RSA是一種非對稱加密算法,它的名字是由它的三位開發(fā)者,即RonRivest、AdiShamir和LeonardAdleman 的姓氏的首字母組成的(Rivest-Shamir-Adleman ),可用于數據加密和數字簽名。
用于數據加密時,消息發(fā)送方利用對方的公鑰進行加密,消息接受方收到密文時使用自己的私鑰進行解密。
實現代碼如下:
import (
"crypto/rsa"
"crypto/rand"
"crypto/x509"
"os"
"encoding/pem"
"fmt"
)
//生成RSA私鑰和公鑰,保存到文件中
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輸出的內容進行編碼
//創(chuàng)建文件保存私鑰
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格式編碼
//創(chuàng)建用于保存公鑰的文件
publicFile, err := os.Create("public.pem")
if err!=nil{
panic(err)
}
defer publicFile.Close()
//創(chuàng)建一個pem.Block結構體對象
publicBlock:= pem.Block{Type: "RSA Public Key",Bytes:X509PublicKey}
//保存到文件
pem.Encode(publicFile,&publicBlock)
}
//RSA加密
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解密
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(){
//生成密鑰對,保存到文件
GenerateRSAKey(2048)
message:=[]byte("hello world")
//加密
cipherText:=RSA_Encrypt(message,"public.pem")
fmt.Println("加密后為:",string(cipherText))
//解密
plainText := RSA_Decrypt(cipherText, "private.pem")
fmt.Println("解密后為:",string(plainText))
}
測試結果如下:

補充:golang中關于RSA加密、解密、簽名、驗簽的總結
golang中關于RSA的加密、解密、簽名、驗簽的使用主要在于使用x509及rsa package下相關的方法。
gocrypt是本人對一般常用的加/解密、簽名/驗簽、hash的封裝庫,歡迎大家使用。
以下總結相關的各種變化類型:
1.秘鑰、加密/簽名字符串加密的格式
目前主要見到有hex及base64
(1)hex
針對hex的加解密
hex.DecodeString(s string)//解密 hex.EncodeToString(src []byte) string//加密
(2)base64
base64.StdEncoding.DecodeString(s string) ([]byte, error)//解密 base64.StdEncoding.EncodeToString(src []byte) string//加密
2.私鑰的格式
解析私鑰的方式如下:
(1)PKCS1
x509.ParsePKCS1PrivateKey(der []byte) (key interface{}, err error)
(2)PKCS8
x509.ParsePKCS8PrivateKey(der []byte) (key interface{}, err error)
3.采用的數字簽名算法SHA
以下為RSA sign的不同說明:
(1)SHA1
hash := sha1.New()
hash.Write([]byte(originalData))
encryptedData, err := rsa.SignPKCS1v15(rand.Reader, prvKey, crypto.SHA1, hash.Sum(nil))
(2)SHA256
hash := sha256.New()
hash.Write([]byte(originalData))
encryptedData, err := rsa.SignPKCS1v15(rand.Reader, prvKey, crypto.SHA256, hash.Sum(nil))
4.RSA使用類型
主要有加密/解密、簽名/驗簽4種方式,且加密/解密與簽名/驗簽均是一個相反的過程。兩對是根據對公鑰及私鑰的使用劃分的。
加密/解密是采用公鑰加密,私鑰解密。
簽名/驗簽是采用私鑰簽名,公鑰驗簽。
(1)加密
rsa.EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) ([]byte, error)
(2)解密
rsa.DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) ([]byte, error)
(3)簽名
rsa.SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error)
(4)驗簽
rsa.VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error
5.具體的使用示例
(1)加密:采用sha1算法加密后轉base64格式
func RsaEncryptWithSha1Base64(originalData,publicKey string)(string,error){
key, _ := base64.StdEncoding.DecodeString(publicKey)
pubKey, _ := x509.ParsePKIXPublicKey(key)
encryptedData,err:=rsa.EncryptPKCS1v15(rand.Reader, pubKey.(*rsa.PublicKey), []byte(originalData))
return base64.StdEncoding.EncodeToString(encryptedData),err
}
(2)解密:對采用sha1算法加密后轉base64格式的數據進行解密(私鑰PKCS1格式)
func RsaDecryptWithSha1Base64(encryptedData,privateKey string)(string,error){
encryptedDecodeBytes,err:=base64.StdEncoding.DecodeString(encryptedData)
if err!=nil {
return "",err
}
key,_:=base64.StdEncoding.DecodeString(privateKey)
prvKey,_:=x509.ParsePKCS1PrivateKey(key)
originalData,err:=rsa.DecryptPKCS1v15(rand.Reader,prvKey,encryptedDecodeBytes)
return string(originalData),err
}
(3)簽名:采用sha1算法進行簽名并輸出為hex格式(私鑰PKCS8格式)
func RsaSignWithSha1Hex(data string, prvKey string) (string, error) {
keyByts, err := hex.DecodeString(prvKey)
if err != nil {
fmt.Println(err)
return "", err
}
privateKey, err := x509.ParsePKCS8PrivateKey(keyByts)
if err != nil {
fmt.Println("ParsePKCS8PrivateKey err", err)
return "", err
}
h := sha1.New()
h.Write([]byte([]byte(data)))
hash := h.Sum(nil)
signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey.(*rsa.PrivateKey), crypto.SHA1, hash[:])
if err != nil {
fmt.Printf("Error from signing: %s\n", err)
return "", err
}
out := hex.EncodeToString(signature)
return out, nil
}
(4)驗簽:對采用sha1算法進行簽名后轉base64格式的數據進行驗簽
func RsaVerySignWithSha1Base64(originalData, signData, pubKey string) error{
sign, err := base64.StdEncoding.DecodeString(signData)
if err != nil {
return err
}
public, _ := base64.StdEncoding.DecodeString(pubKey)
pub, err := x509.ParsePKIXPublicKey(public)
if err != nil {
return err
}
hash := sha1.New()
hash.Write([]byte(originalData))
return rsa.VerifyPKCS1v15(pub.(*rsa.PublicKey), crypto.SHA1, hash.Sum(nil), sign)
}
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
Golang因Channel未關閉導致內存泄漏的解決方案詳解
這篇文章主要為大家詳細介紹了當Golang因Channel未關閉導致內存泄漏時蓋如何解決,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-07-07

