您可以使用 javax.crypto 包的Cipher类解密加密数据.按照下面给出的步骤使用Java解密给定数据.
步骤1:创建KeyPairGenerator对象
KeyPairGenerator class提供 getInstance()方法,该方法接受表示所需密钥生成算法的String变量,并返回生成密钥的KeyPairGenerator对象.
创建 KeyPairGenerator 使用 getInstance()方法的对象,如下所示.
//创建KeyPair生成器对象 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DSA");
第2步:初始化KeyPairGenerator对象
KeyPairGenerator 类提供了一个名为的方法 initialize()此方法用于初始化密钥对生成器.此方法接受表示密钥大小的整数值.
使用 initialize()方法初始化在上一步中创建的KeyPairGenerator对象,如下所示.
//初始化KeyPairGenerator keyPairGen.initialize(2048);
第3步:生成KeyPairGenerator
您可以使用<生成 KeyPair b> KeyPairGenerator 类的generateKeyPair()方法.使用此方法生成密钥对,如下所示.
//生成密钥对 KeyPair pair = keyPairGen.generateKeyPair( );
步骤4:获取公钥
您可以使用
//从密钥对中获取公钥 PublicKey publicKey = pair.getPublic();
步骤5:创建密码对象
getInstance()方法 Cipher 类接受表示所需转换的String变量,并返回实现给定转换的Cipher对象.
使用 getInstance()创建Cipher对象方法如下所示.
//创建一个Cipher对象 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
步骤6:初始化密码对象
init()方法Cipher类接受两个参数
表示操作模式的整数参数(加密/解密)
表示公钥的关键对象
使用 init()方法初始化Cypher对象,如下所示.
//初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE,publicKey);
步骤7:将数据添加到密码对象
update()方法Cipher类接受表示要加密的数据的字节数组,并使用给定的数据更新当前对象.
通过将数据传递给更新来更新初始化的Cipher对象()字节数组形式的方法,如下所示.
//Adding data to the cipherbyte[] input = "Welcome to Tutorialspoint".getBytes(); cipher.update(input);
步骤8:加密数据
密码的 doFinal()方法class完成加密操作.因此,使用此方法完成加密,如下所示.
//加密数据 byte [] cipherText = cipher.doFinal ();
步骤9:初始化Cipher对象以进行解密
要解密前面步骤中加密的密码,您需要初始化它以进行解密.
因此,通过传递参数Cipher.DECRYPT_MODE和PrivateKey对象来初始化密码对象,如下所示.
//为解密初始化相同的密码 cipher.init(Cipher.DECRYPT_MODE,pair.getPrivate());
步骤10:解密数据
最后,使用 doFinal()
//解密文本 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?pv?w?!?H???^?A??????P?u??FA?????_?? ???_jMH-??>??OP?'?j?_?n`?_??'`????o??_GL??g???g_f?????f|???LT?|?Vz_TDu#??\?