Ai
1 Star 0 Fork 0

piao/go-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
aes.go 4.34 KB
一键复制 编辑 原始数据 按行查看 历史
piao 提交于 2020-09-19 10:56 +08:00 . add secret.go
//refer https://gist.github.com/ltyyz/b306746041b48a8366d0f63507a4e7f3
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"fmt"
)
func main() {
src := "NIJOAHS000000000"
key := []byte("1234567890123456")
fmt.Println("SOURCE!:", []byte(src))
encrypted := AESEncrypt(src, key)
fmt.Println("ENCRYPT:", encrypted)
decrypted := AESDecrypt(encrypted, key)
fmt.Println("DECRYPT:", decrypted)
}
func AESEncrypt(src string, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
fmt.Println("key error1", err)
}
if src == "" {
fmt.Println("plain content empty")
}
ecb := cipher.NewCBCEncrypter(block, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
content := []byte(src)
content = PKCS5Padding(content, block.BlockSize())
crypted := make([]byte, len(content))
ecb.CryptBlocks(crypted, content)
return crypted
}
func AESDecrypt(crypt []byte, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
fmt.Println("key error1", err)
}
if len(crypt) == 0 {
fmt.Println("plain content empty")
}
ecb := cipher.NewCBCDecrypter(block, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
decrypted := make([]byte, len(crypt))
ecb.CryptBlocks(decrypted, crypt)
return PKCS5Trimming(decrypted)
}
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS5Trimming(encrypt []byte) []byte {
padding := encrypt[len(encrypt)-1]
return encrypt[:len(encrypt)-int(padding)]
}
/*
aesMsg := aesEncrypt(salt, msgBytes, cfg.AesKey)
func aesEncrypt(salt, content, aesKey []byte) []byte {
var block cipher.Block
var err error
if block, err = aes.NewCipher(aesKey); err != nil {
panic(err)
}
encrypt := cipher.NewCBCEncrypter(block, salt)
var source []byte = pkcs5Padding(content, 16)
var dst []byte = make([]byte, len(source))
encrypt.CryptBlocks(dst, source)
return dst
}
func aesDecrypt(salt, content, aesKey []byte) []byte {
var err error
var block cipher.Block
if block, err = aes.NewCipher(aesKey); err != nil {
panic(err)
}
encrypt := cipher.NewCBCDecrypter(block, salt)
var dst []byte = make([]byte, len(content))
encrypt.CryptBlocks(dst, content)
return pkcs5Unpadding(dst)
}
func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func pkcs5Unpadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func (c Config) rsaEncryptToBase64(merchantAESKey string) (string, error) {
origData := []byte(merchantAESKey)
//var pubKeyBytes = []byte("-----BEGIN PUBLIC KEY-----\n" + c.ipayPubKey + "\n-----END PUBLIC KEY-----")
fmt.Printf("pubKeyBytes %s\n", c.ipayPubKey)
var pubKeyBytes = []byte(c.ipayPubKey)
encBytes, err := rsaEncrypt(origData, pubKeyBytes)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(encBytes), nil
}
func rsaEncrypt(origData []byte, pubKey []byte) ([]byte, error) {
block, _ := pem.Decode(pubKey)
if block == nil {
return nil, fmt.Errorf("public key error")
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
pub := pubInterface.(*rsa.PublicKey)
return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}
func (c Config) rsaDecryptFromBase64(encryptKey string) ([]byte, error) {
origData, err := base64.StdEncoding.DecodeString(encryptKey)
if err != nil {
return nil, err
}
//var priKeyBytes = []byte("-----BEGIN PRIVATE KEY-----\n" + c.merchantPriKey + "\n-----END PRIVATE KEY-----")
fmt.Printf("priKeyBytes %s\n", c.merchantPriKey)
var priKeyBytes = []byte(c.merchantPriKey)
decBytes, err := rsaDecrypt(origData, priKeyBytes)
if err != nil {
return nil, err
}
return decBytes, nil
}
func rsaDecrypt(ciphertext []byte, privKey []byte) ([]byte, error) {
block, _ := pem.Decode(privKey)
if block == nil {
return nil, fmt.Errorf("private key error!")
}
prkI, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
priv, ok := prkI.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("priv key trans error")
}
return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}
*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/piao/go-utils.git
git@gitee.com:piao/go-utils.git
piao
go-utils
go-utils
master

搜索帮助