update encrypt

This commit is contained in:
Pavel 2024-03-22 15:36:36 +03:00
parent eef2c4353a
commit 95a2f52339

@ -17,28 +17,28 @@ func NewEncrypt(pubKey, privKey string) *Encrypt {
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))
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)
if err != nil {
return "", err
return nil, err
}
rsaPubKey, ok := pub.(*rsa.PublicKey)
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))
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))
if block == nil {
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
}
res, err := rsa.DecryptPKCS1v15(rand.Reader, priv, []byte(shifr))
res, err := rsa.DecryptPKCS1v15(rand.Reader, priv, shifr)
if err != nil {
return "", err
}