首页 > 其他 > 详细

RSA加密

时间:2017-11-24 16:37:40      阅读:192      评论:0      收藏:0      [点我收藏+]
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加密

原文:http://www.cnblogs.com/kongxc/p/7890948.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!