AES加密解密(互通.NET和JAVA)

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

namespace YFKClient.Code
{
    public class AESUtility
    {
        private static char[] h = "0123456789ABCDEF".ToCharArray();
        private static Dictionary hs = new Dictionary();
        static AESUtility()
        {
            for (int i = 'A'; i <= 'F'; i++)
            {
                hs.Add(((char)i), i - 'A' + 10);
            }
            for (int i = 'a'; i <= 'f'; i++)
            {
                hs.Add(((char)i), i - 'a' + 10);
            }
            for (int i = '0'; i <= '9'; i++)
            {
                hs.Add(((char)i), i - '0');
            }
        }
        /// 
        /// AES加密(互通JAVA)
        /// 
        /// 明文
        /// 密钥
        /// 密文
        public static string Encrypt(string data, string key)
        {
            string result = string.Empty;
            int len = data.Length;
            StringBuilder d = new StringBuilder();
            d.Append(leftFill(len, 2, "0"));
            d.Append(rightFill(data, 14, "F"));

            result = h2s(Encrypt(Encoding.UTF8.GetBytes(d.ToString()), h2b(key)));

            return result;
        }
        /// 
        /// AES解密(互通JAVA)
        /// 
        /// 
        /// 
        /// 
        public static string Decrypt(string data, string key)
        {
            byte[] bytes = Decrypt(h2b(data), h2b(key));
            string result = UTF8Encoding.UTF8.GetString(bytes);
            if (result != null && result.Length > 1)
            {
                int len = int.Parse(result.Substring(0, 2));
                if (len + 2 <= result.Length)
                    result = result.Substring(2, len + 2-2);
            }
            return result;
        }
        /// 
        /// AES解密(互通JAVA)
        /// 
        /// 
        /// 
        /// 
        public static byte[] Decrypt(byte[] data, byte[] key)
        {
            byte[] keyArray = key;
            byte[] ivArray = key;
            byte[] toEncryptArray = data;

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.IV = ivArray;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.Zeros;

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return resultArray;
        }
        /// 
        /// AES加密(互通JAVA)
        /// 
        /// 
        /// 
        public static byte[] Encrypt(byte[] data, byte[] key)
        {
            byte[] keyArray = key;
            byte[] ivArray = key;
            byte[] toEncryptArray = data;
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.IV = ivArray;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.Zeros;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return resultArray;

        }
        /// 
        /// 二进制数据流转十六进制字符串
        /// 
        /// 
        /// 
        public static String h2s(byte[] bytes)
        {

            StringBuilder s = new StringBuilder();
            foreach (byte b in bytes)
            {
                s.Append(h[(b >> 4) & 0xF]);
                s.Append(h[b & 0xF]);
            }
            return s.ToString();
        }
        /// 
        /// 十六进制字符串转二进制数据流
        /// 
        /// 
        /// 
        public static byte[] h2b(String hex)
        {
            byte[] bs = new byte[hex.Length / 2];
            int i = 0;
            char[] s = hex.ToCharArray();
            for (int j = 0; j < s.Length; )
            {
                bs[i++] = (byte)((hs[s[j++]] << 4) | hs[s[j++]]);
            }
            return bs;
        }
        /// 
        /// 左补充
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string leftFill(object obj, int len, string fill)
        {

            if (obj == null)
                return null;
            string src = obj.ToString();
            StringBuilder s = new StringBuilder();
            for (int l = src.Length; l < len; l++)
            {
                s.Append(fill);
            }
            s.Append(src);
            return s.ToString();
        }
        /// 
        /// 右补充
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string rightFill(object obj, int len, string fill)
        {

            if (obj == null)
                return null;
            string src = obj.ToString();
            StringBuilder s = new StringBuilder();
            s.Append(src);
            for (int l = src.Length; l < len; l++)
            {
                s.Append(fill);
            }
            return s.ToString();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YFKClient.Code;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string pwdKey = "225E8C70688FD76EC5C01A392302320A";
            string pwd = "440196";
            Console.WriteLine(AESUtility.Encrypt(pwd, pwdKey));
            string con = "25F558AD2366AECCCB0E5FC192514737";
            Console.WriteLine(AESUtility.Decrypt(con, pwdKey));
        }
    }
}

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class EnUtil {


	private final static String AES_CIPHER = "AES/CBC/NoPadding";
	private final static String AES_SECRET = "AES";

	public static void main(String[] args) {

		String pwdKey = "225E8C70688FD76EC5C01A392302320A";
		String pwd = "440196";

		
		
		System.out.println("生成密文 : " + (encrypt(pwd, pwdKey)));
		String con = "25F558AD2366AECCCB0E5FC192514737";
		System.out.println("解密 : " + decrypt(con, pwdKey));
	}

	public static byte[] genKey() {
		byte[] result = null;
		try {
			// KeyGenerator提供对称密钥生成器的功能,支持各种算法
			KeyGenerator keygen = KeyGenerator.getInstance("AES");
			keygen.init(128, new SecureRandom());
			// SecretKey负责保存对称密钥
			SecretKey deskey = keygen.generateKey();
			result = deskey.getEncoded();
		} catch (NoSuchAlgorithmException e) {
			//log.warn("[AES密钥生成]系统不支持AES算法", e);
		}
		return result;
	}

	public static byte[] genMD5Key() {
		byte[] result = null;
		try {
			// KeyGenerator提供对称密钥生成器的功能,支持各种算法
			KeyGenerator keygen = KeyGenerator.getInstance("AES");
			keygen.init(128, new SecureRandom());
			// SecretKey负责保存对称密钥
			SecretKey deskey = keygen.generateKey();
			result = deskey.getEncoded();
		} catch (NoSuchAlgorithmException e) {
			//log.warn("[AES密钥生成]系统不支持AES算法", e);
		}
		return result;
	}

	private static final char[] h = "0123456789ABCDEF".toCharArray();

	private static Map hs = new HashMap();
	static {
		for (int i = 'A'; i <= 'F'; i++) {
			hs.put(((char) i), i - 'A' + 10);
		}
		for (int i = 'a'; i <= 'f'; i++) {
			hs.put(((char) i), i - 'a' + 10);
		}
		for (int i = '0'; i <= '9'; i++) {
			hs.put(((char) i), i - '0');
		}
	}

	/**
	 * 十六进制字符串转二进制数据流
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] h2b(String hex) {

		byte[] bs = new byte[hex.length() / 2];
		int i = 0;
		char[] s = hex.toCharArray();
		for (int j = 0; j < s.length;) {
			bs[i++] = (byte) ((hs.get(s[j++]) << 4) | hs.get(s[j++]));
		}
		return bs;
	}

	/**
	 * 二进制数据流转十六进制字符串
	 * 
	 * @param bytes
	 * @return
	 */
	public static String h2s(byte[] bytes) {

		StringBuilder s = new StringBuilder();
		for (byte b : bytes) {
			s.append(h[(b >> 4) & 0xF]);
			s.append(h[b & 0xF]);
		}

		return s.toString();
	}

	/**
	 * AES加密
	 * 
	 * @param data
	 *            明文
	 * @param key
	 *            密钥
	 * @return 密文
	 */
	public static String encrypt(String data, String key) {
		System.out.println("[AES加密]加密前--明文 : " + data + "; 加密密钥 : " + key);

		int len = data.length();
		StringBuilder d = new StringBuilder();
		d.append(leftFill(len, 2, "0"));
		d.append(rightFill(data, 14, "F"));
		String tmp = null;
		String result = h2s(encrypt((tmp = d.toString()).getBytes(), h2b(key)));
		System.out.println("[AES加密]加密后--明文 : " + data + "(" + tmp + "); 加密密钥 : " + key+ "; 加密密文 : " + result);
		return result;
	}

	/**
	 * AES解密
	 * 
	 * @param data
	 *            密文
	 * @param key
	 *            密钥
	 * @return 明文
	 */
	public static String decrypt(String data, String key) {
		System.out.println("[AES解密]解密前--密文 : " + data + "; 解密密钥 : " + key);
		byte[] bytes = decrypt(h2b(data), h2b(key));
		System.out.println("[AES解密]解密结束 : " + (bytes == null ? "null" : h2s(bytes)));
		String result = bytes == null ? null : new String(bytes);
		if (result != null && result.length() > 1) {
			int len = Integer.valueOf(result.substring(0, 2));
			if (len + 2 <= result.length())
				result = result.substring(2, len + 2);
		}
		System.out.println("[AES解密]解密后--密文 : " + data + "; 解密密钥 : " + key + "; 解密明文 : "+ result);
		return result;
	}

	/**
	 * AES加密方法
	 * 
	 * @param data
	 *            待加密明文
	 * @param key
	 *            加密密钥
	 * @return
	 */
	public static byte[] encrypt(byte[] data, byte[] key) {

		byte[] encryptedData = null;
		try {
			Cipher cipher = Cipher.getInstance(AES_CIPHER);// 创建密码器
			IvParameterSpec iv = new IvParameterSpec(key);
			SecretKeySpec sp = new SecretKeySpec(createKey(key), AES_SECRET);

			cipher.init(Cipher.ENCRYPT_MODE, sp, iv);// 初始化
			// 执行加密操作
			encryptedData = cipher.doFinal(data);
		} catch (NoSuchAlgorithmException e) {
			//log.warn("[AES加密]系统不支持AES算法", e);
		} catch (InvalidKeyException e) {
			//log.warn("[AES加密]非法的密钥格式", e);
		} catch (NoSuchPaddingException e) {
			//log.warn("[AES加密]加密算法运算出错", e);
		} catch (IllegalBlockSizeException e) {
			//log.warn("[AES加密]加密数据块大小不正确", e);
		} catch (BadPaddingException e) {
			//log.warn("[AES加密]错误的加密数据封装", e);
		} catch (InvalidAlgorithmParameterException e) {
			//log.warn("[AES加密]错误的加密算法参数", e);
		}
		return encryptedData;
	}

	/**
	 * AES解密算法
	 * 
	 * @param data
	 *            待解密密文
	 * @param key
	 *            解密密钥
	 * @return
	 */
	public static byte[] decrypt(byte[] data, byte[] key) {
		byte[] decryptedData = null;

		try {
			Cipher cipher = Cipher.getInstance(AES_CIPHER);// 创建密码器
			IvParameterSpec iv = new IvParameterSpec(key);
			SecretKeySpec sp = new SecretKeySpec(createKey(key), AES_SECRET);

			cipher.init(Cipher.DECRYPT_MODE, sp,iv);// 初始化

			// 执行加密操作
			decryptedData = cipher.doFinal(data);

		} catch (NoSuchAlgorithmException e) {
			//log.warn("[AES解密]系统不支持AES算法", e);
		} catch (InvalidKeyException e) {
			//log.warn("[AES解密]解法的密钥格式", e);
		} catch (NoSuchPaddingException e) {
			//log.warn("[AES解密]解密算法运算出错", e);
		} catch (IllegalBlockSizeException e) {
			//log.warn("[AES解密]解密数据块大小不正确", e);
		} catch (BadPaddingException e) {
			//log.warn("[AES解密]错误的解密数据封装", e);
		} catch (InvalidAlgorithmParameterException e) {
			//log.warn("[AES加密]错误的解密算法参数", e);
		}

		return decryptedData;
	}

	public static byte[] createKey(byte[] key) {
		 return key;
//		String algorithmName = "";
//		try {
//			algorithmName = "AES";
//			KeyGenerator kgen = KeyGenerator.getInstance(algorithmName);
//			algorithmName = "SHA1PRNG";
//			SecureRandom secureRandom = SecureRandom.getInstance(algorithmName);
//			secureRandom.setSeed(key);
//			kgen.init(128, secureRandom);
//			SecretKey secretKey = kgen.generateKey();
//			return secretKey.getEncoded();
//		} catch (NoSuchAlgorithmException e) {
//			log.warn("[密钥生成]系统不支持此算法", e);
//		}
//		return null;
	}

	/**
	 * MD5 校验
	 * 
	 * @param data
	 * @return
	 */
	public static String md5(String data) {

		String result = null;
		try {
			result = h2s(md5(data.getBytes("UTF-8")));
		} catch (UnsupportedEncodingException e) {
			//log.warn("[MD5加密]程序不支持 UTF-8 编码");
		}
		System.out.println("[MD5加密]校验结果 : " + result + "; 校验内容 : " + data);
		return result;
	}

	/**
	 * MD5加密
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] md5(byte[] data) {

		byte[] result = null;
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(data);
			result = messageDigest.digest();
		} catch (NoSuchAlgorithmException e) {
			//log.warn("[MD5加密]不支持MD5算法", e);
		}

		return result;
	}

	/**
	 * 左补充
	 * 
	 * @param obj
	 * @param len
	 * @param fill
	 * @return
	 */
	public static String leftFill(Object obj, int len, String fill) {

		if (obj == null)
			return null;
		String src = String.valueOf(obj);
		StringBuilder s = new StringBuilder();
		for (int l = src.length(); l < len; l++) {
			s.append(fill);
		}
		s.append(src);
		return s.toString();
	}

	/**
	 * 右补充
	 * 
	 * @param obj
	 * @param len
	 * @param fill
	 * @return
	 */
	public static String rightFill(Object obj, int len, String fill) {

		if (obj == null)
			return null;
		String src = String.valueOf(obj);
		StringBuilder s = new StringBuilder();
		s.append(src);
		for (int l = src.length(); l < len; l++) {
			s.append(fill);
		}
		return s.toString();
	}

}

http://www.2cto.com/kf/201211/165570.html

你可能感兴趣的:(.NET学习笔记)