Java加密篇(3)-RSA

4 篇文章 0 订阅
package com.geom.rsa;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import utils.FileUtils;

public class Rsa 
{
	public static RsaVO createKeyPair()
	{
		return createKeyPair(512);
	}
	
	public static RsaVO createKeyPair( int bit )
	{
		try 
		{
			KeyPairGenerator key = KeyPairGenerator.getInstance("Rsa");
			key.initialize(bit);
			
			KeyPair kp = key.generateKeyPair();
			
			RsaVO vo = new RsaVO();
			
			vo.pubKey = new BASE64Encoder().encode(kp.getPublic().getEncoded());
			vo.privKey = new BASE64Encoder().encode(kp.getPrivate().getEncoded());
			
			return vo;
		}
		catch (NoSuchAlgorithmException e) 
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 公钥加密
	 * @param content
	 * @param publicKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] publicEncrypt(byte[] content, String publicKey) throws Exception
	{
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.ENCRYPT_MODE, string2PublicKey(publicKey));
		byte[] bytes = cipher.doFinal(content);
		return bytes;
	}
	
	/**
	 *  私钥解密
	 * @param content
	 * @param privateKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] privateDecrypt(byte[] content, String privateKey) throws Exception
	{
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, string2PrivateKey(privateKey));
		byte[] bytes = cipher.doFinal(content);
		return bytes;
	}
	
	/**
	 * 私钥加密
	 * @param content
	 * @param privateKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] privateEncrypt2(byte[] content, String privateKey) throws Exception
	{
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.ENCRYPT_MODE, string2PrivateKey(privateKey));
		byte[] bytes = cipher.doFinal(content);
		return bytes;
	}
	
	/**
	 * 公钥解密
	 * @param content
	 * @param publicKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] publicDecrypt2(byte[] content, String publicKey) throws Exception
	{
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, string2PublicKey(publicKey));
		byte[] bytes = cipher.doFinal(content);
		return bytes;
	}
	
	private static PublicKey string2PublicKey(String pubStr) throws Exception
	{
		byte[] keyBytes = new BASE64Decoder().decodeBuffer(pubStr);
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PublicKey publicKey = keyFactory.generatePublic(keySpec);
		return publicKey;
	}
	
	private static PrivateKey string2PrivateKey(String priStr) throws Exception
	{
		byte[] keyBytes = new BASE64Decoder().decodeBuffer(priStr);
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
		return privateKey;
	}
	
	public static void main(String[] args) throws Exception 
	{
		RsaVO vo = Rsa.createKeyPair(1024);
		
		System.out.println(vo.pubKey);
		System.out.println(vo.privKey);
		
		String path = "D:\\Code\\rsa\\";
		FileUtils.writeFile(path + "pub_key", vo.pubKey);
		FileUtils.writeFile(path + "priv_key", vo.privKey);
		
		byte[] bytes = Rsa.publicEncrypt("sdfsdfsdf".getBytes(), vo.pubKey);
		System.out.println(Rsa.privateDecrypt(bytes, vo.privKey));
	}
}
package com.geom.rsa;

public class RsaVO
{
	public String pubKey;
	public String privKey;
}

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值