JAVA 与C# 的AES的加密函数、解密函数,加密解密结果一致

因为开发需要用到java以及c#之间的数据传输,所以使用到AES的加密函数、解密函数。亲测可用,分享给大家:

java代码:

package my;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;

public class Xianchengmingming {   
    public static void main(String[] args) throws Exception {   
        String key = "ABCDEFGHIJKLMNRS";   
        String value="hsb:123:1489048530930:60001";   
        System.out.println("加密:"+aesEncrypt(value,key)); 
        System.out.println("解密:"+aesDecrypt(aesEncrypt(value,key),key));      
    }   
    /**
	 * AES的加密函数
	 * @param str 传入需要加密的字符
	 * @param key 传入一个16位长度的密钥。否则报错
	 * @return 执行成功返回加密结果,否则报错
	 * @throws Exception 抛出一个加密异常
	 */
	public static String aesEncrypt(String str, String key) throws Exception {
        if (str == null || key == null) return null;
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
        byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
        return new BASE64Encoder().encode(bytes);
    }
	/**
	 * AES的解密函数
	 * @param str 传入需要解密的字符
	 * @param key 传入一个16位长度的密钥。否则报错
	 * @return 执行成功返回加密结果,否则报错
	 * @throws Exception 抛出一个解密异常
	 */
    public static String aesDecrypt(String str, String key) throws Exception {
        if (str == null || key == null) return null;
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
        byte[] bytes = new BASE64Decoder().decodeBuffer(str);
        bytes = cipher.doFinal(bytes);
        return new String(bytes, "utf-8");
    } 
} 

C#代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO; 

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            String key = "ABCDEFGHIJKLMNRS";
            Console.WriteLine("解密" + Decrypt("/4+UJG55uVHN6rninZ6Z8nUe2OJHGGAYqcdmdDeQhiM=", key));
            Console.ReadLine();
        }

        /// 
        /// 有密码的AES加密 
        /// 
        /// 加密字符
        /// 加密的密码
        /// 密钥
        /// 
        public static string Encrypt(string toEncrypt,string key)
        {

            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// 
        /// AES解密
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string Decrypt(string toDecrypt,string key)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }

    }

}
 
java运行结果:

JAVA 与C# 的AES的加密函数、解密函数,加密解密结果一致_第1张图片

c#运行结果:

JAVA 与C# 的AES的加密函数、解密函数,加密解密结果一致_第2张图片

你可能感兴趣的:(java,c#,AES)