golang使用aes库实现加解密操作

 更新时间:2020年12月22日 16:15:39   作者:robertkun  
这篇文章主要介绍了golang使用aes库实现加解密操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

golang实现加密解密的库很多,这里使用的是aes库+base64库来实现.

使用时,需要指定一个私钥,来进行加解密,这里指定是:

var aeskey = []byte(“321423u9y8d2fwfl”)

上代码:

package main
import (
 "fmt"
 "crypto/cipher"
 "crypto/aes"
 "bytes"
 "encoding/base64"
)
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 := int(origData[length-1])
 return origData[:(length - unpadding)]
}
func AesEncrypt(origData, key []byte) ([]byte, error) {
 block, err := aes.NewCipher(key)
 if err != nil {
  return nil, err
 }
 blockSize := block.BlockSize()
 origData = PKCS5Padding(origData, blockSize)
 blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
 crypted := make([]byte, len(origData))
 blockMode.CryptBlocks(crypted, origData)
 return crypted, nil
}
func AesDecrypt(crypted, key []byte) ([]byte, error) {
 block, err := aes.NewCipher(key)
 if err != nil {
  return nil, err
 }
 blockSize := block.BlockSize()
 blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
 origData := make([]byte, len(crypted))
 blockMode.CryptBlocks(origData, crypted)
 origData = PKCS5UnPadding(origData)
 return origData, nil
}
func main() {
 var aeskey = []byte("321423u9y8d2fwfl")
 pass := []byte("vdncloud123456")
 xpass, err := AesEncrypt(pass, aeskey)
 if err != nil {
  fmt.Println(err)
  return
 }
 pass64 := base64.StdEncoding.EncodeToString(xpass)
 fmt.Printf("加密后:%v\n",pass64)
 bytesPass, err := base64.StdEncoding.DecodeString(pass64)
 if err != nil {
  fmt.Println(err)
  return
 }
 tpass, err := AesDecrypt(bytesPass, aeskey)
 if err != nil {
  fmt.Println(err)
  return
 }
 fmt.Printf("解密后:%s\n", tpass)
}

输出:

加密后:rLyZug0MCEF2TBcJdhMyjg==

解密后:vdncloud123456

补充:Golang AES CBC 加密

我就废话不多说了,大家还是直接看代码吧~

package main
import (
 "bytes"
 "crypto/aes"
 "crypto/cipher"
 "encoding/base64"
 "fmt"
)
const (
 key = "2018201820182018"
 iv = "1234567887654321"
)
func main() {
 str := "我勒个去"
 es, _ := AesEncrypt(str, []byte(key))
 fmt.Println(es)
 ds, _ := AesDecrypt(es, []byte(key))
 fmt.Println(string(ds))
}
func AesEncrypt(encodeStr string, key []byte) (string, error) {
 encodeBytes := []byte(encodeStr)
 //根据key 生成密文
 block, err := aes.NewCipher(key)
 if err != nil {
  return "", err
 }
 blockSize := block.BlockSize()
 encodeBytes = PKCS5Padding(encodeBytes, blockSize)
 blockMode := cipher.NewCBCEncrypter(block, []byte(iv))
 crypted := make([]byte, len(encodeBytes))
 blockMode.CryptBlocks(crypted, encodeBytes)
 return base64.StdEncoding.EncodeToString(crypted), 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 AesDecrypt(decodeStr string, key []byte) ([]byte, error) {
 //先解密base64
 decodeBytes, err := base64.StdEncoding.DecodeString(decodeStr)
 if err != nil {
  return nil, err
 }
 block, err := aes.NewCipher(key)
 if err != nil {
  return nil, err
 }
 blockMode := cipher.NewCBCDecrypter(block, []byte(iv))
 origData := make([]byte, len(decodeBytes))
 blockMode.CryptBlocks(origData, decodeBytes)
 origData = PKCS5UnPadding(origData)
 return origData, nil
}
func PKCS5UnPadding(origData []byte) []byte {
 length := len(origData)
 unpadding := int(origData[length-1])
 return origData[:(length - unpadding)]
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

您可能感兴趣的文章:

相关文章

  • Go gorilla securecookie库的安装使用详解

    Go gorilla securecookie库的安装使用详解

    这篇文章主要介绍了Go gorilla securecookie库的安装使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • golang正则之命名分组方式

    golang正则之命名分组方式

    这篇文章主要介绍了golang正则之命名分组方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • 如何在golang中检查文件是否存在

    如何在golang中检查文件是否存在

    如果你用的是 Python,可通过 os.path.exists 这样的标准库函数实现,遗憾的是,Go 标准库没有提供这样直接的函数,所以下面我们就来了解下如何使用GO语言能实现检查文件是否存在呢
    2024-02-02
  • golang将多路复异步io转成阻塞io的方法详解

    golang将多路复异步io转成阻塞io的方法详解

    常见的IO模型有阻塞、非阻塞、IO多路复用,异,下面这篇文章主要给大家介绍了关于golang将多路复异步io转成阻塞io的方法,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2017-09-09
  • go语言中gorm时间格式化

    go语言中gorm时间格式化

    本文主要介绍了go语言中gorm时间格式化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • 一文带你入门Go语言中定时任务库Cron的使用

    一文带你入门Go语言中定时任务库Cron的使用

    在平时的开发需求中,我们经常会有一些重复执行的操作需要触发执行,说白了就是定时任务。这篇文章主要给大家介绍一下如何在go项目中实现一个crontab功能,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助
    2022-08-08
  • 一文带你理解Golang中的Time结构

    一文带你理解Golang中的Time结构

    根据golang的time包的文档可以知道,golang的time结构中存储了两种时钟,一种是Wall Clocks,一种是Monotonic Clocks,下面我们就来简单了解一下这两种结构吧
    2023-09-09
  • 详解Go开发Struct转换成map两种方式比较

    详解Go开发Struct转换成map两种方式比较

    本篇文章主要介绍了详解Go开发Struct转换成map两种方式比较,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • 用Go获取短信验证码的示例代码

    用Go获取短信验证码的示例代码

    要用Go获取短信验证码,通常需要连接到一个短信服务提供商的API,并通过该API发送请求来获取验证码,由于不同的短信服务提供商可能具有不同的API和授权方式,我将以一个简单的示例介绍如何使用Go语言来获取短信验证码,需要的朋友可以参考下
    2023-07-07
  • Goland编辑器设置选择范围背景色的操作

    Goland编辑器设置选择范围背景色的操作

    这篇文章主要介绍了Goland编辑器设置选择范围背景色的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12

最新评论