非对称加密 对称加密 相关介绍及使用示例 Java代码
Sure! Let's start with a brief introduction to asymmetric and symmetric encryption:
-
Asymmetric Encryption:
Asymmetric encryption, also known as public-key encryption, uses a pair of keys: a public key and a private key. The public key is shared with everyone, while the private key is kept secret. Data encrypted with the public key can only be decrypted with the corresponding private key. Asymmetric encryption is commonly used for secure key exchange and digital signatures. -
Symmetric Encryption:
Symmetric encryption, also known as secret-key encryption, uses the same key for both encryption and decryption. This means that both the sender and the receiver need to possess the same secret key to encrypt and decrypt messages. Symmetric encryption is generally faster than asymmetric encryption, making it suitable for encrypting large amounts of data.
Here's a simple Java example demonstrating how to use asymmetric and symmetric encryption using the Java Cryptography Extension (JCE) library:
import javax.crypto.*;
import java.security.*;
public class EncryptionExample {
public static void main(String[] args) throws Exception {
// Asymmetric Encryption
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String plaintext = "Hello, this is a secret message!";
byte[] encryptedData = encryptWithPublicKey(plaintext, publicKey);
String decryptedData = decryptWithPrivateKey(encryptedData, privateKey);
System.out.println("Original Message: " + plaintext);
System.out.println("Decrypted Message: " + decryptedData);
// Symmetric Encryption
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128); // Using AES with 128-bit key
SecretKey secretKey = keyGenerator.generateKey();
String plaintext2 = "This is another secret message!";
byte[] encryptedData2 = encryptWithSymmetricKey(plaintext2, secretKey);
String decryptedData2 = decryptWithSymmetricKey(encryptedData2, secretKey);
System.out.println("Original Message 2: " + plaintext2);
System.out.println("Decrypted Message 2: " + decryptedData2);
}
private static byte[] encryptWithPublicKey(String plaintext, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(plaintext.getBytes());
}
private static String decryptWithPrivateKey(byte[] encryptedData, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedData = cipher.doFinal(encryptedData);
return new String(decryptedData);
}
private static byte[] encryptWithSymmetricKey(String plaintext, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(plaintext.getBytes());
}
private static String decryptWithSymmetricKey(byte[] encryptedData, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedData = cipher.doFinal(encryptedData);
return new String(decryptedData);
}
}
Please note that this example demonstrates the basic usage of asymmetric and symmetric encryption. In real-world scenarios, you should follow best practices for key management and security. Additionally, remember to handle exceptions and error cases appropriately.
作者:ukyo--碳水化合物
出处:https://www.cnblogs.com/ukzq/p/17600156.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2022-08-02 JITWatch
2022-08-02 新手电吉他是三单还是单单双?
2022-08-02 最惨iPhone诞生 iPhone14 mini要被取消
2021-08-02 java获取系统信息依赖包
2020-08-02 简单netty demo 未完成
2019-08-02 Truffle - 以太坊Solidity编程语言开发框架