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();        }    }}
原创粉丝点击