Java encryption algorithm: summary of symmetric encryption and asymmetric encryption

There are two common encryption algorithms in Java: symmetric encryption and asymmetric encryption. This article will introduce these two encryption algorithms and their application in Java in detail.

1. Symmetric encryption

Symmetric encryption algorithms require a key to encrypt and decrypt data. Encryption and decryption use the same key, so it is called symmetric encryption. The advantage of the symmetric encryption algorithm is that the encryption and decryption speed is fast, but its disadvantage is that the key needs to be transmitted securely, otherwise the attacker will obtain and decrypt the ciphertext.

In Java, common symmetric encryption algorithms include DES, 3DES, AES, etc. Among them, AES is the most commonly used symmetric encryption algorithm, which has the following advantages:

  1. High security: AES is one of the currently recognized safe symmetric encryption algorithms.

  2. Fast speed: AES encryption and decryption speed are fast, suitable for encrypting large amounts of data.

  3. Algorithm flexibility: AES supports multiple key lengths, including 128, 192, and 256 bits. The longer the key, the stronger the encryption.

In Java, symmetric encryption can be implemented using related classes and methods provided by the javax.crypto package.

The following is a simple Java code example to encrypt and decrypt data using the AES symmetric encryption algorithm:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class SymmetricEncryptionDemo {
    public static void main(String[] args) throws Exception {
        String plainText = "This is a test message.";

        // 生成 AES 密钥
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();

        // 加密
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] cipherTextBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
        String cipherText = Base64.getEncoder().encodeToString(cipherTextBytes);
        System.out.println("Cipher text: " + cipherText);

        // 解密
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(cipherText));
        String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);
        System.out.println("Decrypted text: " + decryptedText);
    }
}

In the above code, we have encrypted and decrypted the string data using the AES symmetric encryption algorithm. We first use the KeyGenerator class to generate a random AES key, and then use the Cipher class to encrypt and decrypt the data.

2. Asymmetric encryption

Asymmetric encryption algorithms require a pair of keys, a private key and a public key. The public key is used to encrypt data, and the private key is used to decrypt data or make digital signatures. Asymmetric encryption algorithm is used in digital signature, key agreement and so on. The asymmetric encryption algorithm can avoid the problem of key transmission security in the symmetric encryption algorithm, but its disadvantage is that the encryption and decryption speed is relatively slow.

In Java, common asymmetric encryption algorithms include RSA, DSA, etc. Among them, RSA is the most commonly used asymmetric encryption algorithm, which has the following advantages:

  1. High security: RSA is one of the currently recognized safe asymmetric encryption algorithms.

  2. Flexible algorithm: RSA supports multiple key lengths, including 512, 1024 and 2048 bits. The longer the key, the stronger the encryption.

In Java, asymmetric encryption can be implemented using related classes and methods provided by the java.security package.

The following is a simple Java code example to encrypt and decrypt data using the RSA asymmetric encryption algorithm:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;

public class AsymmetricEncryptionDemo {
    public static void main(String[] args) throws Exception {
        String plainText = "This is a test message.";

        // 生成 RSA 密钥对
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);
        KeyPair keyPair = keyGen.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        // 加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] cipherTextBytes = cipher.doFinal(plainText.getBytes());
        String cipherText = new String(cipherTextBytes);

        // 解密
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(cipherTextBytes);
        String decryptedText = new String(decryptedBytes);

        System.out.println("Original text: " + plainText);
        System.out.println("Encrypted text: " + cipherText);
        System.out.println("Decrypted text: " + decryptedText);
    }
}

In the above code, we have encrypted and decrypted the string data using the RSA asymmetric encryption algorithm. We first use the KeyPairGenerator class to generate a random RSA key pair, and then use the Cipher class to encrypt and decrypt the data. In the encryption process, we encrypt the data with the public key; in the decryption process, we decrypt the data with the private key. Finally we output the original text, encrypted text and decrypted text.

Guess you like

Origin blog.csdn.net/u013558123/article/details/131321303