代码拉取完成,页面将自动刷新
//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)
}
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。