密碼學是研究和實踐不同技術以保護通訊免受第三方乾擾的學科。它用於網路安全。我們試圖開發方法和實踐來保護敏感資料。密碼學的唯一目標是保護資料免受攻擊者的侵害。非對稱加密也被稱為公鑰/私鑰加密。私鑰如其名,將保持私有,而公鑰可以分發。加密是兩個金鑰之間的數學關聯,一個用於加密,另一個用於解密。例如,如果有兩個金鑰“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 geneInstance() method which is used to pass a string variable which signify the key gene algorithm and it returns to key generator object#.
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())); } }
<p style="color:#D2593F;"><img src="https://img.php.cn/upload/article/000/465/014/169241191890148.jpg" alt="Java中的非對稱加密密碼學" /><b></b></p>
// 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>
以上是Java中的非對稱加密密碼學的詳細內容。更多資訊請關注PHP中文網其他相關文章!