0
点赞
收藏
分享

微信扫一扫

解决可逆加解密算法 Java的具体操作步骤

紫荆峰 2023-07-13 阅读 19

可逆加解密算法 Java

1. 简介

可逆加解密算法是一种能够将数据加密成密文,并且能够通过解密算法将密文还原成原始数据的算法。在实际应用中,可逆加解密算法常用于保护敏感数据的安全传输和存储,以及实现数据的加密和解密操作。

在 Java 中,我们可以使用各种加解密算法实现数据的保护和解析。本文将介绍几种常见的可逆加解密算法,并附上相应的示例代码。

2. 可逆加解密算法示例

2.1. 对称加解密算法

对称加解密算法使用同一个密钥进行加密和解密操作,常见的对称加解密算法有 DES、AES 等。

以下是使用 AES 对称加解密算法实现数据加密和解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class SymmetricEncryption {
    private static final String ALGORITHM = "AES";
    private static final String SECRET_KEY = "ThisIsASecretKey";

    public static String encrypt(String data) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encryptedData = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    public static String decrypt(String encryptedData) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decryptedData);
    }

    public static void main(String[] args) {
        try {
            String originalData = "Hello World!";
            String encryptedData = encrypt(originalData);
            String decryptedData = decrypt(encryptedData);

            System.out.println("Original Data: " + originalData);
            System.out.println("Encrypted Data: " + encryptedData);
            System.out.println("Decrypted Data: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在上述代码中,我们使用 AES 对称加解密算法实现了 encryptdecrypt 方法,分别用于对数据进行加密和解密。在 main 方法中,我们演示了对字符串 "Hello World!" 进行加密和解密的过程。

2.2. 非对称加解密算法

非对称加解密算法使用一对密钥,包括公钥和私钥,其中公钥用于加密数据,私钥用于解密数据。常见的非对称加解密算法有 RSA、DSA 等。

以下是使用 RSA 非对称加解密算法实现数据加密和解密的示例代码:

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

public class AsymmetricEncryption {
    private static final String ALGORITHM = "RSA";

    public static String encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedData = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decryptedData, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        try {
            // 生成密钥对
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();

            String originalData = "Hello World!";
            String encryptedData = encrypt(originalData, publicKey);
            String decryptedData = decrypt(encryptedData, privateKey);

            System.out.println("Original Data: " + originalData);
            System.out.println("Encrypted Data: " + encryptedData);
            System.out.println("Decrypted Data: " + decryptedData);
        } catch (Exception e
举报

相关推荐

0 条评论