目录

Java密码学 - 解密数据( Decrypting Data)

您可以使用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()方法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()方法接受两个参数

  • 表示操作模式的整数参数(加密/解密)
  • 表示公钥的关键对象

使用init()方法初始化Cypher对象,如下所示。

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

步骤7:将数据添加到Cipher对象

Cipher类的update()方法接受表示要加密的数据的字节数组,并使用给定的数据更新当前对象。

通过以字节数组的形式将数据传递给update()方法来update()初始化的Cipher对象,如下所示。

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

第8步:加密数据

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

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

步骤9:初始化Cipher对象以进行解密

要解密前面步骤中加密的密码,您需要初始化它以进行解密。

因此,通过传递参数Cipher.DECRYPT_MODE和PrivateKey对象来初始化密码对象,如下所示。

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

第10步:解密数据

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

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

例子 (Example)

以下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 IoWiki".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));
   }
}

输出 (Output)

上述程序生成以下输出 -

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 IoWiki
↑回到顶部↑
WIKI教程 @2018