密码学是研究和实践不同技术以保护通信免受第三方干扰的学科。它用于网络安全。我们试图开发方法和实践来保护敏感数据。密码学的唯一目标是保护数据免受攻击者的侵害。非对称加密也被称为公钥/私钥加密。私钥如其名,将保持私有,而公钥可以分发。加密是两个密钥之间的数学关联,一个用于加密,另一个用于解密。例如,如果有两个密钥“A1”和“A2”,那么如果密钥“A1”用于加密,“A2”用于解密,反之亦然。
我们使用RSA算法进行非对称加密,首先我们会生成一对密钥(公钥、私钥)。
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.
现在实施上述方法 −
// 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())); } }
<p style="color:#D2593F;"><img src="https://img.php.cn/upload/article/000/465/014/169241191890148.jpg" alt="Java中的非对称加密密码学" /><b></b></p>
现在可以采取以下步骤来创建程序代码 −
我们使用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); } }
<p style="text-align: center; color: rgb(210, 89, 63);"><img src="https://img.php.cn/upload/article/000/465/014/169241191871435.jpg" alt="Java中的非对称加密密码学" /><b></b></p>
Thus, using RSA algorithm we created encrypted text “This is the PlainText I want to Encrpyt using RSA” in this article.
以上是Java中的非对称加密密码学的详细内容。更多信息请关注PHP中文网其他相关文章!