golang使用aes库实现加解密操作
来源:脚本之家
2023-01-07 12:04:27
0浏览
收藏
知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《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)]
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持golang学习网。如有错误或未考虑完全的地方,望不吝赐教。
好了,本文到此结束,带大家了解了《golang使用aes库实现加解密操作》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
版本声明
本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除
解决Go语言数据库中null值的问题
- 上一篇
- 解决Go语言数据库中null值的问题
- 下一篇
- 使用go操作redis的有序集合(zset)
查看更多
最新文章
-
- Golang · Go教程 | 5小时前 | goroutine · pprof · Go教程 · 运行时 · Go pprof 性能诊断 goroutine profile pprof.Lookup
- Go pprof.Lookup 怎样读取 goroutine profile 文本
- 465浏览 收藏
-
- Golang · Go教程 | 5小时前 | 标准库 · HTTP · Go教程 · Go 请求头 textproto CanonicalMIMEHeaderKey MIMEHeader
- Go textproto.CanonicalMIMEHeaderKey 怎样统一请求头键名
- 283浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Go image.NewRGBA 怎样创建可修改的 RGBA 画布
- 289浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Go compress/gzip Reader 怎样复用并正确关闭
- 454浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Go archive/zip FileHeader.ExternalAttrs 读取条目权限属性
- 460浏览 收藏
-
- Golang · Go教程 | 5小时前 | 标准库 · Go教程 · Go 邮箱地址 net/mail ParseAddressList 收件人列表
- Go net/mail ParseAddressList 怎样解析多个收件人
- 465浏览 收藏
-
- Golang · Go教程 | 5小时前 | Go教程 · Go crypto/rand 随机令牌 随机字节 密钥材料
- Go crypto/rand 怎样生成固定字节数随机值
- 443浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Go net.Dialer 怎样配置连接建立超时
- 151浏览 收藏
-
- Golang · Go教程 | 6小时前 |
- Go tls.Config 怎样限制最低 TLS 版本
- 197浏览 收藏
-
- Golang · Go教程 | 6小时前 | go · Plugin · Go plugin 版本兼容 buildmode=plugin
- Go plugin 包为什么不适合跨版本加载
- 426浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 603次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 643次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 673次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 639次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 460次使用
查看更多
相关文章
-
- Go语言使用对称加密的示例详解
- 2023-02-16 322浏览
-
- go GCM gin中间件的加密解密文件流处理
- 2022-12-31 351浏览
-
- 通过Golang编写一个AES加密解密工具
- 2022-12-30 373浏览
-
- Golang实现AES加密和解密的示例代码
- 2023-01-07 307浏览
-
- Go语言实现AES加密并编写一个命令行应用程序
- 2023-02-25 453浏览

