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

Beyond The API / 2023-08-02 / 原文

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.