AES/ECB/PKCS5Padding-128在JAVA,PHP,JavaScript, Python,Golang五种语言中的相互加解密

本文主要是介绍AES/ECB/PKCS5Padding-128在JAVA,PHP,JavaScript, Python,Golang五种语言中的相互加解密,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 java版本AES/ECB/PKCS5Padding-128:

package test;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;public class DigestUtil {//用于构建十六进制输出private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};/*** AES解密 128 AES/ECB/PKCS5Padding** @param source      需要解密的字符串* @param key         需要解密的秘钥* @param hexOrBase64 true:hex,false:base64* @return 返回解密结果*/public static String decryptAES(String source, String key, boolean hexOrBase64) {try {SecretKey secretKey = new SecretKeySpec(key.getBytes(), "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE, secretKey);if (hexOrBase64) {return new String(cipher.doFinal(DigestUtil.hex2bytes(source)));} else {return new String(cipher.doFinal(DigestUtil.decodeBase64(source)));}} catch (Exception e) {System.out.println(e.getMessage());throw new RuntimeException("出错啦!", e);}}public static byte[] hex2bytes(String hexStr) {byte[] result = new byte[hexStr.length() / 2];for (int i = 0; i < hexStr.length() / 2; i++) {int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);result[i] = (byte) (high * 16 + low);}return result;}public static byte[] decodeBase64(String text) {return java.util.Base64.getDecoder().decode(text);}public static char[] bytes2hex(final byte[] data) {final int l = data.length;final char[] out = new char[l << 1];// two characters form the hex value.for (int i = 0, j = 0; i < l; i++) {out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];out[j++] = DIGITS_LOWER[0x0F & data[i]];}return out;}public static String encodeBase64(byte[] data) {return Base64.getEncoder().encodeToString(data);}/*** AES加密 128 AES/ECB/PKCS5Padding** @param source      需要加密的字符串* @param key         需要加密的秘钥* @param hexOrBase64 true:hex,false:base64* @return 返回加密结果*/public static String encryptAES(String source, String key, boolean hexOrBase64) {try {SecretKey secretKey = new SecretKeySpec(key.getBytes(), "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, secretKey);if (hexOrBase64) {return new String(DigestUtil.bytes2hex(cipher.doFinal(source.getBytes())));} else {return DigestUtil.encodeBase64(cipher.doFinal(source.getBytes()));}} catch (Exception e) {throw new RuntimeException("出错啦!", e);}}public static void main(String[] args) throws Exception {String value = "nwflag=1&empno=BEJY3293";//"HmtaYAN+n8dzKifi50/sA4Sv/dLKR5MnfZKcJVSzmST0D7ZkbmM8AebuNNkKBc4Z7DoXDnDwwwap2hg+6ztWQAZdQZOfAHR5bueqpApDN9il6gIuQadcdPcvrurRRiTifOQYsE9nRQKy6smol0jrz0wLEDxyPh3ZfBZG0e5KxrEq6Es6E3/05F4jIBYy+dUtvYBPSrVi3kzkblFex6qZOf/6jbtSlchWJVXMuEgA05LplUlUvNr5vwNC65KIy6beHcyhntIO2wJHx5uknjtidXUft3s87ZlxTzMEiKV6cXU9ejuCxdOz9n0c7UPeAOqHprxW2FvOhytVSMoSPP6No9pSochmKdXBPgOQd15rswBi0ejSZ+qWG4/MrBhVFD5OaQ8Cr7wy9hoWdnYrd0tcxPZv4EiwF3DDqctffYgQjy8=";//"openid=ouQdHv18KY5HPhW16cDk43Y5-yyk&nickname=%e6%9d%8e%e5%a8%81&headimg=http%3a%2f%2fthirdwx.qlogo.cn%2fmmopen%2fajNVdqHZLLB6qkUjQibrUJniaBhLf8hjS5L4roMR84T62m7NyHiaIAXKh1cl2vJwfdSfEzju8HicugEXbJQFHx1pZw%2f132&rytype=1&nwflag=1&empno=BEJY3293&stime=1603340863000";String key = "5678998765123322";//加密String s = encryptAES(value, key, false);System.out.println(s);//解密String k = decryptAES(s, key, false);System.out.println(k);}
}

PHP版本:

class Aes
{/*** @param $input string 待加密字符串* @param $key string 秘钥* @param bool $hexOutput 默认true hex 输出, false base64输出* @return string 加密结果*/public static function encrypt($input, $key, $hexOutput = true){$data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);if ($hexOutput) {$data = bin2hex($data);}else {$data = base64_encode($data);}return $data;}/*** @param $sStr 解密字符串* @param $sKey 解密秘钥* @param bool $hexInput 解密模式 默认true hex输入, false base64输入* @return string 解密结果*/public static function decrypt($sStr, $sKey, $hexInput = true){if ($hexInput) {$sStr = hex2bin($sStr);}else {$sStr = base64_decode($sStr);}$decrypted = openssl_decrypt($sStr, 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA);return $decrypted;}}

JavaScript 使用GitHub - ihsmarkitosi/CryptoJS-v3.1.2: CryptoJS v3.1.2, 下载后按html示例引入

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title></head>
<script src="rollups/aes.js"></script>
<script src="components/mode-ecb.js"></script>
<script src="components/pad-nopadding.js"></script>
<script>/**** @param data string 待加密字符串* @param key string 秘钥* @returns {*} 返回base64或hex格式的密文*/function encodeAesString(data, key, hexInput=true) {var key = CryptoJS.enc.Utf8.parse(key);var encrypted = CryptoJS.AES.encrypt(data, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});if (hexInput) {return encrypted.ciphertext.toString();} else {return encrypted;}}/**** @param encrypted base64格式或hex格式的密文* @param key sting 秘钥* @returns {string} 解密后的字符串*/function decodeAesString(data, key, hexInput=true) {var key = CryptoJS.enc.Utf8.parse(key);if (hexInput) {data = CryptoJS.enc.Hex.parse(data);data = CryptoJS.enc.Base64.stringify(data);}var decrypted = CryptoJS.AES.decrypt(data, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});return decrypted.toString(CryptoJS.enc.Utf8);}// 测试加、解密function testAES() {var key = "5678998765123321";var data = "openid=ouQdHv18KY5HPhW16cDk43Y5-yyk&nickname=%e6%9d%8e%e5%a8%81&headimg=http%3a%2f%2fthirdwx.qlogo.cn%2fmmopen%2fajNVdqHZLLB6qkUjQibrUJniaBhLf8hjS5L4roMR84T62m7NyHiaIAXKh1cl2vJwfdSfEzju8HicugEXbJQFHx1pZw%2f132&rytype=1&nwflag=1&empno=BEJY3293&stime=1603340863000";console.log("加密前:" + data);var encrypted = encodeAesString(data, key); // 密文console.log("加密后: " + encrypted);console.log(encrypted == "e016d67088b27cba729f264def4d0633da66e046cdcb7bc7cef6b3be1334c45d8e68ab5108845a0e58b364725c81503c416f161c6aa02b136e2f3e62ca12228bb2a590ff8089a84487a0e1a283a93293615b83056b8633f137df25fb10cda385d8838d9f8d1afa092047963d3fb682bfbd804f4ab562de4ce46e515ec7aa9939fffa8dbb5295c8562555ccb84800d392e9954954bcdaf9bf0342eb9288cba6de1dcca19ed20edb0247c79ba49e3b6275751fb77b3ced99714f330488a57a7175b45b61f10a8099464ab4eee2c083255f2824bd38d84e0e89731861a9dd4b58b60c594e2664a2d8497fd749d19162d62d2fbe7fc086e983aad528890cf76a6d6f01603d003f1b59e51b4fa5edc9476c20");//     console.log("加密后: " + encrypted == "4BbWcIiyfLpynyZN700GM9pm4EbNy3vHzvazvhM0xF2OaKtRCIRaDlizZHJcgVA8QW8WHGqgKxNuLz5iyhIii7KlkP+AiahEh6DhooOpMpNhW4MFa4Yz8TffJfsQzaOF2IONn40a+gkgR5Y9P7aCv72AT0q1Yt5M5G5RXseqmTn/+o27UpXIViVVzLhIANOS6ZVJVLza+b8DQuuSiMum3h3MoZ7SDtsCR8ebpJ47YnV1H7d7PO2ZcU8zBIilenF1tFth8QqAmUZKtO7iwIMlXygkvTjYTg6Jcxhhqd1LWLYMWU4mZKLYSX/XSdGRYtYtL75/wIbpg6rVKIkM92ptbwFgPQA/G1nlG0+l7clHbCA=");//     var s = "4BbWcIiyfLpynyZN700GM9pm4EbNy3vHzvazvhM0xF2OaKtRCIRaDlizZHJcgVA8QW8WHGqgKxNuLz5iyhIii7KlkP+AiahEh6DhooOpMpNhW4MFa4Yz8TffJfsQzaOF2IONn40a+gkgR5Y9P7aCv72AT0q1Yt5M5G5RXseqmTn/+o27UpXIViVVzLhIANOS6ZVJVLza+b8DQuuSiMum3h3MoZ7SDtsCR8ebpJ47YnV1H7d7PO2ZcU8zBIilenF1tFth8QqAmUZKtO7iwIMlXygkvTjYTg6Jcxhhqd1LWLYMWU4mZKLYSX/XSdGRYtYtL75/wIbpg6rVKIkM92ptbwFgPQA/G1nlG0+l7clHbCA=";var decryptedStr = decodeAesString(encrypted, key);console.log("解密后: " + decryptedStr);}testAES();// t = decodeURIComponent(t);</script>
<body></body>
</html>

python AES

import hashlib
import binascii
from Crypto.Cipher import AES
import base64# padding算法
BLOCK_SIZE = 16
pad = lambda s: bytes(s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE), encoding='utf8')
unpad = lambda s: s[0:-ord(s[-1:])]class AES_ENCRYPT(object):def __init__(self, key, hex=True):self.key = keyself.hex_mode = hex# 加密函数def encrypt(self, text):cryptor = AES.new(self.key, AES.MODE_ECB)ciphertext = cryptor.encrypt(pad(text))if self.hex_mode:return binascii.b2a_hex(ciphertext)else:return  base64.b64encode(ciphertext)# 解密函数def decrypt(self, text):if self.hex_mode:decode = binascii.a2b_hex(text)else:decode = base64.b64decode(text)cryptor = AES.new(self.key, AES.MODE_ECB)plain_text = cryptor.decrypt(decode)return unpad(plain_text)if __name__ == '__main__':aes_encrypt = AES_ENCRYPT("5678998765123321", False)text = "openid=ouQdHv18KY5HPhW16cDk43Y5-yyk&nickname=%e6%9d%8e%e5%a8%81&headimg=http%3a%2f%2fthirdwx.qlogo.cn%2fmmopen%2fajNVdqHZLLB6qkUjQibrUJniaBhLf8hjS5L4roMR84T62m7NyHiaIAXKh1cl2vJwfdSfEzju8HicugEXbJQFHx1pZw%2f132&rytype=1&nwflag=1&empno=BEJY3293&stime=1603340863000"e = aes_encrypt.encrypt(text)print(e)d = aes_encrypt.decrypt(e)print(text)print(d)

Golang AES

package mainimport ("encoding/base64""encoding/base64""encoding/hex""fmt""github.com/forgoer/openssl")func main()  {src := []byte("openid=ouQdHv18KY5HPhW16cDk43Y5-yyk&nickname=%e6%9d%8e%e5%a8%81&headimg=http%3a%2f%2fthirdwx.qlogo.cn%2fmmopen%2fajNVdqHZLLB6qkUjQibrUJniaBhLf8hjS5L4roMR84T62m7NyHiaIAXKh1cl2vJwfdSfEzju8HicugEXbJQFHx1pZw%2f132&rytype=1&nwflag=1&empno=BEJY3293&stime=1603340863000")key := []byte("5678998765123321")//加密dst , _ := openssl.AesECBEncrypt(src, key, openssl.PKCS7_PADDING)fmt.Printf(base64.StdEncoding.EncodeToString(dst))fmt.Printf(hex.EncodeToString(dst))//解密dst , _ = openssl.AesECBDecrypt(dst, key, openssl.PKCS7_PADDING)fmt.Println(string(dst)) //
}

这篇关于AES/ECB/PKCS5Padding-128在JAVA,PHP,JavaScript, Python,Golang五种语言中的相互加解密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/749538

相关文章

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

Python装饰器之类装饰器详解

《Python装饰器之类装饰器详解》本文将详细介绍Python中类装饰器的概念、使用方法以及应用场景,并通过一个综合详细的例子展示如何使用类装饰器,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. 引言2. 装饰器的基本概念2.1. 函数装饰器复习2.2 类装饰器的定义和使用3. 类装饰

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映