package aes import ( "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" ) var ContactInfoKey = "www.ichub.com@v2" func AesEncrypt(orig string, key string) string { defer func() { if err := recover(); err != nil { // logger.Errorf("[AesEncrypt]加密失败,orig:%s,key:%s,err:%s", orig, key, err) } }() // 转成字节数组 origData := []byte(orig) k := []byte(key) // 分组秘钥 block, _ := aes.NewCipher(k) // 获取秘钥块的长度 blockSize := block.BlockSize() // 补全码 origData = PKCS7Padding(origData, blockSize) // 加密模式 blockMode := cipher.NewCBCEncrypter(block, k[:blockSize]) // 创建数组 cryted := make([]byte, len(origData)) // 加密 blockMode.CryptBlocks(cryted, origData) return base64.StdEncoding.EncodeToString(cryted) } func AesDecrypt(cryted string, key string) string { defer func() { if err := recover(); err != nil { // logger.Errorf("[AesDecrypt]解密失败,cryted:%s,key:%s,err:%s", cryted, key, err) } }() // 转成字节数组 crytedByte, _ := base64.StdEncoding.DecodeString(cryted) k := []byte(key) // 分组秘钥 block, _ := aes.NewCipher(k) // 获取秘钥块的长度 blockSize := block.BlockSize() // 加密模式 blockMode := cipher.NewCBCDecrypter(block, k[:blockSize]) // 创建数组 orig := make([]byte, len(crytedByte)) // 解密 blockMode.CryptBlocks(orig, crytedByte) // 去补全码 orig = PKCS7UnPadding(orig) return string(orig) } // 补码 func PKCS7Padding(ciphertext []byte, blocksize int) []byte { padding := blocksize - len(ciphertext)%blocksize padtext := bytes.Repeat([]byte{byte(padding)}, padding) return append(ciphertext, padtext...) } // 去码 func PKCS7UnPadding(origData []byte) []byte { length := len(origData) unpadding := int(origData[length-1]) return origData[:(length - unpadding)] }
package encrypt import ( "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/base64" "encoding/pem" "errors" ) /* RSA 加解密 */ type RasEncrypt struct { privateKey *rsa.PrivateKey privateKeyStr string publicKeyStr string } func NewRasEncrypt() *RasEncrypt { return &RasEncrypt{} } func NewRasEncryptDefault(bits int) (*RasEncrypt, error) { rasEncrypt := &RasEncrypt{} _, _, er := rasEncrypt.GenKey(bits) return rasEncrypt, er } func NewRasEncryptByPrivateKey(privateKey string) (*RasEncrypt, error) { if len(privateKey) == 0 { return nil, errors.New("private key is empty") } block, _ := pem.Decode([]byte(privateKey)) if block == nil { return nil, errors.New("private key error") } parsePKCS1PrivateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) if err != nil { return nil, err } rasEncrypt := &RasEncrypt{privateKey: parsePKCS1PrivateKey} return rasEncrypt, rasEncrypt.init(parsePKCS1PrivateKey) } func (s *RasEncrypt) init(privateKey *rsa.PrivateKey) error { x509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey) privateBlock := &pem.Block{Type: "RSA Private Key", Bytes: x509PrivateKey} x509PublicKey, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey) if err != nil { return err } publicBlock := &pem.Block{Type: "RSA Public Key", Bytes: x509PublicKey} s.privateKeyStr = string(pem.EncodeToMemory(privateBlock)) s.publicKeyStr = string(pem.EncodeToMemory(publicBlock)) return nil } func (s *RasEncrypt) GetPublicKey() string { return s.publicKeyStr } func (s *RasEncrypt) GetPrivateKey() string { return s.privateKeyStr } // GenKey 生成密钥对 func (s *RasEncrypt) GenKey(bits int) (publicKey, privateKey string, err error) { tmpPrivateKey, err := rsa.GenerateKey(rand.Reader, bits) if err != nil { return publicKey, privateKey, err } s.privateKey = tmpPrivateKey return "", "", s.init(tmpPrivateKey) } // Encrypt 加密 func (s *RasEncrypt) Encrypt(plainText []byte) (cipherText []byte, err error) { // 3.使用公钥加密 cipherTextBt, err := rsa.EncryptPKCS1v15(rand.Reader, &s.privateKey.PublicKey, plainText) if err != nil { return cipherText, err } cipherText = cipherTextBt return } // Decrypt 解密 func (s *RasEncrypt) Decrypt(cipherText string) (plainText []byte, err error) { cipherTextBt, err := base64.StdEncoding.DecodeString(cipherText) if err != nil { return plainText, err } // 3.解密数据 plainTextBt, err := rsa.DecryptPKCS1v15(rand.Reader, s.privateKey, cipherTextBt) if err != nil { return plainText, err } return plainTextBt, nil }