--- /dev/null
+package main
+
+import (
+ "os"
+ "io/ioutil"
+ "errors"
+ "crypto/rand"
+ "crypto/sha256"
+ "crypto/rsa"
+ "crypto/x509"
+ "encoding/pem"
+)
+
+type Crypto struct {
+ pubKey *rsa.PublicKey
+ priKey *rsa.PrivateKey
+}
+
+func FileLoad(path string) (data []byte, err error) {
+ fileObject,e := os.OpenFile(path, os.O_RDONLY, 0644)
+ if e != nil {
+ err = e
+ return
+ }
+ defer fileObject.Close()
+ data,e = ioutil.ReadAll(fileObject)
+ if e != nil {
+ err = e
+ return
+ }
+ return
+}
+
+func (t *Crypto) GetPKIXPubKey(pubPEMData []byte) (err error) {
+ block, _ := pem.Decode(pubPEMData)
+ if block == nil || block.Type != "PUBLIC KEY" {
+ err = errors.New("failed to decode PEM block containing public key")
+ return
+ }
+
+ pubI, e := x509.ParsePKIXPublicKey(block.Bytes)
+ if e != nil {
+ err = e
+ return
+ }
+ t.pubKey = pubI.(*rsa.PublicKey)
+
+ return
+}
+
+func (t *Crypto) GetPKCS1PriKey(priPEMData []byte) (err error) {
+ block, _ := pem.Decode(priPEMData)
+ if block == nil || block.Type != "RSA PRIVATE KEY" {
+ err = errors.New("failed to decode PEM block containing private key")
+ return
+ }
+
+ t.priKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
+
+ return
+}
+
+func (t *Crypto) GetEncrypt(sourceByte []byte) (tragetByte []byte, err error) {
+ if t.pubKey == nil {
+ err = errors.New(`public key not load`)
+ return
+ }
+ return rsa.EncryptOAEP(sha256.New(), rand.Reader, t.pubKey, sourceByte, []byte{})
+}
+
+func (t *Crypto) GetDecrypt(sourceByte []byte) (tragetByte []byte, err error) {
+ if t.priKey == nil {
+ err = errors.New(`private key not load`)
+ return
+ }
+ return rsa.DecryptOAEP(sha256.New(), rand.Reader, t.priKey, sourceByte, []byte{})
+}
\ No newline at end of file
--- /dev/null
+package main
+
+import "testing"
+
+func Test(t *testing.T){
+ var k Crypto
+ {
+ d,_ := FileLoad(`public.pem`)
+ k.GetPKIXPubKey(d)
+ }
+ {
+ d,_ := FileLoad(`private.pem`)
+ k.GetPKCS1PriKey(d)
+ }
+ if srcs,e := k.GetEncrypt([]byte(`1we23`));e != nil {
+ t.Error(e)
+ } else if des,e := k.GetDecrypt(srcs);e != nil {
+ t.Error(e)
+ } else {
+ if s := string(des);s != `1we23` {t.Error(`not Match`,s)}
+ }
+}
\ No newline at end of file