RSA加密算法的java实现

package rsa;

import java.security.*;
import java.security.interfaces.*;
import javax.crypto.*;

public class Test {
    
    protected static RSAPrivateKey privateKey;
    protected static RSAPublicKey publicKey;
    protected static byte[] resultBytes;
    
    public Test(){
        try{
            String message = "广东省广州市越秀区";
            
//            Test p = new Test();
            System.out.println("明文是" + message);
            
            //生成公钥和私钥对,基于RSA算法生成对象
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
            
            //初始化密钥对生成器,密钥大小为1024位
            keyPairGen.initialize(1024);
            
            //生成一个密钥对,保存在keyPair中
            KeyPair keyPair = keyPairGen.generateKeyPair();
            
            //得到私钥和公钥
            privateKey =(RSAPrivateKey) keyPair.getPrivate();
            publicKey = (RSAPublicKey)keyPair.getPublic();
            
            
//            System.out.println(privateKey.toString());
            
            //用公钥加密
            byte[] srcBytes = message.getBytes();
            resultBytes = Test.encrypt(publicKey, srcBytes);
            String result = new String(resultBytes);
            System.out.println("用公钥加密后密文是:" + result);
            
//            return privateKey;
//            //用私钥解密
//            byte[] decBytes = Test.decrypt(privateKey,resultBytes);
//            String dec = new String(decBytes);
//            System.out.println("用私钥加密后的结果是:" + dec);
        }catch(Exception e){
            e.printStackTrace();
        }
//        return null;
    }
    
    protected static byte[] encrypt(RSAPublicKey publicKey,byte[] srcBytes){
        if(publicKey != null){            
            try{
                //Cipher负责完成加密或解密工作,基于RSA
                Cipher cipher = Cipher.getInstance("RSA");
                
                //根据公钥,对Cipher对象进行初始化
                cipher.init(Cipher.ENCRYPT_MODE, publicKey);
                
                //加密,结果保存进resultBytes,并返回
                byte[] resultBytes = cipher.doFinal(srcBytes);
                return resultBytes;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }
    
    protected static byte[] decrypt(RSAPrivateKey privateKey,byte[] encBytes){
        if(privateKey != null){
            try{
                Cipher cipher = Cipher.getInstance("RSA");
                
                //根据私钥对Cipher对象进行初始化
                cipher.init(Cipher.DECRYPT_MODE, privateKey);
                
                //解密并将结果保存进resultBytes
                byte[] decBytes = cipher.doFinal(encBytes);
                return decBytes;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }
}

package rsa;

public class PrivateKeyTest {
    public static void main(String[] args){
        Test test = new Test();
        
//        System.out.println(Test.privateKey);
//        String msg = new String(Test.resultBytes);
        
        byte[] decBytes = Test.decrypt(Test.privateKey, Test.resultBytes);
        String dec = new String(decBytes);
        
        System.out.println("用私钥加密后的结果是:" + dec);
    }
}

  在一个项目中,要对二维码进行加密,这是测试RSA加密算法的模块。由于刚接触加密算法,很多细节还不清楚。通过这个测试搞清楚了几点,一是每次加密产生的公钥和私钥都是不同。

  对Java的一些基础知识也有了补充。在定义了静态变量后,用类名调用,并且在之后使用这一静态变量的时候不要再重新定义,不然会产生空指针问题。

 

转载于:https://www.cnblogs.com/waynelin/p/5774209.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值