Java 密碼學 - 資料解密



您可以使用javax.crypto包的Cipher類解密加密資料。請按照以下步驟使用Java解密給定資料。

步驟1:建立KeyPairGenerator物件

KeyPairGenerator類提供getInstance()方法,該方法接受一個表示所需金鑰生成演算法的String變數,並返回一個生成金鑰的KeyPairGenerator物件。

使用getInstance()方法建立KeyPairGenerator物件,如下所示。

//Creating KeyPair generator object
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DSA");

步驟2:初始化KeyPairGenerator物件

KeyPairGenerator類提供一個名為initialize()的方法,此方法用於初始化金鑰對生成器。此方法接受一個整數,表示金鑰大小。

使用initialize()方法初始化上一步中建立的KeyPairGenerator物件,如下所示。

//Initializing the KeyPairGenerator
keyPairGen.initialize(2048);

步驟3:生成KeyPairGenerator

您可以使用KeyPairGenerator類的generateKeyPair()方法生成KeyPair。使用此方法生成金鑰對,如下所示。

//Generate the pair of keys
KeyPair pair = keyPairGen.generateKeyPair();

步驟4:獲取公鑰

您可以使用getPublic()方法從生成的KeyPair物件中獲取公鑰,如下所示。

//Getting the public key from the key pair
PublicKey publicKey = pair.getPublic();

步驟5:建立Cipher物件

Cipher類的getInstance()方法接受一個表示所需轉換的String變數,並返回一個實現給定轉換的Cipher物件。

使用getInstance()方法建立Cipher物件,如下所示。

//Creating a Cipher object
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

步驟6:初始化Cipher物件

Cipher類的init()方法接受兩個引數:

  • 一個整數引數,表示操作模式(加密/解密)
  • 表示公鑰的Key物件

使用init()方法初始化Cipher物件,如下所示。

//Initializing a Cipher object
cipher.init(Cipher.ENCRYPT_MODE, publicKey);

步驟7:將資料新增到Cipher物件

Cipher類的update()方法接受一個表示要加密資料的位元組陣列,並使用給定的資料更新當前物件。

透過將資料以位元組陣列的形式傳遞給update()方法來更新已初始化的Cipher物件,如下所示。

//Adding data to the cipher
byte[] input = "Welcome to Tutorialspoint".getBytes();	  
cipher.update(input);

步驟8:加密資料

Cipher類的doFinal()方法完成加密操作。因此,請使用此方法完成加密,如下所示。

//Encrypting the data
byte[] cipherText = cipher.doFinal();

步驟9:初始化用於解密的Cipher物件

要解密前面步驟中加密的密文,您需要將其初始化為解密模式。

因此,透過傳遞引數Cipher.DECRYPT_MODE和PrivateKey物件來初始化Cipher物件,如下所示。

//Initializing the same cipher for decryption
cipher.init(Cipher.DECRYPT_MODE, pair.getPrivate());

步驟10:解密資料

最後,使用doFinal()方法解密加密文字,如下所示。

//Decrypting the text
byte[] decipheredText = cipher.doFinal(cipherText);

示例

以下Java程式接受使用者的文字,使用RSA演算法對其進行加密,並列印給定文字的密文,解密密文並再次列印解密後的文字。

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Signature;

import javax.crypto.Cipher;

public class CipherDecrypt {
   public static void main(String args[]) throws Exception{
	   //Creating a Signature object
      Signature sign = Signature.getInstance("SHA256withRSA");
      
      //Creating KeyPair generator object
      KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
      
      //Initializing the key pair generator
      keyPairGen.initialize(2048);
      
      //Generate the pair of keys
      KeyPair pair = keyPairGen.generateKeyPair();   
      
      //Getting the public key from the key pair
      PublicKey publicKey = pair.getPublic();  

      //Creating a Cipher object
      Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

      //Initializing a Cipher object
      cipher.init(Cipher.ENCRYPT_MODE, publicKey);
	  
      //Add data to the cipher
      byte[] input = "Welcome to Tutorialspoint".getBytes();	  
      cipher.update(input);
	  
      //encrypting the data
      byte[] cipherText = cipher.doFinal();	 
      System.out.println( new String(cipherText, "UTF8"));

      //Initializing the same cipher for decryption
      cipher.init(Cipher.DECRYPT_MODE, pair.getPrivate());
      
      //Decrypting the text
      byte[] decipheredText = cipher.doFinal(cipherText);
      System.out.println(new String(decipheredText));
   }
}

輸出

以上程式生成以下輸出:

Encrypted Text:
]/[?F3?D?p
v?w?!?H???^?A??????P?u??FA?
?
???_?? ???_jMH-??>??OP?'?j?_?n`
?_??'`????o??_GL??g???g_f?????f|???LT?|?Vz_TDu#??\?<b,,?$C2???Bq?#?lDB`??g,^??K?_?v???`}
?;LX?a?_5e???#???_?6?/B&B_???^?__Ap^#_?q?IEh????_?,??*??]~_?_?D?
_y???lp??a?P_U{

Decrypted Text:
Welcome to Tutorialspoint
廣告
© . All rights reserved.