支持php、c#、java、javascript、golang、swift、flutter-drat的通用aes-cbc-127 pkcs7填充算法例子
详细请查看https://mikudoc.com/balrogsxt/apidemo 加密解密篇,内有调试截图等信息
这里只显示4种算法,详细请查看链接后续会增加更多通用算法
php加密
//密钥偏移均为16位字符串 $key = '密钥'; $iv = '偏移'; $data = "加密数据内容"; $result = openssl_encrypt($data,'aes-128-cbc',$key,0,$iv) //$result即为加密返回数据字符串
php解密
//密钥偏移均为16位字符串 $key = '密钥'; $iv = '偏移'; $data = "解密数据内容"; $result = openssl_decrypt($data,'aes-128-cbc',$key,0,$iv); //$result即为解密返回数据字符串
Js加密
let c = require("crypto-js");//导入crypto-js //密钥偏移均为16位字符串 let key = '密钥'; let iv = '偏移'; let data = '加密内容'; data = c.enc.Utf8.parse(data); key = c.enc.Utf8.parse(key); iv = c.enc.Utf8.parse(iv); let r = c.AES.encrypt(data, key, { iv, mode: c.mode.CBC, padding: c.pad.Pkcs7 }); let result = r.toString(); //result及为加密结果
Js解密
let c = require("crypto-js");//导入crypto-js //密钥偏移均为16位字符串 let key = '密钥'; let iv = '偏移'; let data = '解密内容内容'; key = c.enc.Utf8.parse(key); iv = c.enc.Utf8.parse(iv); var decrypt = c.AES.decrypt(data, key, { iv, mode: c.mode.CBC, padding: c.pad.Pkcs7 }); let result = decrypt.toString(c.enc.Utf8); //result及为解密结果
Java加密&解密
package com.acgxt.java.test; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.Base64; public class Main { public static void main(String args[]){ String data = "加密的内容";//加密的内容 String key = "1111111111111111"; //16位字符串密钥 String iv = "1111111111111111";//16位字符串偏移 //加密数据 String encodeResult = encryptAES(data,key,iv); System.out.println(String.format("aes-cbc-128加密结果 -> %s",encodeResult)); //解密数据 String decodeResult = decryptAES(encodeResult,key,iv); System.out.println(String.format("解密结果: -> %s",decodeResult)); } /** * 加密数据 * @param data 明文内容 * @param key 密钥 * @param iv 偏移 * @return */ private static String encryptAES(String data,String key,String iv) { try { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"), new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8))); byte[] result = cipher.doFinal(pkcs7padding(data, cipher.getBlockSize())); // 好气啊, 要自己手动实现 PKCS7Padding 填充 return Base64.getEncoder().encodeToString(result); } catch (Exception e) { e.printStackTrace(); } return "NULL"; } /** * 解密数据 * @param data 加密内容 * @param key 密钥 * @param iv 偏移 * @return */ private static String decryptAES(String data, String key,String iv) { try { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"), new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8))); byte[] result = cipher.doFinal(Base64.getDecoder().decode(data)); return new String(result, StandardCharsets.UTF_8).trim(); } catch (Exception e) { e.printStackTrace(); } return "NULL"; } /** * pkcs7填充 * @param content * @param blockSize * @return */ private static byte[] pkcs7padding(String content, int blockSize) { byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8); int pad = blockSize - (contentBytes.length % blockSize); // 计算需要补位的长度 byte padChr = (byte) pad; // 补位字符 (即用补位长度) byte[] paddedBytes = new byte[contentBytes.length + pad]; // 在原有的长度上加上补位长度 System.arraycopy(contentBytes, 0, paddedBytes, 0, contentBytes.length); // 原有的先复制过去 for (int i = contentBytes.length; i < paddedBytes.length; i++) { // 补位字符填充 paddedBytes[i] = padChr; } return paddedBytes; } }
c#加密&解密
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace AesTest { class Program { static void Main(string[] args) { string data = "加密的内容"; string key = "1111111111111111"; string iv = "1111111111111111"; string encodeData = encode(data,key,iv); Console.WriteLine($"加密结果:{encodeData}"); string decodeData = decode(encodeData, key, iv); Console.WriteLine($"解密结果:{decodeData}"); Console.ReadKey(); } /// <summary> /// 加密数据 /// </summary> /// <param name="data">明文数据</param> /// <param name="key">密钥</param> /// <param name="iv">偏移</param> /// <returns></returns> private static string encode(string data, string key, string iv) { try { byte[] keyArray = Encoding.UTF8.GetBytes(key); byte[] ivArray = Encoding.UTF8.GetBytes(iv); byte[] toEncryptArray = Encoding.UTF8.GetBytes(data); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.IV = ivArray; rDel.Mode = CipherMode.CBC; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } catch (Exception e) { return null; } } /// <summary> /// 解密数据 /// </summary> /// <param name="data">加密内容</param> /// <param name="key">密钥</param> /// <param name="iv">偏移</param> /// <returns></returns> private static string decode(string data, string key, string iv) { try { byte[] keyArray = Encoding.UTF8.GetBytes(key); byte[] ivArray = Encoding.UTF8.GetBytes(iv); byte[] toEncryptArray = Convert.FromBase64String(data); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.IV = ivArray; rDel.Mode = CipherMode.CBC; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray); } catch { return null; } } } }
结束,详细截图请在https://mikudoc.com/balrogsxt/apidemo中加密解密篇查看
6条评论登录后可见