密码学是研究和实践不同技术以保护通信免受第三方干扰的学科。它用于网络安全。我们试图开发方法和实践来保护敏感数据。密码学的唯一目标是保护数据免受攻击者的侵害。非对称加密也被称为公钥/私钥加密。私钥如其名,将保持私有,而公钥可以分发。加密是两个密钥之间的数学关联,一个用于加密,另一个用于解密。例如,如果有两个密钥“A1”和“A2”,那么如果密钥“A1”用于加密,“A2”用于解密,反之亦然。
我们使用RSA算法进行非对称加密,首先我们会生成一对密钥(公钥、私钥)。
非对称加密在Java中的密码学
To generate asymmetric key following steps can be followed −
-
首先,我们使用SecureRandom类生成公钥和私钥。它用于生成随机数。
-
By using RSA algorithm to generate keys. This class will provide getInstance() method which is used to pass a string variable which signify the key generation algorithm and it returns to key generator object.
-
使用2048位密钥大小初始化密钥生成器对象,并传递随机数。
-
Now, the secret key is generated and we want to look at the key we can convert it into hexbinary format by using DatatypeConvertor.
现在实施上述方法 −
Syntax
// Java program to create a
// asymmetric key
package java_cryptography;
import java.security.KeyPair;
import java.security
.KeyPairGenerator;
import java.security
.SecureRandom;
import javax.xml.bind
.DatatypeConverter;
// Class to create an asymmetric key
public class Asymmetric {
private static final String RSA
= "RSA";
// Generating public and private keys
// using RSA algorithm.
public static KeyPair generateRSAKkeyPair()
throws Exception
{
SecureRandom secureRandom
= new SecureRandom();
KeyPairGenerator keyPairGenerator
= KeyPairGenerator.getInstance(RSA);
keyPairGenerator.initialize(
2048, secureRandom);
return keyPairGenerator
.generateKeyPair();
}
// Driver code
public static void main(String args[])
throws Exception
{
KeyPair keypair
= generateRSAKkeyPair();
System.out.println(
"Public Key is: "
+ DatatypeConverter.printHexBinary(
keypair.getPublic().getEncoded()));
System.out.println(
"Private Key is: "
+ DatatypeConverter.printHexBinary(
keypair.getPrivate().getEncoded()));
}
}
登录后复制
输出
登录后复制
现在可以采取以下步骤来创建程序代码 −
-
我们使用cipher类创建两种不同的模式:加密和解密。加密密钥是私钥,解密密钥是公钥。
-
在密码器上调用doFinal()方法,该方法可以对数据进行单部分操作进行加密/解密,或者完成多部分操作并返回字节数组。
-
最后,我们在使用ENCRYPT_MODE进行加密后得到了密文。
程序,代码
// Java program to perform the
// encryption and decryption
// using asymmetric key
package java_cryptography;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.xml.bind
.DatatypeConverter;
public class Asymmetric {
private static final String RSA
= "RSA";
private static Scanner sc;
// Generating public & private keys
// using RSA algorithm.
public static KeyPair generateRSAKkeyPair()
throws Exception
{
SecureRandom secureRandom = new SecureRandom();
KeyPairGenerator keyPairGenerator
= KeyPairGenerator.getInstance(RSA);
keyPairGenerator.initialize(
2048, secureRandom);
return keyPairGenerator
.generateKeyPair();
}
// Encryption function which converts
// the plainText into a cipherText
// using private Key.
public static byte[] do_RSAEncryption(
String plainText,
PrivateKey privateKey)
throws Exception
{
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(
plainText.getBytes());
}
// Decryption function which converts
// the ciphertext back to the
// original plaintext.
public static String do_RSADecryption(
byte[] cipherText,
PublicKey publicKey)
throws Exception
{
Cipher cipher
= Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE,
publicKey);
byte[] result
= cipher.doFinal(cipherText);
return new String(result);
}
// Driver code
public static void main(String args[])
throws Exception
{
KeyPair keypair
= generateRSAKkeyPair();
String plainText = "This is the PlainText "+ "I want to Encrypt using RSA.";
byte[] cipherText
= do_RSAEncryption(
plainText,
keypair.getPrivate());
System.out.println(
"The Public Key is: "
+ DatatypeConverter.printHexBinary(
keypair.getPublic().getEncoded()));
System.out.println(
"The Private Key is: "
+ DatatypeConverter.printHexBinary(
keypair.getPrivate().getEncoded()));
System.out.print("The Encrypted Text is: ");
System.out.println(
DatatypeConverter.printHexBinary(
cipherText));
String decryptedText
= do_RSADecryption(
cipherText,
keypair.getPublic());
System.out.println(
"The decrypted text is: "
+ decryptedText);
}
}
登录后复制
输出
登录后复制
结论
Thus, using RSA algorithm we created encrypted text “This is the PlainText I want to Encrpyt using RSA” in this article.
以上就是Java中的非对称加密密码学的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!