Go语言实现AES加密算法(CBC模式)
发布时间:2021-05-29 编辑:jiaochengji.com
教程集为您提供Go语言实现AES加密算法(CBC模式)等资源,欢迎您收藏本站,我们将为您提供最新的Go语言实现AES加密算法(CBC模式)资源
Go语言实现AES加密算法(CBC模式)
AES是目前比较流行的对称加密算法,是一种分组密码算法,AES的分组长度为128比特(16字节),而密钥长度可以是128比特、192比特或256比特。
CBC模式(密码分组链接模式)是常用的一种分组密码的模式。(点击查看分组密码及CBC模式概述)
实现代码如下:
import (
"crypto/aes"
"crypto/cipher"
"fmt"
"bytes"
)
//对明文进行填充
func Padding(plainText []byte,blockSize int) []byte{
//计算要填充的长度
n:= blockSize-len(plainText)%blockSize
//对原来的明文填充n个n
temp:=bytes.Repeat([]byte{byte(n)},n)
plainText=append(plainText,temp...)
return plainText
}
//对密文删除填充
func UnPadding(cipherText []byte) []byte{
//取出密文最后一个字节end
end:=cipherText[len(cipherText)-1]
//删除填充
cipherText=cipherText[:len(cipherText)-int(end)]
return cipherText
}
//AEC加密(CBC模式)
func AES_CBC_Encrypt(plainText []byte,key []byte) []byte{
//指定加密算法,返回一个AES算法的Block接口对象
block,err:=aes.NewCipher(key)
if err!=nil{
panic(err)
}
//进行填充
plainText=Padding(plainText,block.BlockSize())
//指定初始向量vi,长度和block的块尺寸一致
iv:=[]byte("12345678abcdefgh")
//指定分组模式,返回一个BlockMode接口对象
blockMode:=cipher.NewCBCEncrypter(block,iv)
//加密连续数据库
cipherText:=make([]byte,len(plainText))
blockMode.CryptBlocks(cipherText,plainText)
//返回密文
return cipherText
}
//AEC解密(CBC模式)
func AES_CBC_Decrypt(cipherText []byte,key []byte) []byte{
//指定解密算法,返回一个AES算法的Block接口对象
block,err:=aes.NewCipher(key)
if err!=nil{
panic(err)
}
//指定初始化向量IV,和加密的一致
iv:=[]byte("12345678abcdefgh")
//指定分组模式,返回一个BlockMode接口对象
blockMode:=cipher.NewCBCDecrypter(block,iv)
//解密
plainText:=make([]byte,len(cipherText))
blockMode.CryptBlocks(plainText,cipherText)
//删除填充
plainText=UnPadding(plainText)
return plainText
}
测试代码如下:
func main(){
message:=[]byte("Hello!My name is X.")
//指定密钥h
key:=[]byte("abcdefgh98765432")
//加密
cipherText:=AES_CBC_Encrypt(message,key)
fmt.Println("加密后为:",string(cipherText))
//解密
plainText:=AES_CBC_Decrypt(cipherText,key)
fmt.Println("解密后为:",string(plainText))
}
测试结果如下:
您可能感兴趣的文章:
golang基础学习-AES加密
Go 加密解密算法总结
go语言对称加密算法
Go语言实现AES加密算法(CBC模式)
密码学之对称加密
golang实现aes加解密,对称加密
PHP 数据加密的方法
php aes加密类代码分享
PHP如何使用AES加密和解密
Python:常见的加密方式
上一篇:Golang里面slice初始化的坑
下一篇:Mysql 修改密码加密方式
[关闭]