...
|
...
|
@@ -40,7 +40,7 @@ func checkKeySize(key []byte) error { |
|
|
}
|
|
|
|
|
|
// AES encrypt pkcs7padding CBC, key for choose algorithm
|
|
|
func AES_CBC_PKCS7_EncryptByte(plantText, key []byte) ([]byte, error) {
|
|
|
func AesEncryptCBC(plantText, key []byte) ([]byte, error) {
|
|
|
err := checkKeySize(key)
|
|
|
if err != nil {
|
|
|
return nil, err
|
...
|
...
|
@@ -49,8 +49,8 @@ func AES_CBC_PKCS7_EncryptByte(plantText, key []byte) ([]byte, error) { |
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
plantText = pKCS7Padding(plantText, block.BlockSize())
|
|
|
//偏转向量iv长度等于密钥key的长度
|
|
|
plantText = PKCS7Padding(plantText, block.BlockSize())
|
|
|
//偏转向量iv长度等于密钥key块大小
|
|
|
iv := key[:block.BlockSize()]
|
|
|
blockModel := cipher.NewCBCEncrypter(block, iv)
|
|
|
|
...
|
...
|
@@ -60,7 +60,7 @@ func AES_CBC_PKCS7_EncryptByte(plantText, key []byte) ([]byte, error) { |
|
|
return cipherText, nil
|
|
|
}
|
|
|
|
|
|
func AES_CBC_PKCS7_DecryptByte(cipherText, key []byte) ([]byte, error) {
|
|
|
func AesDecryptCBC(cipherText, key []byte) ([]byte, error) {
|
|
|
err := checkKeySize(key)
|
|
|
if err != nil {
|
|
|
return nil, err
|
...
|
...
|
@@ -70,37 +70,24 @@ func AES_CBC_PKCS7_DecryptByte(cipherText, key []byte) ([]byte, error) { |
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
//偏转向量iv长度等于密钥key的长度
|
|
|
//偏转向量iv长度等于密钥key块大小
|
|
|
iv := key[:block.BlockSize()]
|
|
|
blockModel := cipher.NewCBCDecrypter(block, iv)
|
|
|
plantText := make([]byte, len(cipherText))
|
|
|
blockModel.CryptBlocks(plantText, cipherText)
|
|
|
plantText = pKCS7UnPadding(plantText, block.BlockSize())
|
|
|
plantText = PKCS7UnPadding(plantText, block.BlockSize())
|
|
|
return plantText, nil
|
|
|
}
|
|
|
|
|
|
//AES Decrypt pkcs7padding CBC, key for choose algorithm
|
|
|
func pKCS7UnPadding(plantText []byte, blockSize int) []byte {
|
|
|
func PKCS7UnPadding(plantText []byte, blockSize int) []byte {
|
|
|
length := len(plantText)
|
|
|
unPadding := int(plantText[length-1])
|
|
|
return plantText[:(length - unPadding)]
|
|
|
}
|
|
|
|
|
|
func pKCS7Padding(cipherText []byte, blockSize int) []byte {
|
|
|
func PKCS7Padding(cipherText []byte, blockSize int) []byte {
|
|
|
padding := blockSize - len(cipherText)%blockSize
|
|
|
padText := bytes.Repeat([]byte{byte(padding)}, padding)
|
|
|
return append(cipherText, padText...)
|
|
|
} |
|
|
|
|
|
// 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)]
|
|
|
// } |
...
|
...
|
|