首頁 >Java >java教程 >Java中的非對稱加密密碼學

Java中的非對稱加密密碼學

王林
王林轉載
2023-08-19 10:25:15595瀏覽

密碼學是研究和實踐不同技術以保護通訊免受第三方乾擾的學科。它用於網路安全。我們試圖開發方法和實踐來保護敏感資料。密碼學的唯一目標是保護資料免受攻擊者的侵害。非對稱加密也被稱為公鑰/私鑰加密。私鑰如其名,將保持私有,而公鑰可以分發。加密是兩個金鑰之間的數學關聯,一個用於加密,另一個用於解密。例如,如果有兩個金鑰“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位元金鑰大小初始化金鑰產生器對象,並傳遞隨機數。
  • 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()));
	}
}

輸出

<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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除