zoukankan      html  css  js  c++  java
  • RSA加密

    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);
        }
    }
  • 相关阅读:
    Java在Web开发语言上败给了PHP(转)
    很开心收到了Andreas Loew发给我的注册key
    Cocos2d-x开发移植到安卓平台横竖屏设置及相关
    学习实战二:利用Cocos2d-x写的第一个游戏
    Cocos2d-x项目移植到安卓平台易出现的错误
    cocos2d-x帧动画实现(写下备忘)
    cocos2d-x学习遇到的问题
    C++指针的管理
    Win7开自带的虚拟WIFI
    【SICP练习】21 练习1.27
  • 原文地址:https://www.cnblogs.com/kongxc/p/7890948.html
Copyright © 2011-2022 走看看