总结
豆包 AI 助手文章总结
首页 > Java > java教程 > 正文

Java中的非对称加密密码学

王林
发布: 2023-08-19 10:25:15
转载
706人浏览过

密码学是研究和实践不同技术以保护通信免受第三方干扰的学科。它用于网络安全。我们试图开发方法和实践来保护敏感数据。密码学的唯一目标是保护数据免受攻击者的侵害。非对称加密也被称为公钥/私钥加密。私钥如其名,将保持私有,而公钥可以分发。加密是两个密钥之间的数学关联,一个用于加密,另一个用于解密。例如,如果有两个密钥“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.

    立即学习Java免费学习笔记(深入)”;

  • 使用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;">@@##@@<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);">@@##@@<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中的非对称加密密码学Java中的非对称加密密码学

以上就是Java中的非对称加密密码学的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号