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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听