39 lines
912 B
Go
39 lines
912 B
Go
|
package mycrypto
|
||
|
|
||
|
import (
|
||
|
"encoding/base64"
|
||
|
"service/library/logger"
|
||
|
|
||
|
"github.com/forgoer/openssl"
|
||
|
)
|
||
|
|
||
|
type AesEcbCrypto struct {
|
||
|
aesPriKey []byte
|
||
|
}
|
||
|
|
||
|
func NewAesEcbCryptoFromString(aesPrivateKeyStr string) (p *AesEcbCrypto, err error) {
|
||
|
p = &AesEcbCrypto{}
|
||
|
|
||
|
//读取私钥
|
||
|
p.aesPriKey, err = base64.StdEncoding.DecodeString(aesPrivateKeyStr)
|
||
|
if err != nil {
|
||
|
logger.Error("Decoding aes primary key failed, check your config, please!, err: %v", err)
|
||
|
return
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (p *AesEcbCrypto) Encrypt(msg []byte) (encryptedBytes []byte, err error) {
|
||
|
//ECB加密
|
||
|
encryptedBytes, err = openssl.AesECBEncrypt(msg, p.aesPriKey, openssl.PKCS5_PADDING)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (p *AesEcbCrypto) Decrypt(encryptedBytes []byte) (decryptedBytes []byte, err error) {
|
||
|
if len(encryptedBytes) == 0 {
|
||
|
return
|
||
|
}
|
||
|
decryptedBytes, err = openssl.AesECBDecrypt(encryptedBytes, p.aesPriKey, openssl.PKCS5_PADDING)
|
||
|
return
|
||
|
}
|