RSA的java实现

RSA非对称加密整理

辅助类

package net.yun10000.zf.util;


import Java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;


public class RSAKeys {

private RSAPublicKey publicKey;

private RSAPrivateKey privateKey;


public RSAPublicKey getPublicKey() {
return publicKey;
}


public RSAPrivateKey getPrivateKey() {
return privateKey;
}


public RSAKeys(RSAPublicKey publicKey, RSAPrivateKey privateKey) {

this.publicKey = publicKey;
this.privateKey = privateKey;
}




}


工具类

<pre name="code" class="java">package net.yun10000.zf.util;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.crypto.Cipher;

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

/**
 * rsa工具类
 * @author ydy
 * */
public class RSAUtil {
	
	private  static final String SIGN_SHA1="SHA1WithRSA"; 
	/**
	 * 初始化rsa钥匙
	 * 
	 * */
	public static  RSAKeys initkeys(){
		
		try {
			//rsa工厂
			KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
			//长度
			keyPairGenerator.initialize(1024);
			KeyPair keyPair = keyPairGenerator.generateKeyPair();
			//公钥
			RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
			//私钥
			RSAPrivateKey rsaPrivateKey=(RSAPrivateKey) keyPair.getPrivate();
			RSAKeys rsaKeys=new RSAKeys(rsaPublicKey, rsaPrivateKey);
			return rsaKeys;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	/**
	 * 获取公钥
	 * @param publicKeyStr
	 * */
	public static RSAPublicKey getPublicKey(String publicKeyStr){
		try {
			KeyFactory keyFactory=KeyFactory.getInstance("RSA");
			PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(Base64.decodeBase64(publicKeyStr));
			RSAPublicKey rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(pkcs8EncodedKeySpec);
			return rsaPublicKey;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取私钥
	 * @param privateKeyStr
	 * */
	public static RSAPrivateKey getPrivateKey(String privateKeyStr){
		try {
			KeyFactory keyFactory=KeyFactory.getInstance("RSA");
			PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyStr));
			RSAPrivateKey privateKey=(RSAPrivateKey) keyFactory.generatePrivate(pkcs8EncodedKeySpec);
			return privateKey;
		} catch (Exception e) {
            e.printStackTrace();
		}
		return null;
	}
	/**
	 * 私钥解密
	 * @param contentBytes
	 * @param privateKey
	 * */
	public static byte[] decrypt(byte[] contentBytes,RSAPrivateKey privateKey){
		try {
			
			Cipher cipher = Cipher.getInstance("RSA");
			cipher.init(Cipher.DECRYPT_MODE, privateKey);
			return cipher.doFinal(contentBytes);
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	/**
	 * 公钥加密
	 * @param contentBytes
	 * @param rsaPublicKey
	 * */
	public static byte[] encrypt(byte[] contentBytes,RSAPublicKey rsaPublicKey){
		try {
			Cipher cipher = Cipher.getInstance("RSA");
			cipher.init(Cipher.ENCRYPT_MODE,rsaPublicKey);
			return cipher.doFinal(contentBytes);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 数字签名
	 * @param contentBytes 待签名数据
	 * @param privateKey 私钥
	 * */
	public static byte[] signSHA1(byte[] contentBytes,RSAPrivateKey privateKey){
		try {
			Signature signature=Signature.getInstance(SIGN_SHA1);
			signature.initSign(privateKey);
			signature.update(contentBytes);
			return signature.sign();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
	/**
	 * 验证数字签名
	 * @param contentBytes 数据
	 * @param signBytes 签名数据
	 * @param publicKey 公钥
	 * */
	public boolean verifySHA1(byte[] contentBytes,byte[] signBytes,RSAPublicKey publicKey){
		try {
			Signature signature=Signature.getInstance(SIGN_SHA1);
			signature.initVerify(publicKey);
			signature.update(contentBytes);
			return signature.verify(signBytes);
		}  catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public static void main(String[] args)  {
		RSAKeys rsakeys = initkeys();
		RSAPublicKey rsaPublicKey = rsakeys.getPublicKey();
		String publicKeyStr=Base64.encodeBase64String(rsaPublicKey.getEncoded());
		System.out.println("公钥");
		System.out.println(publicKeyStr);
		System.out.println("-------------------------------------------------------------------------------------------------");
		
		RSAPrivateKey rsaPrivateKey = rsakeys.getPrivateKey();
		String privateKeyStr = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
		System.out.println("私钥");
		System.out.println(privateKeyStr);
		
		String str="我的测试";
		System.out.println("开始加密");
		byte[] decodeBase64 = Base64.encodeBase64(StringUtils.getBytesUtf8(str));
		//加密
	    byte[] enByte=	encrypt(decodeBase64, rsaPublicKey);
	    //解密
	    byte[] deByte=decrypt(enByte, rsaPrivateKey);
	    byte[] strByte = Base64.decodeBase64(deByte);
	    String strResult=   StringUtils.newString(strByte, "utf-8");
	    System.out.println(strResult);
	    
	
		
		
	}
	
	 

}

参考:java加密与解密的艺术

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值