侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

在go语言及Node.js中进行AES加密解密及go跟node.js传输中互相加解密

Go  /  管理员 发布于 1年前   510

本文就分别介绍使用 Node.js和go进行AES加密解密的方法, AES 有很多不同的算法,如aes192,aes-128-ecb,aes-256-cbc等,根据不同的密钥长度会使用不同的算法。 

加密后的结果有两种表示方法:hex和base64,我们这里使用 hex.


golang

使用 golang 实现 aes 加密,我使用标准库的方法实现,我使用的 CBC 模式。

加密

func AesEncrypt(encryptStr string, key []byte, iv string) (string, error) {
   encryptBytes := []byte(encryptStr)
   block, err := aes.NewCipher(key)
   if err != nil {
       return "", err
   }
   blockSize := block.BlockSize()
   encryptBytes = pkcs5Padding(encryptBytes, blockSize)
   blockMode := cipher.NewCBCEncrypter(block, []byte(iv))
   encrypted := make([]byte, len(encryptBytes))
   blockMode.CryptBlocks(encrypted, encryptBytes)
   return hex.EncodeToString(encrypted), nil
}

解密

func AesDecrypt(decryptStr string, key []byte, iv string) (string, error) {
   decryptBytes, err := hex.DecodeString(decryptStr)
   if err != nil {
       return "", err
   }
   block, err := aes.NewCipher(key)
   if err != nil {
       return "", err
   }
   blockMode := cipher.NewCBCDecrypter(block, []byte(iv))
   decrypted := make([]byte, len(decryptBytes))
   blockMode.CryptBlocks(decrypted, decryptBytes)
   decrypted = pkcs5UnPadding(decrypted)
   return string(decrypted), nil
}

运行加密解密例子

package main

import (
   "bytes"
   "crypto/aes"
   "crypto/cipher"
   "encoding/hex"
   "fmt"
)

func AesEncrypt(encryptStr string, key []byte, iv string) (string, error) {
   encryptBytes := []byte(encryptStr)
   block, err := aes.NewCipher(key)
   if err != nil {
   return "", err
   }
   blockSize := block.BlockSize()
   encryptBytes = pkcs5Padding(encryptBytes, blockSize)
   blockMode := cipher.NewCBCEncrypter(block, []byte(iv))
   encrypted := make([]byte, len(encryptBytes))
   blockMode.CryptBlocks(encrypted, encryptBytes)
   return hex.EncodeToString(encrypted), nil
}

func AesDecrypt(decryptStr string, key []byte, iv string) (string, error) {
   decryptBytes, err := hex.DecodeString(decryptStr)
   if err != nil {
   return "", err
   }
   block, err := aes.NewCipher(key)
   if err != nil {
   return "", err
   }
   blockMode := cipher.NewCBCDecrypter(block, []byte(iv))
   decrypted := make([]byte, len(decryptBytes))
   blockMode.CryptBlocks(decrypted, decryptBytes)
   decrypted = pkcs5UnPadding(decrypted)
   return string(decrypted), 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 pkcs5UnPadding(decrypted []byte) []byte {
   length := len(decrypted)
   unPadding := int(decrypted[length-1])
   return decrypted[:(length - unPadding)]
}

func main() {
   data := "i am test data"
   key := []byte("1111111111111111")
   iv := "1111122211111111"
   encrypt, err := AesEncrypt(data, key, iv)
   if err != nil {
       panic(err)
       return
   }
   fmt.Println(encrypt)
   decrypt, err := AesDecrypt(encrypt, key, iv)
   if err != nil {
       panic(err)
   }
   
   fmt.Println(decrypt)
}

1.png

可以看到顺利生成加密后的字符串,加密后的字符串也顺利解密成功。


Node.JS

Node.js 我们同样使用标准库来进行加密解密,crypto模块提供通用的加密和哈希算法。用纯JavaScript代码实现这些功能不是不可能,但速度会非常慢。Nodejs用C/C++实现这些算法后,通过cypto这个模块暴露为JavaScript接口,这样用起来方便,运行速度也快。


加密

function aesEncode(data, secret,iv) {
    const cipher = crypto.createCipheriv('aes-128-cbc', secret, iv);
    var crypted = cipher.update(data, "binary", "hex");
    crypted += cipher.final('hex');
    return crypted
}

我们根据输入的 data, key,iv 创建 Cipheriv 对象,设置输入的data的格式,输出加密后 hex 编码的字符串。

解密

function aesDecode(data, secret,iv) {
    const cipher = crypto.createDecipheriv('aes-128-cbc', secret, iv);
    var crypted = cipher.update(data, "hex", "binary");
    crypted += cipher.final('binary');
    return crypted
}

我们根据输入的 加密字符串, key,iv 创建 Decipheriv 对象,设置输入的data的格式,输出数据的原始数据。

运行例子

var crypto = require("crypto");
function aesEncode(data, secret,iv) {
    const cipher = crypto.createCipheriv('aes-128-cbc', secret, iv);
    var crypted = cipher.update(data, "binary", "hex");
    crypted += cipher.final('hex');
    return crypted
}
function aesDecode(data, secret,iv) {
    const cipher = crypto.createDecipheriv('aes-128-cbc', secret, iv);
    var crypted = cipher.update(data, "hex", "binary");
    crypted += cipher.final('binary');
    return crypted
}
const date_crypto = aesEncode("111", "1234567812345678","1234567812345678")
console.log(date_crypto)
console.log(aesDecode(date_crypto, "1234567812345678","1234567812345678"))

2.png


node 、go 互相加密解密

接下来我们来简单模拟下 go 和 node 之间的数据传输过程。 统一使用 

data = “i am test data” 
key = “abcdefghabcdefgh” 
iv = “1234567812345678”


go 加密数据 node 解密

这是使用 go 加密后的数据,我们接下来使用 node解密

42961cdaeab0aba366826529fdc36035

33.png

可以看到顺利输出了原始数据


node 加密数据 go 解密

接下来我们先使用 node 加密同样的数据,然后使用 go 解密,看看能不能输出原始数据。

可以看到 node 也输出了同样的字符串 

42961cdaeab0aba366826529fdc36035

,接下来使用 go 解密。


可以看到也顺利还原数据成功。

i am test data

  • 上一条:
    在go语言json数据解析后,整数也是是float64类型
    下一条:
    在uni-app中使用Ucharts柱状图地步横向滚动条无法滑动解决方式
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-09
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-12
    • 2021-01
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-08
    • 2024-11
    • 2025-02
    • 2025-04
    • 2025-05
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客