From c9129de1e2e3a93a4c742ebda5984ad35d22a039 Mon Sep 17 00:00:00 2001 From: learn Date: Fri, 19 May 2023 11:24:21 +0800 Subject: [PATCH 1/2] =?UTF-8?q?GenerateRSAKey=20=E7=94=9F=E6=88=90RSA?= =?UTF-8?q?=E7=A7=81=E9=92=A5=E5=92=8C=E5=85=AC=E9=92=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 65122c59cd33c1c5eca30b43bc9bbfcf3ac72bb1) --- rsa_key.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 rsa_key.go diff --git a/rsa_key.go b/rsa_key.go new file mode 100644 index 0000000..5e086d7 --- /dev/null +++ b/rsa_key.go @@ -0,0 +1,59 @@ +package gorsa + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/base64" + "encoding/pem" + "fmt" +) + +// GenerateRSAKey 生成RSA私钥和公钥 +// bits 证书大小 +func GenerateRSAKey(bits int) (resp struct { + pubStr string //公钥字符串 + priStr string //私钥字符串 + pubKey *rsa.PublicKey //公钥 + priKey *rsa.PrivateKey //私钥 +}, err error) { + + // -------------------------- 设置私钥 -------------------------- + // GenerateKey 函数使用随机数据生成器,random生成一对具有指定字位数的RSA密钥 + // Reader 是一个全局、共享的密码用强随机数生成器 + privateKey, err := rsa.GenerateKey(rand.Reader, bits) + if err != nil { + return + } + //保存私钥 + //通过x509标准将得到的ras私钥序列化为ASN.1 的 DER编码字符串 + X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey) + //使用pem格式对x509输出的内容进行编码 + //构建一个pem.Block结构体对象 + privateBlock := pem.Block{Type: "RSA Private Key", Bytes: X509PrivateKey} + // 保存到内存 + privateKeyPem := pem.EncodeToMemory(&privateBlock) + privateKeyStr := base64.StdEncoding.EncodeToString(privateKeyPem) + + resp.priStr = fmt.Sprintf("-----BEGIN Private key-----\n%v\n-----END Private key-----\n", privateKeyStr) + resp.priKey = privateKey + + // -------------------------- 设置公钥 -------------------------- + //获取公钥的数据 + publicKey := privateKey.PublicKey + //X509对公钥编码 + X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey) + if err != nil { + return + } + //pem格式编码 + //创建一个pem.Block结构体对象 + publicBlock := pem.Block{Type: "RSA Public Key", Bytes: X509PublicKey} + //保存到内存 + publicKeyPem := pem.EncodeToMemory(&publicBlock) + publicKeyStr := base64.StdEncoding.EncodeToString(publicKeyPem) + + resp.pubStr = fmt.Sprintf("-----BEGIN Public key-----\n%v\n-----END Public key-----\n", publicKeyStr) + resp.pubKey = &publicKey + return +} From 29ef32cad84f4c041aec880db02c3fcc703de1d5 Mon Sep 17 00:00:00 2001 From: learn Date: Fri, 19 May 2023 16:15:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=94=9F=E6=88=90RSAKey=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=85=AC=E5=BC=80=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rsa_key.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/rsa_key.go b/rsa_key.go index 5e086d7..004a5bf 100644 --- a/rsa_key.go +++ b/rsa_key.go @@ -9,14 +9,16 @@ import ( "fmt" ) +type RSAKey struct { + PubStr string //公钥字符串 + PriStr string //私钥字符串 + PubKey *rsa.PublicKey //公钥 + PriKey *rsa.PrivateKey //私钥 +} + // GenerateRSAKey 生成RSA私钥和公钥 // bits 证书大小 -func GenerateRSAKey(bits int) (resp struct { - pubStr string //公钥字符串 - priStr string //私钥字符串 - pubKey *rsa.PublicKey //公钥 - priKey *rsa.PrivateKey //私钥 -}, err error) { +func GenerateRSAKey(bits int) (resp RSAKey, err error) { // -------------------------- 设置私钥 -------------------------- // GenerateKey 函数使用随机数据生成器,random生成一对具有指定字位数的RSA密钥 @@ -34,9 +36,9 @@ func GenerateRSAKey(bits int) (resp struct { // 保存到内存 privateKeyPem := pem.EncodeToMemory(&privateBlock) privateKeyStr := base64.StdEncoding.EncodeToString(privateKeyPem) - - resp.priStr = fmt.Sprintf("-----BEGIN Private key-----\n%v\n-----END Private key-----\n", privateKeyStr) - resp.priKey = privateKey + // 设置返回值:私钥 + resp.PriStr = fmt.Sprintf("-----BEGIN Private key-----\n%v\n-----END Private key-----\n", privateKeyStr) + resp.PriKey = privateKey // -------------------------- 设置公钥 -------------------------- //获取公钥的数据 @@ -52,8 +54,8 @@ func GenerateRSAKey(bits int) (resp struct { //保存到内存 publicKeyPem := pem.EncodeToMemory(&publicBlock) publicKeyStr := base64.StdEncoding.EncodeToString(publicKeyPem) - - resp.pubStr = fmt.Sprintf("-----BEGIN Public key-----\n%v\n-----END Public key-----\n", publicKeyStr) - resp.pubKey = &publicKey + // 设置返回值:公钥 + resp.PubStr = fmt.Sprintf("-----BEGIN Public key-----\n%v\n-----END Public key-----\n", publicKeyStr) + resp.PubKey = &publicKey return }