对称加密-AES 64位加密 java实现

 

 

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
**
**64位加密解密
**/
public class AES64 {
	private static final String BASE64_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz*-";
	private static final char[] BASE64_CHARSET = BASE64_CHARS.toCharArray();
	public static String seedString = "retsaas";

	/**
	 * AES加密
	 */
	public static String encrypt(String seed, String cleartext)
			throws Exception {
		byte[] rawKey = getRawKey(seed.getBytes("UTF-8"));
		byte[] result = encrypt(rawKey, cleartext.getBytes("UTF-8"));
		return toBase64(result);
	}

	/**
	 * AES解密
	 */
	public static String decrypt(String seed, String encrypted)
			throws Exception {
		byte[] rawKey = getRawKey(seed.getBytes("UTF-8"));
		byte[] enc = fromBase64(encrypted);
		byte[] result = decrypt(rawKey, enc);
		return new String(result);
	}

	private static byte[] getRawKey(byte[] seed) throws Exception {
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
		sr.setSeed(seed);
		kgen.init(128, sr); // 192 and 256 bits may not be available
		SecretKey skey = kgen.generateKey();
		byte[] raw = skey.getEncoded();
		raw=new byte[]{-35, 55, 36, -8, 2, -104, 59, -47, -57, 82, -4, 29, 101, -18, 2, -125};
		return raw;
	}

	private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		byte[] encrypted = cipher.doFinal(clear);
		return encrypted;
	}

	private static byte[] decrypt(byte[] raw, byte[] encrypted)
			throws Exception {
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.DECRYPT_MODE, skeySpec);
		byte[] decrypted = cipher.doFinal(encrypted);
		return decrypted;
	}
 
 
 
	/**
	 * 字符串转换为字节数组 
	 * @param str
	 * @return
	 * @throws Exception
	 */
	private static byte[] fromBase64(String str) throws Exception {
		int len = str.length();
		if (len == 0) {
			throw new Exception("Empty string");
		}
		byte[] a = new byte[len + 1];
		int i, j;
		for (i = 0; i < len; i++) {
			try {
				a[i] = (byte) BASE64_CHARS.indexOf(str.charAt(i));
			} catch (Exception x) {
				throw new Exception("Illegal character at #" + i);
			}
		}
		i = len - 1;
		j = len;
		try {
			while (true) {
				a[j] = a[i];
				if (--i < 0) {
					break;
				}
				a[j] |= (a[i] & 0x03) << 6;
				j--;
				// a[j] = (byte)((a[i] & 0x3C) >>> 2);
				a[j] = (byte) ((a[i] & 0x3C) >> 2);
				if (--i < 0) {
					break;
				}
				a[j] |= (a[i] & 0x0F) << 4;
				j--;
				// a[j] = (byte)((a[i] & 0x30) >>> 4);
				a[j] = (byte) ((a[i] & 0x30) >> 4);
				if (--i < 0) {
					break;
				}
				a[j] |= (a[i] << 2);
				j--;
				a[j] = 0;
				if (--i < 0) {
					break;
				}
			}
		} catch (Exception ignored) {
		}

		try { // ignore leading 0-bytes
			while (a[j] == 0) {
				j++;
			}
		} catch (Exception x) {
			return new byte[1]; // one 0-byte
		}
		byte[] result = new byte[len - j + 1];
		arraycopy(a, j, result, 0, len - j + 1);
		return result;
	}
	/**
	 * 把一个字节数组的元素拷贝到另一个字节数组中
	 * @param src
	 * @param srcPos
	 * @param dest
	 * @param destPos
	 * @param length
	 */
	private static void arraycopy(byte[] src, int srcPos, byte[] dest, int destPos,
			int length) {
		if (dest != null && src != null) {// 当两个都不为空时
			byte[] temp = new byte[length];
			for (int i = 0; i < length; i++) {
				temp[i] = src[srcPos + i];
			}
			for (int i = 0; i < length; i++) {
				dest[destPos + i] = temp[i];
			}
		}
	}
	
	

	/**
	 * 字节数组转换为字符串
	 * @param buffer
	 * @return
	 */
	private static String toBase64(byte[] buffer) {
		int len = buffer.length, pos = len % 3;
		byte b0 = 0, b1 = 0, b2 = 0;
		switch (pos) {
		case 1:
			b2 = buffer[0];
			break;
		case 2:
			b1 = buffer[0];
			b2 = buffer[1];
			break;
		}
		String returnValue = "";
		int c;
		boolean notleading = false;
		do {
			// c = (b0 & 0xFC) >>> 2;
			c = (b0 & 0xFC) >> 2;
			if (notleading || c != 0) {
				returnValue += BASE64_CHARSET[c];
				notleading = true;
			}
			// c = ((b0 & 0x03) << 4) | ((b1 & 0xF0) >>> 4);
			c = ((b0 & 0x03) << 4) | ((b1 & 0xF0) >> 4);
			if (notleading || c != 0) {
				returnValue += BASE64_CHARSET[c];
				notleading = true;
			}
			// c = ((b1 & 0x0F) << 2) | ((b2 & 0xC0) >>> 6);
			c = ((b1 & 0x0F) << 2) | ((b2 & 0xC0) >> 6);
			if (notleading || c != 0) {
				returnValue += BASE64_CHARSET[c];
				notleading = true;
			}
			c = b2 & 0x3F;
			if (notleading || c != 0) {
				returnValue += BASE64_CHARSET[c];
				notleading = true;
			}
			if (pos >= len) {
				break;
			} else {
				try {
					b0 = buffer[pos++];
					b1 = buffer[pos++];
					b2 = buffer[pos++];
				} catch (Exception x) {
					break;
				}
			}
		} while (true);

		if (notleading) {
			return returnValue;
		}
		return "0";
	}
	
	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String text = "sdfsd";
		String jiami = encrypt(seedString, text);
		System.out.println("加密==" + jiami);
		System.out.println(decrypt(seedString, jiami));
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值