Explorar o código

添加aes128加解密

huangrf %!s(int64=6) %!d(string=hai) anos
pai
achega
da6124cb37
Modificáronse 1 ficheiros con 100 adicións e 0 borrados
  1. 100 0
      utils/aes128.go

+ 100 - 0
utils/aes128.go

@@ -0,0 +1,100 @@
+package utils
+
+import (
+	"bytes"
+	"crypto/aes"
+	"crypto/cipher"
+	"encoding/base64"
+)
+
+const (
+	sKey        = "dde4b1f8a9e6b814"
+	ivParameter     = "d690b1f8a957b834"
+)
+
+//加密
+func PswEncrypt(src string)(string){
+	key := []byte(sKey)
+	iv := []byte(ivParameter)
+
+	result, err := Aes128Encrypt([]byte(src), key, iv)
+	if err != nil {
+		panic(err)
+	}
+	return  base64.RawStdEncoding.EncodeToString(result)
+}
+
+//解密
+func PswDecrypt(src string)(string) {
+
+	key := []byte(sKey)
+	iv := []byte(ivParameter)
+
+	var result []byte
+	var err error
+
+	result,err=base64.RawStdEncoding.DecodeString(src)
+	if err != nil {
+		panic(err)
+	}
+	origData, err := Aes128Decrypt(result, key, iv)
+	if err != nil {
+		panic(err)
+	}
+	return string(origData)
+
+}
+
+func Aes128Encrypt(origData, key []byte,IV []byte) ([]byte, error) {
+	if key == nil || len(key) != 16 {
+		return nil, nil
+	}
+	if IV != nil && len(IV) != 16 {
+		return nil, nil
+	}
+
+	block, err := aes.NewCipher(key)
+	if err != nil {
+		return nil, err
+	}
+	blockSize := block.BlockSize()
+	origData = PKCS5Padding(origData, blockSize)
+	blockMode := cipher.NewCBCEncrypter(block, IV[:blockSize])
+	crypted := make([]byte, len(origData))
+	// 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
+	blockMode.CryptBlocks(crypted, origData)
+	return crypted, nil
+}
+
+func Aes128Decrypt(crypted, key []byte,IV []byte) ([]byte, error) {
+	if key == nil || len(key) != 16 {
+		return nil, nil
+	}
+	if IV != nil && len(IV) != 16 {
+		return nil, nil
+	}
+
+	block, err := aes.NewCipher(key)
+	if err != nil {
+		return nil, err
+	}
+	blockSize := block.BlockSize()
+	blockMode := cipher.NewCBCDecrypter(block,IV[:blockSize])
+	origData := make([]byte, len(crypted))
+	blockMode.CryptBlocks(origData, crypted)
+	origData = PKCS5UnPadding(origData)
+	return origData, nil
+}
+
+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 次
+	unpadding := int(origData[length-1])
+	return origData[:(length - unpadding)]
+}