AES+RSA加密及解密

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/P923284735/article/details/83655213

AES、RSA加解密

  1. RSA加解密
  2. AES加解密
  3. Base64编码
  4. 代码示例

RSA加密工具类

package RSA;

import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

public class RSAUtils {
	/**
	 * 加密算法RSA
	 */
	public static final String KEY_ALGORITHM = "RSA";

	/**
	 * 签名算法
	 */
	public static final String SIGNATURE_ALGORITHM = "MD5withRSA";

	/**
	 * 获取公钥的key
	 */
	private static final String PUBLIC_KEY = "RSAPublicKey";

	/**
	 * 获取私钥的key
	 */
	private static final String PRIVATE_KEY = "RSAPrivateKey";

	/**
	 * RSA最大加密明文大小
	 */
	private static final int MAX_ENCRYPT_BLOCK = 117;

	/**
	 * RSA最大解密密文大小
	 */
	private static final int MAX_DECRYPT_BLOCK = 128;

	/**
	 * 生成密钥对(公钥和密钥)
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> getKenPair() throws Exception {
		// 指定加密算法
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
		keyPairGen.initialize(1024);
		// 获取密钥对
		KeyPair keyPair = keyPairGen.generateKeyPair();
		// 获取公钥
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
		// 获取私钥
		RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
		Map<String, Object> keyMap = new HashMap<String, Object>();
		keyMap.put(PUBLIC_KEY, publicKey);
		keyMap.put(PRIVATE_KEY, privateKey);
		return keyMap;
	}

	/**
	 * 用私钥对信息生成数字签名
	 * 
	 * @param data
	 * @param privateKey
	 * @return
	 * @throws Exception
	 */
	public static String sign(byte[] data, String privateKey) throws Exception {
		// Base64解码
		byte[] keyBytes = Base64.decodeBase64(privateKey);
		// PKCS编码对象
		PKCS8EncodedKeySpec pkcs8Encode = new PKCS8EncodedKeySpec(keyBytes);
		// 算法工厂
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		// 密钥对象
		PrivateKey privateKeyObj = keyFactory.generatePrivate(pkcs8Encode);
		// 数字签名对象
		Signature signture = Signature.getInstance(SIGNATURE_ALGORITHM);
		// 密钥签名
		signture.initSign(privateKeyObj);
		// 加密
		signture.update(data);
		return Base64.encodeBase64String(signture.sign());
	}

	/**
	 * 用公钥校验签名
	 * 
	 * @param data
	 * @param publicKey
	 * @param sign
	 * @return
	 * @throws Exception
	 */
	public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
		// 解码
		byte[] keyBytes = Base64.decodeBase64(publicKey);
		// X509编码对象
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
		// 算法工厂
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		// 公钥对象
		PublicKey publicKeyObj = keyFactory.generatePublic(keySpec);
		// 数字签名对象
		Signature signture = Signature.getInstance(SIGNATURE_ALGORITHM);
		// 公钥初始化
		signture.initVerify(publicKeyObj);
		signture.update(data);
		return signture.verify(Base64.decodeBase64(sign));
	}

	/**
	 * 私钥解密
	 * 
	 * @param encryptedData
	 * @param privateKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptedByPirvateKey(byte[] encryptedData, String privateKey) throws Exception {
		// Base64解码
		byte[] keyBytes = Base64.decodeBase64(privateKey);
		// PKCS8编码对象
		PKCS8EncodedKeySpec pkcs8EncodeSpec = new PKCS8EncodedKeySpec(keyBytes);
		// 算法工厂
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key privateK = keyFactory.generatePrivate(pkcs8EncodeSpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, privateK);
		int inputLen = encryptedData.length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offset = 0;
		byte[] cache;
		int i = 0;
		// 对数据进行分段解密
		while (inputLen - offset > 0) {
			if (inputLen - offset > MAX_DECRYPT_BLOCK) {
				cache = cipher.doFinal(encryptedData, offset, MAX_DECRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(encryptedData, offset, inputLen - offset);
			}
			out.write(cache, 0, cache.length);
			i++;
			offset = i * MAX_DECRYPT_BLOCK;
		}
		byte[] decrytedData = out.toByteArray();
		out.close();
		return decrytedData;
	}

	/**
	 * 公钥解密
	 * 
	 * @param encryptedData
	 * @param publicKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptedByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
		// Base64解码
		byte[] keyBytes = Base64.decodeBase64(publicKey);
		// X509编码对象
		X509EncodedKeySpec x509EncodeSpec = new X509EncodedKeySpec(keyBytes);
		// 算法工厂
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		// 公钥key
		Key publicKeyObj = keyFactory.generatePublic(x509EncodeSpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, publicKeyObj);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int inputLen = encryptedData.length;
		int offset = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段解密
		while (inputLen - offset > 0) {
			if (inputLen - offset > MAX_DECRYPT_BLOCK) {
				cache = cipher.doFinal(encryptedData, offset, MAX_DECRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(encryptedData, offset, inputLen - offset);
			}
			out.write(cache, 0, cache.length);
			i++;
			offset = i * MAX_DECRYPT_BLOCK;
		}
		byte[] decryptedData = out.toByteArray();
		out.close();
		return decryptedData;
	}

	/**
	 * 公钥加密
	 * 
	 * @param data
	 * @param publicKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptedDataByPublicKey(byte[] data, String publicKey) throws Exception {
		// 解码
		byte[] keyBytes = Base64.decodeBase64(publicKey);
		X509EncodedKeySpec x509 = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key publicKeyObj = keyFactory.generatePublic(x509);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, publicKeyObj);
		int inputLen = data.length;
		int offset = 0;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int i = 0;
		byte[] cache;
		// 对数据进行分段加密
		while (inputLen - offset > 0) {
			if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
				cache = cipher.doFinal(data, offset, MAX_ENCRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(data, offset, inputLen - offset);
			}
			out.write(cache, 0, cache.length);
			i++;
			offset = i * MAX_ENCRYPT_BLOCK;
		}
		byte[] encryptedData = out.toByteArray();
		out.close();
		return encryptedData;
	}

	/**
	 * 私钥加密
	 * 
	 * @param data
	 * @param privateKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptedDataByPrivateKey(byte[] data, String privateKey) throws Exception {
		byte[] keyBytes = Base64.decodeBase64(privateKey);
		PKCS8EncodedKeySpec pkcs8Spec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key privateKeyObj = keyFactory.generatePrivate(pkcs8Spec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, privateKeyObj);
		int inputLen = data.length;
		int offset = 0;
		int i = 0;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] cache;
		while (inputLen - offset > 0) {
			if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
				cache = cipher.doFinal(data, offset, MAX_ENCRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(data, offset, inputLen - offset);
			}
			out.write(cache, 0, cache.length);
			i++;
			offset = i * MAX_ENCRYPT_BLOCK;
		}
		byte[] encryptedData = out.toByteArray();
		out.close();
		return encryptedData;
	}

	/**
	 * 获取私钥
	 * 
	 * @param map
	 * @return
	 */
	public static String getPrivateKey(Map<String, Object> map) throws Exception {
		Key key = (Key) map.get(PRIVATE_KEY);
		return Base64.encodeBase64String(key.getEncoded());
	}

	/**
	 * 获取公钥
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static String getPublicKey(Map<String, Object> map) throws Exception {
		Key key = (Key) map.get(PUBLIC_KEY);
		return Base64.encodeBase64String(key.getEncoded());
	}

}

AES加密工具类

package AES;

import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


/**
 * AES加密工具类
 * 
 * @author 微软
 *
 */
public class AESUtil {

	static final String AES_ENCRYPT = "AES";

	static final String CHARTSETS = "UTF-8";

	/**
	 * AES加密
	 * 
	 * @param content
	 * @param password
	 * @return
	 */
	public static byte[] encrypt(String content, String password) {
		try {
			// 初始化
			KeyGenerator kgen = KeyGenerator.getInstance(AES_ENCRYPT);
			// 初始化长度,以及密码
			kgen.init(128, new SecureRandom(password.getBytes(CHARTSETS)));
			// 生成key
			SecretKey secretKey = kgen.generateKey();
			// 根据用户密码生成密钥
			byte[] cnCodeFormat = secretKey.getEncoded();
			// 转换为AES密钥
			SecretKeySpec key = new SecretKeySpec(cnCodeFormat, AES_ENCRYPT);
			// 初始化加密器
			Cipher cipher = Cipher.getInstance(AES_ENCRYPT);
			byte[] byteContent = content.getBytes(CHARTSETS);
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] result = cipher.doFinal(byteContent);// 加密
			// AES加密后的密文必须Base64编码
			return util.Base64.encode(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * AES解密
	 * 
	 * @param content
	 * @param password
	 * @return
	 */
	public static byte[] decrypt(byte[] content, String password) {
		try {
			// 初始化
			KeyGenerator kgen = KeyGenerator.getInstance(AES_ENCRYPT);
			// 128长度
			kgen.init(128, new SecureRandom(password.getBytes(CHARTSETS)));
			// 根据密码生成密钥
			SecretKey key = kgen.generateKey();
			byte[] keyContent = key.getEncoded();
			// 转换为AES密钥
			SecretKeySpec keySpec = new SecretKeySpec(keyContent, AES_ENCRYPT);
			// 创建密码器
			Cipher cipher = Cipher.getInstance(AES_ENCRYPT);
			cipher.init(Cipher.DECRYPT_MODE, keySpec);
			// 将需要解密的内容进行Base64解码
			byte[] result = cipher.doFinal(util.Base64.decode(content));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

Base64工具类

package util;

public class Base64 {

	static final String chartset = "UTF-8";

	/**
	 * 编码
	 * 
	 * @param content
	 * @return
	 */
	public static byte[] encode(byte[] content) {
		byte[] cons = null;
		try {
			cons = org.apache.commons.codec.binary.Base64.encodeBase64(content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cons;
	}

	/**
	 * 解码
	 * 
	 * @param content
	 * @return
	 */
	public static byte[] decode(byte[] content) {
		byte[] cons = null;
		try {
			cons = org.apache.commons.codec.binary.Base64.decodeBase64(content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cons;
	}
}

测试代码示例:

package DoubleEncrypt;

import java.util.Map;
import AES.AESUtil;
import RSA.RSAUtils;
import util.Base64;

/**
 * 双重加密(RSA+AES) AES加密密钥 RSA加密数据
 * 
 * @author 微软
 *
 */
public class DoubleTest {

	// AES密码
	static final String password = "123";

	// RSA公钥、密钥
	static String publicKey = "";

	static String privateKey = "";

	static {
		try {
			// 生成公钥、密钥对
			Map<String, Object> map = RSAUtils.getKenPair();
			publicKey = RSAUtils.getPublicKey(map);
			privateKey = RSAUtils.getPrivateKey(map);
			Thread.sleep(100);
			System.out.println("生成公钥:");
			System.err.println(publicKey);
			Thread.sleep(500);
			System.out.println("生成密钥:");
			System.err.println(privateKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		try {
			Thread.sleep(1000);
			String source = "HelloWorld";
			System.out.println("原内容:" + source);
			// 对密钥进性对称加密,这样发给对方时,对方必须解密才可以拿到真正的密钥
			System.out.println("---------------------------AES加密阶段-------------------------------");
			publicKey = new String(AESUtil.encrypt(publicKey, password), "UTF-8");
			System.out.println("AES加密后的公钥:");
			System.err.println(publicKey);
			// 拿我方私钥加密
			byte[] dataBypri = Base64.encode(RSAUtils.encryptedDataByPrivateKey(source.getBytes("UTF-8"), privateKey));
			System.out.println("加密后的内容为:");
			System.err.println(new String(dataBypri, "UTF-8"));
			System.out.println("--------------------------AES解密阶段--------------------------------");
			// 对方拿到公钥后,需要进行AES解密.
			byte[] tempPubs = AESUtil.decrypt(publicKey.getBytes("UTF-8"), password);
			System.out.println("AES解密后的公钥");
			String a = new String(tempPubs, "UTF-8");
			System.err.println(a);
			// Base64解码密文
			byte[] tempBytes = Base64.decode(dataBypri);
			// 根据公钥进行解密
			byte[] dataByPub = RSAUtils.decryptedByPublicKey(tempBytes, a);
			Thread.sleep(100);
			System.out.println("解密后的数据内容为:");
			System.err.println(new String(dataByPub, "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

控制台输出:

猜你喜欢

转载自blog.csdn.net/P923284735/article/details/83655213