Java 加密 - 資料加密



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

步驟 1:建立 KeyPairGenerator 物件

**KeyPairGenerator** 類提供 **getInstance()** 方法,該方法接受一個表示所需金鑰生成演算法的字串變數,並返回一個生成金鑰的 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()** 方法接受一個表示所需轉換的字串變數,並返回一個實現給定轉換的 Cipher 物件。

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

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

步驟 6:初始化 Cipher 物件

**Cipher** 類的 **init()** 方法接受兩個引數:一個表示操作模式(加密/解密)的整數引數和一個表示公鑰的 Key 物件。

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

//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();

示例

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

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

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;

public class CipherSample {
   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);
      
      //Generating the pair of keys
      KeyPair pair = keyPairGen.generateKeyPair();      
	
      //Creating a Cipher object
      Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        
      //Initializing a Cipher object
      cipher.init(Cipher.ENCRYPT_MODE, pair.getPublic());
	  
      //Adding 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"));
   }
}

輸出

以上程式生成以下輸出:

Encrypted Text: 
"???:]J_?]???;Xl??????*@??u???r??=T&???_?_??.??i?????(?$_f?zD??????ZGH??g???
g?E:_??bz^??f?~o???t?}??u=uzp\UI????Z??l[?G?3??Y?UAEfKT?f?O??N_?d__?????a_?15%?^?
'p?_?$,9"{??^??y??_?t???,?W?PCW??~??[?$??????e????f?Y-Zi__??_??w?_?&QT??`?`~?[?K_??_???
廣告

© . All rights reserved.