JAVA加密算法RSA相关代码实现

RSA

        是一种非对称加密算法(即加密和解密使用的是不同的密钥,我们将它们称为公钥私钥)非对称加密的速度慢安全性高

       

package com.test.RSA;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RSATest {
    public static void main(String[] args) throws Exception {
        //生成密钥
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(512);
        KeyPair keyPair = kpg.generateKeyPair();
        //获取公钥
        byte[] publicKey = keyPair.getPublic().getEncoded();
        //获取私钥
        byte[] privateKey = keyPair.getPrivate().getEncoded();
        //定义明文
        String clearText = "hello world";

        //使用公钥加密
        byte[] cipherPublicText = encryptPublic(publicKey, clearText);
        System.out.println("公钥加密后:"+Base64.getEncoder().encodeToString(cipherPublicText));
        //使用私钥解密
        byte[] clearPrivateText = decryptPrivate(privateKey,cipherPublicText);
        System.out.println("私钥解密后:"+new String(clearPrivateText));


        //使用私钥加密
        byte[] cipherPrivateText = encryptPrivate(privateKey,clearText);
        System.out.println("私钥加密后:"+Base64.getEncoder().encodeToString(cipherPrivateText));
        //使用公钥解密
        byte[] clearPublicText = decryptPublic(publicKey,cipherPrivateText);
        System.out.println("公钥解密后:"+new String(clearPublicText));
    }

    private static byte[] decryptPublic(byte[] publicKey, byte[] cipherPrivateText) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey key = keyFactory.generatePublic(new X509EncodedKeySpec(publicKey));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE,key);
        byte[] clearPublicText = cipher.doFinal(cipherPrivateText);
        return  clearPublicText;
    }

    private static byte[] encryptPrivate(byte[] privateKey, String clearText) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey key = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE,key);
        byte[] cipherPrivateText = cipher.doFinal(clearText.getBytes());
        return cipherPrivateText;
    }

    private static byte[] decryptPrivate(byte[] privateKey, byte[] cipherPublicText) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey key = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE,key);
        byte[] clearPrivateText = cipher.doFinal(cipherPublicText);
        return clearPrivateText;
    }

    //使用公钥对明文进行加密
    private static byte[] encryptPublic(byte[] publicKey, String clearText) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey key = keyFactory.generatePublic(new X509EncodedKeySpec(publicKey));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE,key);
        byte[] cipherPublicText = cipher.doFinal(clearText.getBytes());
        return cipherPublicText;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值