添加rsa非对称加密的助手方法

master
caiyuzheng 2020-08-02 22:56:47 +08:00
parent a39e9d7b84
commit 256ea13cc2
3 changed files with 98 additions and 8 deletions

View File

@ -8,4 +8,5 @@
"go.gotoSymbol.includeImports": true, "go.gotoSymbol.includeImports": true,
"go.inferGopath": true, "go.inferGopath": true,
"go.gotoSymbol.includeGoroot": true, "go.gotoSymbol.includeGoroot": true,
"C_Cpp.intelliSenseEngineFallback": "Disabled",
} }

15
test/crypto_test.go Normal file
View File

@ -0,0 +1,15 @@
package test
import (
"gobase/utils"
"log"
"testing"
)
func TestCrypto(t *testing.T) {
pub, pri := utils.GenRsaKey(1024)
enc, e := utils.RSAEncrypt([]byte{1, 2, 3}, pub)
log.Print(enc, e)
dec, e := utils.RSADecrypt(enc, pri)
log.Print(dec, e)
}

View File

@ -1,8 +1,13 @@
package utils package utils
import ( import (
"bytes"
"crypto/md5" "crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/hex" "encoding/hex"
"encoding/pem"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -41,19 +46,88 @@ func InArrary(arr interface{}, ele interface{}) int {
} }
return 1 return 1
} }
// 将程序变成守护进程。 // 将程序变成守护进程。
func Daemonize() { func Daemonize() {
if runtime.GOOS == "windows"{ if runtime.GOOS == "windows" {
// windows不支持进程守护,直接return // windows不支持进程守护,直接return
return return
} }
if runtime.GOOS == "linux"{ if runtime.GOOS == "linux" {
if os.Getppid()!= 1{ //判断当其是否是子进程当父进程return之后子进程会被 系统1 号进程接管 if os.Getppid() != 1 { //判断当其是否是子进程当父进程return之后子进程会被 系统1 号进程接管
filePath,_:=filepath.Abs(os.Args[0]) //将命令行参数中执行文件路径转换成可用路径 filePath, _ := filepath.Abs(os.Args[0]) //将命令行参数中执行文件路径转换成可用路径
os.StartProcess(filePath,os.Args[1:],&os.ProcAttr{Files:[]*os.File{os.Stdin,os.Stdout,os.Stderr}}) os.StartProcess(filePath, os.Args[1:], &os.ProcAttr{Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}})
os.Exit(0) os.Exit(0)
}else{ } else {
return return
} }
} }
} }
/*
* RSA
*/
func RSADecrypt(src []byte, buf []byte) ([]byte, error) {
block, _ := pem.Decode(buf)
if block == nil {
return nil, nil
}
// 解析出一个der编码的私钥
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
// 私钥解密
result, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, src)
if err != nil {
return nil, err
}
return result, nil
}
func RSAEncrypt(src []byte, buf []byte) ([]byte, error) {
block, _ := pem.Decode(buf)
if block == nil {
return nil, nil
}
// 解析一个der编码的公钥
publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return nil, err
}
// 公钥加密
result, _ := rsa.EncryptPKCS1v15(rand.Reader, publicKey, src)
return result, nil
}
func GenRsaKey(bits int) ([]byte, []byte) {
pribuf := bytes.NewBuffer([]byte{})
pubbuf := bytes.NewBuffer([]byte{})
// 生成私钥文件
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, nil
}
derPrivateStream := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: derPrivateStream,
}
err = pem.Encode(pribuf, block)
if err != nil {
return nil, nil
}
// 生成公钥文件
publicKey := &privateKey.PublicKey
derPkix := x509.MarshalPKCS1PublicKey(publicKey)
block = &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: derPkix,
}
err = pem.Encode(pubbuf, block)
if err != nil {
return nil, nil
}
return pubbuf.Bytes(), pribuf.Bytes()
}