栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

RSA加密算法

Java 更新时间:发布时间: 百科书网 趣学号

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;


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;
    }
}

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/956396.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号