前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RSA的java实现

RSA的java实现

作者头像
东营浪人
发布2019-09-05 17:56:27
7040
发布2019-09-05 17:56:27
举报
文章被收录于专栏:浪人聊编程浪人聊编程

RSA非对称加密整理

辅助类

代码语言:javascript
复制
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;
}




}

工具类

代码语言:javascript
复制
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加密与解密的艺术

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 辅助类
  • 工具类
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档