java实现RSA非对称加密算法 对数据进行加密【附代码】

今天看了一下RSA非对称加密算法  上网搜索了许多资料 都没有找到比较详细的讲解和实现代码 


后来和朋友交流了下数据交互 突然发现了数据加密的重要性 ,决定自己来写一篇关于非对称加密的文章 附上代码 看看怎么一步一步的实现rsa加密的


1、需要引入的包


import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Date;
import java.util.concurrent.ExecutionException;

import javax.crypto.Cipher;
import javax.xml.crypto.Data;

import sun.misc.BASE64Encoder;

2、编写Rsa类

public class Rsa {
	
	
}

3、写入rsa算法需要的方法

public class Rsa {
    //生成密钥对  
    public static KeyPair genKeyPair(int keyLength) throws Exception{  
        KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");  
        keyPairGenerator.initialize(1024);        
        return keyPairGenerator.generateKeyPair();  
    }  
      
    //公钥加密  
    public static byte[] encrypt(byte[] content, PrivateKey publicKey) throws Exception{  
        Cipher cipher=Cipher.getInstance("RSA");//java默认"RSA"="RSA/ECB/PKCS1Padding"  
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
        return cipher.doFinal(content);  
    }  
      
    //私钥解密  
    public static byte[] decrypt(byte[] content, PublicKey privateKey) throws Exception{  
        Cipher cipher=Cipher.getInstance("RSA");  
        cipher.init(Cipher.DECRYPT_MODE, privateKey);  
        return cipher.doFinal(content);  
    }  
	
}

4、方法有了之后就可以开始进行操作打了 首先要有个概念 公匙加密 私匙解密 或者 私匙加密 公匙解密

首先准备一个需要加密的数据

    public static String data="{date:'hellow',time:'2017-10-29'}";

5、有了数据之后准备公钥秘钥

//公钥秘钥
		PublicKey publicKEY =keyPair.getPublic();
		PrivateKey privateKey =keyPair.getPrivate();


6、有了公钥秘钥之后就可以进行加密解密了
//公钥加密
		byte[] dataCode = encrypt(data.getBytes(),publicKEY);
		System.out.println(base64.encode(dataCode));


//秘钥解密
		byte[] dateprivate =decrypt(dataCode,privateKey);
		System.out.println(new String(dateprivate));


7、附上完整代码

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Date;
import java.util.concurrent.ExecutionException;

import javax.crypto.Cipher;
import javax.xml.crypto.Data;

import sun.misc.BASE64Encoder;


public class Rsa {
	
	public static String data="{date:'hellow',time:'2017-10-29'}";  

	
	public static void main(String[] args) throws Exception {
		//base64编码对象
		final BASE64Encoder base64 =new BASE64Encoder();
		KeyPair keyPair=genKeyPair(1024);
		
		//公钥秘钥
		PublicKey publicKEY =keyPair.getPublic();
		PrivateKey privateKey =keyPair.getPrivate();
		
		//公钥加密
		byte[] dataCode = encrypt(data.getBytes(),publicKEY);
		System.out.println(base64.encode(dataCode));
		System.out.println("");
		
		//秘钥解密
		byte[] dateprivate =decrypt(dataCode,privateKey);
		System.out.println(new String(dateprivate));
		
		
	}

	
	//生成密钥对  
    public static KeyPair genKeyPair(int keyLength) throws Exception{  
        KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");  
        keyPairGenerator.initialize(1024);        
        return keyPairGenerator.generateKeyPair();  
    }  
      
    //公钥加密  
    public static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception{  
        Cipher cipher=Cipher.getInstance("RSA");//java默认"RSA"="RSA/ECB/PKCS1Padding"  
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
        return cipher.doFinal(content);  
    }  
      
    //私钥解密  
    public static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception{  
        Cipher cipher=Cipher.getInstance("RSA");  
        cipher.init(Cipher.DECRYPT_MODE, privateKey);  
        return cipher.doFinal(content);  
    }  
}


8、运行结果




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值