go、JS AES(CBC模式)加密解密兼容
js 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script src="aes/aes.js"></script>
<!--
<script src="components/pad-zeropadding.js"></script>
-->
<body>
<script>
var key = CryptoJS.enc.Utf8.parse("7971267812365mn8");
var plaintText = 'hello world'; // 明文
var iv=key; //16位字符串
var encryptedData = CryptoJS.AES.encrypt(plaintText, key, {
iv: key,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
console.log("加密前:"+plaintText);
console.log("加密后:"+encryptedData); //Pkcs7: WoCzvm6eZiM4/bx5o/CzGw==
//ZeroPadding : cUYmaJktt7P+dqv+Ijds9g==
console.log("加密后 base64:"+encryptedData.ciphertext.toString(CryptoJS.enc.Base64));
encryptedData = encryptedData.ciphertext.toString();
console.log("解密前-no-hex:"+encryptedData);
var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData);
console.log("解密前hex:"+encryptedHexStr);
var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
console.log("解密前:"+encryptedBase64Str);
var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
iv: key,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
console.log("解密后:"+decryptedStr);
</script>
</body>
</html>
go代码:
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"en_de/cryption"
"encoding/base64"
"fmt"
)
func testAes111() {
// AES-128。key长度:16, 24, 32 bytes 对应 AES-128, AES-192, AES-256
key := []byte("7971267812365mn8")
result, err := AesEncrypt([]byte("hello world"), key)
if err != nil {
panic(err)
}
fmt.Println("加密后:",base64.StdEncoding.EncodeToString(result)) //zero UR5c4C1iW5mIdxrv5rxo4w==,pkcs jE7BUAKWpdJWb2ulcFWd/g== 和pthon,js相同
origData, err := AesDecrypt(result, key)
if err != nil {
panic(err)
}
fmt.Println("解密后:",string(origData))
}
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)
//origData = ZeroPadding(origData, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) //iv=key
crypted := make([]byte, len(origData))
// 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
// crypted := 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))
// origData := crypted
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
// origData = ZeroUnPadding(origData)
return origData, nil
}
func ZeroPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{0}, padding)
return append(ciphertext, padtext...)
}
func ZeroUnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
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)]
}
func main() {
testAes111()
return
}
go、JS AES(CBC模式)加密解密兼容相关推荐
- python aes加密 cbc_【转载】python AES CBC模式加密
今天需要用到AES CBC模式加密,搜索了很久,终于加密成功,记录一下今天的理解. 首先要安装pycrypto库,不知道为什么在windows安装失败,在linux可以正常安装 这里有几点要注意,ke ...
- C++或C 实现AES ECB模式加密解密,支持官方验证
本文主要介绍 AES 算法的加解密方法.本文使用的语言为 C++,调用的 AES 库为:cryptopp. 1 概述 AES 加密算法的介绍如下(摘自 WikiPedia): 高级加密标准(英语:Ad ...
- python aes加密 cbc_Python实现AES的CBC模式加密和解密过程详解 和 chr() 函数 和 s[a:b:c] 和函数lambda...
1.chr()函数 chr() 用一个范围在 range(256)内的(就是0-255)整数作参数,返回一个对应的字符. 2.s[a:b:c] s=(1,2,3,4,5) 1>. s[a]下标访 ...
- python 3des加密_DES/3DES之ECB模式和CBC模式加解密 及 Python 实现
概念说明 DES:Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法. 3DES:Triple DES,是三重数据加密算法(TDEA,Triple Data ...
- Vue前端和Java后端 联调使用AES 前后端加密解密
Vue前端和Java后端 联调使用AES 前后端加密解密 最近在项目中需要针对重要数据进行加密传输,在网上找了一大推加密方式 最终采用AES 加密 Java端 package com.zk.web.u ...
- AES CBC模式下的CBC bit flipping Attack
<?xml version="1.0" encoding="utf-8"?> AES CBC模式下的CBC bit flipping Attack ...
- 【转】国密算法sm4 CBC模式加解密
一.什么是CBC模式? CBC模式的全称是Cipher Block Chaining模式(密文分组链接模式),之所以叫这个名字,是因为密文分组像链条一样相互连接在一起. 在CBC模式中,首先将明文分组 ...
- angular和JAVA实现aes、rsa加密解密,前后端交互,前端加解密和后端JAVA加解密实现
今天实现了下AES和RSA加密解密,主要的功能是对前后端交互数据进行加密解密,为什么要用到两个算法呢,首先RSA默认的话加密长度是有限的100多个byte吧大约,并且需要公钥私钥,而AES加密没有限制 ...
- python aes 加盐 加密解密
python aes 加盐 加密解密 爬虫就是在学习的过程,前几天从某网站的js里学到的加密方式,通过下断点弄清加密原理后,用python复现一下,在这里和大家分享下 #--------引入模块--- ...
最新文章
- 计算机视觉及深度学习_面试问题(一)
- highcharts一天时间 与一周时间_如何规划自己一天的时间
- 图解CMS垃圾回收机制,你值得拥有
- dbc数据库 与 mysql_使用反应式关系数据库连接规范R2DBC操作MySQL数据库
- Git之删除本地无用分支
- c语言图片raw保存,求指导,如何用c语言实现读取*.raw格式图像
- php $path_info,PHP $_SERVER['PATH_INFO'] 无法获取到内容怎么办?
- python作者_Python
- [转] linux操作系统下c语言编程入门--基础知识
- Java+Swing+mysql实现学生选课管理系统
- 常见Android智能手机通信录导入方法
- 三国群英传服务器维护是几点,20191112维护公告
- flask蓝图buleprint使用
- linux设置spi时钟频率,Linux下S3C2416的SPI设置问题,CLK和MOSI都没有输出,求助
- windows7隐藏桌面计算机,win7小技巧之隐藏桌面图标
- 【Web技术】948- 生成前端海报的 N 种方案和优劣
- Flask--Blueprint(蓝图)简单使用笔记
- 小甲鱼Python学习笔记之魔法方法
- FFmpeg命令行工具系列二---转码流程及过滤器
- 从粉丝经济看粉丝运营
热门文章
- 计算机软件方法专利撰写,干货 | 计算机软件专利撰写模板
- python3 cookie_Python3标准库:http.cookies HTTP cookie
- w ndows7启动项怎么关,win7如何关闭启动项Windows Security notification icon
- SAP ABAP ZBA_R003 查询用户下的角色里的公司
- python中运用django_【Django】在项目中使用python
- layui自定义新增tab页方法
- linux 源码搭建lnmp_LINUX CENTOS 6.5下源码搭建LNMP
- powerpoint预览_如何调整PowerPoint模板的大小
- 一款好玩的关联微信运动的云遛狗微信小程序
- 深入理解Java虚拟机到底是什么