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的一些基礎知識也有了補充。在定義了靜態變量后,用類名調用,並且在之后使用這一靜態變量的時候不要再重新定義,不然會產生空指針問題。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM