update encrypt
This commit is contained in:
parent
eef2c4353a
commit
95a2f52339
@ -17,28 +17,28 @@ func NewEncrypt(pubKey, privKey string) *Encrypt {
|
|||||||
return &Encrypt{pubKey: pubKey, privKey: privKey}
|
return &Encrypt{pubKey: pubKey, privKey: privKey}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encrypt) EncryptStr(str string) (string, error) {
|
func (e *Encrypt) EncryptStr(str string) ([]byte, error) {
|
||||||
block, _ := pem.Decode([]byte(e.pubKey))
|
block, _ := pem.Decode([]byte(e.pubKey))
|
||||||
if block == nil {
|
if block == nil {
|
||||||
return "", errors.New("failed to parse PEM block containing the public key")
|
return nil, errors.New("failed to parse PEM block containing the public key")
|
||||||
}
|
}
|
||||||
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
|
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
rsaPubKey, ok := pub.(*rsa.PublicKey)
|
rsaPubKey, ok := pub.(*rsa.PublicKey)
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", errors.New("failed to parse RSA public key")
|
return nil, errors.New("failed to parse RSA public key")
|
||||||
}
|
}
|
||||||
|
|
||||||
shifr, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPubKey, []byte(str))
|
shifr, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPubKey, []byte(str))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
return string(shifr), nil
|
return shifr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encrypt) DecryptStr(shifr string) (string, error) {
|
func (e *Encrypt) DecryptStr(shifr []byte) (string, error) {
|
||||||
block, _ := pem.Decode([]byte(e.privKey))
|
block, _ := pem.Decode([]byte(e.privKey))
|
||||||
if block == nil {
|
if block == nil {
|
||||||
return "", errors.New("failed to parse PEM block containing the private key")
|
return "", errors.New("failed to parse PEM block containing the private key")
|
||||||
@ -49,7 +49,7 @@ func (e *Encrypt) DecryptStr(shifr string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := rsa.DecryptPKCS1v15(rand.Reader, priv, []byte(shifr))
|
res, err := rsa.DecryptPKCS1v15(rand.Reader, priv, shifr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user