github.com/suiyunonghen/DxCommonLib@v0.5.3/cryptlib/aes.go (about)

     1  package cryptlib
     2  
     3  import (
     4  	"crypto/aes"
     5  	"crypto/cipher"
     6  	"encoding/base64"
     7  	"github.com/suiyunonghen/DxCommonLib"
     8  )
     9  
    10  type  AES struct {
    11  	fInitVector	[]byte	//CBC初始化向量
    12  }
    13  
    14  func NewAes(initvector []byte)*AES  {
    15  	return &AES{fInitVector:initvector}
    16  }
    17  
    18  func (aestool *AES)ResetVector(vector []byte)  {
    19  	aestool.fInitVector = vector
    20  }
    21  
    22  func (aestool *AES)Encrypt(key,value []byte)([]byte,error)  {
    23  	var realkey []byte
    24  	btlen := len(key)
    25  	if btlen < 16{
    26  		realkey = append(key,make([]byte,16-btlen)...)
    27  	}else{
    28  		realkey = key[:16]
    29  	}
    30  	block, err := aes.NewCipher(realkey[:16])
    31  	if err != nil {
    32  		return nil,err
    33  	}
    34  	blockSize := block.BlockSize()
    35  	value = PKCS5Padding(value, blockSize)
    36  	blockMode := cipher.NewCBCEncrypter(block, aestool.fInitVector[:blockSize])
    37  	crypted := make([]byte, len(value))
    38  	// 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
    39  	blockMode.CryptBlocks(crypted, value)
    40  	return crypted,nil
    41  }
    42  
    43  func (aestool *AES)EncryptBase64(key,value []byte)(string,error)  {
    44  	crypted,err := aestool.Encrypt(key,value)
    45  	if err != nil{
    46  		return "",err
    47  	}
    48  	return  base64.StdEncoding.EncodeToString(crypted),nil
    49  }
    50  
    51  func (aestool *AES)EncryptHex(key,value []byte)(string,error)  {
    52  	crypted,err := aestool.Encrypt(key,value)
    53  	if err != nil{
    54  		return "",err
    55  	}
    56  	return  DxCommonLib.Bin2Hex(crypted),nil
    57  }
    58  
    59  func (aestool *AES)DecryptBase64(value,key string) string {
    60  	var realkey []byte
    61  	bt := []byte(key)
    62  	btlen := len(bt)
    63  	if btlen < 16{
    64  		realkey = append(bt,make([]byte,16-btlen)...)
    65  	}else{
    66  		realkey = bt[:16]
    67  	}
    68  	block, err := aes.NewCipher(realkey[:16])
    69  	if err != nil {
    70  		return value
    71  	}
    72  	var vbyte []byte
    73  	vbyte,err = base64.StdEncoding.DecodeString(value)
    74  	if err != nil{
    75  		return value
    76  	}
    77  	blockSize := block.BlockSize()
    78  	blockMode := cipher.NewCBCDecrypter(block,aestool.fInitVector[:blockSize])
    79  	origData := make([]byte, len(vbyte))
    80  	blockMode.CryptBlocks(origData, vbyte)
    81  	origData = PKCS5UnPadding(origData)
    82  	if origData == nil{
    83  		return value
    84  	}
    85  	return string(origData)
    86  }
    87  
    88  func (aestool *AES)DecryptWithHex(value,key string) string {
    89  	var realkey []byte
    90  	bt := []byte(key)
    91  	btlen := len(bt)
    92  	if btlen < 16{
    93  		realkey = append(bt,make([]byte,16-btlen)...)
    94  	}else{
    95  		realkey = bt[:16]
    96  	}
    97  	block, err := aes.NewCipher(realkey[:16])
    98  	if err != nil {
    99  		return value
   100  	}
   101  	vbyte := DxCommonLib.Hex2Binary(value)
   102  	blockSize := block.BlockSize()
   103  	blockMode := cipher.NewCBCDecrypter(block,aestool.fInitVector[:blockSize])
   104  	origData := make([]byte, len(vbyte))
   105  	blockMode.CryptBlocks(origData, vbyte)
   106  	origData = PKCS5UnPadding(origData)
   107  	if origData == nil{
   108  		return value
   109  	}
   110  	return string(origData)
   111  }
   112  
   113  func (aestool *AES)Decrypt(value []byte,key string) []byte {
   114  	var realkey []byte
   115  	bt := []byte(key)
   116  	btlen := len(bt)
   117  	if btlen < 16{
   118  		realkey = append(bt,make([]byte,16-btlen)...)
   119  	}else{
   120  		realkey = bt[:16]
   121  	}
   122  	block, err := aes.NewCipher(realkey[:16])
   123  	if err != nil {
   124  		return value
   125  	}
   126  	blockSize := block.BlockSize()
   127  	blockMode := cipher.NewCBCDecrypter(block,aestool.fInitVector[:blockSize])
   128  	origData := make([]byte, len(value))
   129  	blockMode.CryptBlocks(origData, value)
   130  	origData = PKCS5UnPadding(origData)
   131  	if origData == nil{
   132  		return value
   133  	}
   134  	return origData
   135  }