非对称加密 对称加密 相关介绍及使用示例 Java代码

Sure! Let's start with a brief introduction to asymmetric and symmetric encryption:

  1. 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.

  2. 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.

posted @   ukyo--碳水化合物  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有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编程语言开发框架
主题色彩
点击右上角即可分享
微信分享提示
人是要整活的——没活了,可不就是死了么?