RSA非对称加密算法

1.概述:在对称加密算法中,加密和解密都使用的是同一把密钥,特点为加密的速度和效率高,但由于双方使用的都是同样的密钥,安全性得不到保证,非对称加密算法加密和解密使用的是不同的密钥,当使用A同学的公钥加密后得到的密文,只有A同学使用自己的私钥才能解密,如果别人知道了他的公钥,也不能计算出对应的私钥,所以使用非对称加密算法时,需要使用对方的公钥进行加密。

2.案例实现

A同学需要给B同学传送消息,A同学需要先向B同学索要B同学的公钥,使用B同学的公钥进行加密操作,当解密时,只有B同学使用的自己的私钥才可以进行解密。

Student类:

class Student{
    //名字
	private String name;
    //公钥
	private PublicKey publicKey;
    //私钥
	private PrivateKey privateKey;
	
	public Student(String name) {
		this.name=name;
		try {
            //根据非对称加密算法名称创建密钥对生成器
			KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");
			keyPairGenerator.initialize(1024);
            //生成密钥对
			KeyPair keyPair=keyPairGenerator.generateKeyPair();
			this.publicKey=keyPair.getPublic();
			this.privateKey=keyPair.getPrivate();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public PublicKey getPublicKey() {
		return publicKey;
	}
	public void setPublicKey(PublicKey publicKey) {
		this.publicKey = publicKey;
	}
	public PrivateKey getPrivateKey() {
		return privateKey;
	}
	public void setPrivateKey(PrivateKey privateKey) {
		this.privateKey = privateKey;
	}
	
    //使用对方的公钥进行加密操作
	public byte[] encrypt(byte[] message,byte[] publicKey) throws GeneralSecurityException {
        //通过对方公钥的字节数组恢复公钥对象
		X509EncodedKeySpec publiSpec=new X509EncodedKeySpec(publicKey);
		KeyFactory keyFactory=KeyFactory.getInstance("RSA");
		PublicKey pubKey=keyFactory.generatePublic(publiSpec);
		
        
		Cipher cipher=Cipher.getInstance("RSA");
		//设置cipher的操作模式为加密以及公钥
		cipher.init(Cipher.ENCRYPT_MODE, pubKey);
		
		return cipher.doFinal(message);
	}
	
    //使用自己的私钥进行解密
	public byte[] decrypt(byte[] message) throws GeneralSecurityException {
		Cipher cipher=Cipher.getInstance("RSA");
		//设置cipher的操作模式为解密以及私钥
		cipher.init(Cipher.DECRYPT_MODE, this.privateKey);
		
		return cipher.doFinal(message);
	}
	
}

在Student类的构造方法中,我们使用KeyPairGenerator密钥生成器生成了基于RSA加密算法的一对密钥,分别作为该对象的公钥和私钥,在定义的encrypt()加密方法中,需要使用到对方的公钥对消息进行加密,在decrypt()解密方法中,使用自己的私钥进行解密,加密方法表示该对象需要发送消息给对方,所以必须使用对方的公钥加密,解密方法表示该对象收到了对方使用它的公钥进行解密的密文,所以必须使用自己的私钥进行解密。

public static void main(String[] args) throws GeneralSecurityException {
		String message="我本将心向明月";
        //同学A
		Student a=new Student("A");
        //同学B
		Student b=new Student("B");
		
        //使用B同学的公钥进行加密
		byte[] encrypted=a.encrypt(message.getBytes(), b.getPublicKey().getEncoded());
		System.out.println("加密结果:"+String.format("%x", new BigInteger(1,encrypted)));
        //B同学使用自己的私钥进行解密
		byte[] decrypted=b.decrypt(encrypted);
		System.out.println("解密结果:"+new String(decrypted));
	}

在程序中,我们创建了两个Student类对象分别代表A同学和B同学,A同学想给B同学发送消息,就需要使用B同学的公钥进行加密,只有B同学自己的私钥才可以解密。

运行结果: 

3.对称加密和非对称加密的区别

对称加密算法加密和解密使用的是同一把密钥,安全性不能保证,但加密速度快,加密效率高,非对称加密算法加密和解密不是同一把密钥,安全性好,但加密速度和加密效率相比对称加密算法要差很多。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值