JAVA和C#通用的AES加密
来源:互联网 发布:淘宝和农村淘宝的区别 编辑:程序博客网 时间:2024/05/22 00:08
JAVA版本
AESUtils工具类
import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;public class AESUtils { //实际的加密解密操作 private static byte[] Operation(byte[] src,String key,int mode) throws Exception{ if (key==null) { System.out.println("Key不能为空"); return null; } if (key.length()!=16) { System.out.println("Key需要16位长度"); return null; } byte[] raw=key.getBytes("utf-8"); SecretKeySpec keySpec=new SecretKeySpec(raw, "AES"); Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(mode, keySpec); byte[] encrypted=cipher.doFinal(src); return encrypted; } public static byte[] Encrypt(byte[] src,String key) throws Exception{ return Operation(src, key, Cipher.ENCRYPT_MODE); } public static byte[] Decrypt(byte[] src,String key) throws Exception{ return Operation(src, key, Cipher.DECRYPT_MODE); }}
主函数
import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class Main { public static void main(String[] args) { String key="kkkkkkk123456789";//必须保持16位// System.out.println("原数据:"); String content="Hello,world"; System.out.println(content); byte[] contentByte=content.getBytes(); System.out.println("原数据转字节:"); for (int i = 0; i < contentByte.length; i++) { System.out.print(contentByte[i]); } System.out.println(); try { System.out.println("加密后字节:"); byte[] resultByte=AESUtils.Encrypt(contentByte, key); for (int i = 0; i < resultByte.length; i++) { System.out.print(resultByte[i]); } System.out.println(); System.out.println("解密后字节:"); byte[] finalByte= AESUtils.Decrypt(resultByte, key); for (int i = 0; i < finalByte.length; i++) { System.out.print(finalByte[i]); } System.out.println(); System.out.println("解密后数据:"); System.out.println(new String(finalByte)); String strBase64=new BASE64Encoder().encode(resultByte); System.out.println("加密后Base64的数据,这个可在C#解密:"); System.out.println(strBase64); byte[] base64Byte=new BASE64Decoder().decodeBuffer(strBase64); System.out.println("Base64解密后的数据:"); byte[] base64ResultByte=AESUtils.Decrypt(base64Byte, key); System.out.println(new String(base64ResultByte)); } catch (Exception e) { e.printStackTrace(); } }}
C#版本
AES工具类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Security.Cryptography;using System.IO;namespace AESTest{ class AESHelper { private static byte[] Operation(byte[] src,string strKey,bool isEncrypt) { if (string.IsNullOrEmpty(strKey)) return null; RijndaelManaged rm = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(strKey), Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }; ICryptoTransform cTransform; if(isEncrypt) { cTransform = rm.CreateEncryptor(); } else { cTransform = rm.CreateDecryptor(); } byte[] resultArray = cTransform.TransformFinalBlock(src, 0, src.Length); return resultArray; } public static byte[] Encrypt(byte[] src, string strKey) { return Operation(src, strKey, true); } public static byte[] Decrypt(byte[] src, string strKey) { return Operation(src, strKey, false); } }}
主函数
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Security.Cryptography;using System.IO;namespace AESTest{ class Program { static void Main(string[] args) { string key = "kkkkkkk123456789";//保持16位 string content = "Hello,world"; Console.WriteLine("原数据:"); Console.WriteLine(content); Console.WriteLine("原数据转字节:"); byte[] contentByte = System.Text.Encoding.Default.GetBytes(content); for (int i = 0; i < contentByte.Length;i++) { Console.Write(contentByte[i]); } Console.WriteLine(); Console.WriteLine("加密后字节:"); byte[] resultByte = AESHelper.Encrypt(contentByte, key); for (int i = 0; i < resultByte.Length; i++) { Console.Write(resultByte[i]); } Console.WriteLine(); Console.WriteLine("解密后字节:"); byte[] finalByte = AESHelper.Decrypt(resultByte, key); for (int i = 0; i < finalByte.Length; i++) { Console.Write(finalByte[i]); } Console.WriteLine(); Console.WriteLine("解密后数据:"); Console.WriteLine(System.Text.Encoding.Default.GetString(finalByte)); Console.WriteLine("BASE64加密后数据,Java可用:"); Console.WriteLine(Convert.ToBase64String(resultByte)); Console.WriteLine("BASE64解密后数据,Java可用:"); string javaData = "U/YfVsHyIjczgk9iQfR2VA=="; byte[] javaBaseArray= Convert.FromBase64String(javaData); byte[] finalJavaByte=AESHelper.Decrypt(javaBaseArray, key); Console.WriteLine(System.Text.Encoding.Default.GetString(finalJavaByte)); Console.Read(); } }}
阅读全文
1 0
- JAVA和C#通用的AES加密
- AES 加密,net和java 通用加密
- android 和java平台通用的AES加密解密
- 关于JAVA和C#的AES + Base64加密互通!~
- JAVA AES加密 对应的 C# 方法
- JAVA AES加密 对应的 C# 方法
- OC与JAVA通用的AES加密解密
- OC与JAVA通用的AES加密解密
- Java 和 C#通用的DES加密工具类
- JAVA android IOS AES通用加密
- ios、Android、java通用AES加密方式
- JAVA的AES加密
- java 和 C# 通用加密解密
- 在Java和C#中的简单的AES加密和解密
- java实现对称加密AES和DES的加密、解密
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现【多种语言AES/CBC/PKCS5Padding通用加解密数据】
- Java aes加密C#解密的取巧方法
- Java的AES加密解密
- 向量范数与机器学习中的正则项
- linux怎么不输入路径直接运行程序脚本
- 第五天:html—各种常用标签
- gcc 与 g++ 的使用
- Python学习——os模块中几个常用的方法
- JAVA和C#通用的AES加密
- MeasureSpec的简单说明
- 《机器学习实战》代码片段学习2 决策树
- How to Install Nas System with PI
- py-faster-rcnn + ZF 实现自己的数据训练与检测(一)
- logback日志配置文件学习
- Google Hacking
- java AbstractMethodError 的解释
- bzoj3732 Network(Kruskal重构树)