24 lines
503 B
Go
24 lines
503 B
Go
package crypto
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/sha1"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
"encoding/pem"
|
|
)
|
|
|
|
func RsaEncrypt(pubKey []byte, text []byte) (string, error) {
|
|
block, _ := pem.Decode(pubKey)
|
|
key, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
k := key.(*rsa.PublicKey)
|
|
ciphertext, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, k, text, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
|
}
|