密碼學是研究和實踐不同技術以保護通訊免受第三方乾擾的學科。它用於網路安全。我們試圖開發方法和實踐來保護敏感資料。密碼學的唯一目標是保護資料免受攻擊者的侵害。非對稱加密也被稱為公鑰/私鑰加密。私鑰如其名,將保持私有,而公鑰可以分發。加密是兩個金鑰之間的數學關聯,一個用於加密,另一個用於解密。例如,如果有兩個金鑰“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 geneInstance() method which is used to pass a string variable which signify the key gene algorithm and it returns to key generator object#.
使用2048位元金鑰大小初始化金鑰產生器對象,並傳遞隨機數。 - #現在實作上述方法 −
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="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/465/014/169241191890148.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="Java中的非對稱加密密碼學" /><b></b></p>
現在可以採取以下步驟來建立程式碼 −
- 我們使用cipher類別創建兩種不同的模式:加密和解密。加密金鑰是私鑰,解密金鑰是公鑰。
// 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="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/465/014/169241191871435.jpg?x-oss-process=image/resize,p_40" class="lazy" 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中文網其他相關文章!

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

Java代碼在不同平台上運行時會有性能差異。 1)JVM的實現和優化策略不同,如OracleJDK和OpenJDK。 2)操作系統的特性,如內存管理和線程調度,也會影響性能。 3)可以通過選擇合適的JVM、調整JVM參數和代碼優化來提升性能。

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑戰WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具