RSA加密算法

RSA是目前比较流行的一种非对称加密的算法(对称与非对称的区别是加密和解密是否使用的是同一把钥匙),通常采用的是公钥加密,私钥解密;或者是私钥加密,公钥解密

下面附带使用Java进行加密和解密的代码:

package com.qianfeng.gp09.util;

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

import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * RSA加密算法的工具类
 * 解决的问题:
 * X509 <---> PKCS8
 * 1、生成 公钥 私钥
 * 2、公钥加密  私钥解密
 * 3、私钥加密  公钥解密
 * 
 */
public class RSAUtil {
    /**
     * 私钥
     */
    private static String privateKeyString;
    /**
     * 公钥
     */
    private static String publicKeyString;

    /**
     * 随机生成一对儿密钥
     */
    public static void genKeyPair(){
        try {
            //生成RSA密钥对的生成器
            KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");
            //初始化密钥对生成器,密钥大小为512
            keyPairGenerator.initialize(512,new SecureRandom());
            //生成一个密钥对
            KeyPair keyPair=keyPairGenerator.generateKeyPair();
            //获取公钥
            RSAPrivateKey privateKey= (RSAPrivateKey) keyPair.getPrivate();
            //获取私钥
            RSAPublicKey publicKey= (RSAPublicKey) keyPair.getPublic();
            //以base64的形式存储
            privateKeyString = Base64.encodeBase64String(privateKey.getEncoded());
            publicKeyString = Base64.encodeBase64String(publicKey.getEncoded());

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        genKeyPair();
        System.out.println("私钥是:"+privateKeyString);
        System.out.println("私钥的长度是:"+privateKeyString.length());
        System.out.println("公钥是:"+publicKeyString);
        String info="好程序员09";
        //info->s1
        String s1 = encryptPub(info, publicKeyString);
        System.out.println("公钥加密之后为:"+s1);
        //s1->s2  s2 info
        String s2 = decryptPri(s1,privateKeyString);
        System.out.println("私钥解密之后为:"+s2);
        //info->s3
        String s3 = encryptPri(info,privateKeyString);
        System.out.println("私钥加密之后为:"+s3);
        //s3->s4  s4 info
        String s4 = decryptPub(s3,publicKeyString);
        System.out.println("公钥解密之后为:"+s4);

    }

    /**
     * 公钥加密
     */
    public static String encryptPub(String str,String publicKey){
        try {
            //把公钥由base64还原为字节数组
            byte [] bytes=Base64.decodeBase64(publicKey);
            //把公钥由字节数组,还原为公钥对象
            RSAPublicKey pubKey= (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));
            //用于加密的对象
            Cipher cipher=Cipher.getInstance("RSA");
            //指定密钥
            cipher.init(Cipher.ENCRYPT_MODE,pubKey);
            //做加密这件事
            String result=Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
            return result;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 私钥解密
     */
    public static String decryptPri(String str,String privateKey){
        try{
            //把私钥还原为字节数组
            byte[] bytes = Base64.decodeBase64(privateKey);
            //把私钥由字节数组还原为对象
            RSAPrivateKey priKey= (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(bytes));
            //把待解密的密文,还原为字节数组
            byte [] inputByte=Base64.decodeBase64(str.getBytes("UTF-8"));
            //RSA解密对象
            Cipher cipher=Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE,priKey);
            //做解密这件事
            String result=new String(cipher.doFinal(inputByte));
            return result;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 私钥加密
     */
    public static String encryptPri(String str,String privateKey){
        try{
            //把私钥由base64还原为字节数组
            byte[] bytes = Base64.decodeBase64(privateKey);

            //把私钥由字节数组还原为私钥对象
            RSAPrivateKey priKey= (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(bytes));
            //加密对象
            Cipher cipher=Cipher.getInstance("RSA");
            //指定密钥
            cipher.init(Cipher.ENCRYPT_MODE,priKey);
            //进行加密
            String result=Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 公钥解密
     */
    public static String decryptPub(String str,String publicKey){
        try{
            //拿到密文对应的字节数组
            byte [] inputByte=Base64.decodeBase64(str.getBytes("UTF-8"));
            //还原公钥
            byte [] keyByte=Base64.decodeBase64(publicKey);
            RSAPublicKey pubKey= (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyByte));
            //RSA解密对象
            Cipher cipher=Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE,pubKey);
            //进行解密
            String result=new String(cipher.doFinal(inputByte));
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值