Java 公私钥加解密实现流程

1. 概述

在Java中实现公私钥加解密涉及到使用RSA算法。RSA是一种非对称加密算法,可以实现数据的加密和解密,并且可以保证加密和解密的安全性。本文将详细介绍如何使用Java实现RSA公私钥加解密。

2. 流程图

stateDiagram
    [*] --> 生成秘钥对
    生成秘钥对 --> 加密数据
    加密数据 --> 解密数据
    解密数据 --> 结束
    结束 --> [*]

3. 具体步骤

3.1 生成秘钥对

首先,我们需要生成一对公私钥。在Java中,可以使用KeyPairGenerator类来生成秘钥对。以下是代码示例:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

public class KeyPairGeneratorExample {

    public KeyPair generateKeyPair() throws NoSuchAlgorithmException {
        // 创建 KeyPairGenerator 对象
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        // 初始化 KeyPairGenerator 对象
        keyPairGenerator.initialize(2048);
        // 生成 KeyPair 对象
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return keyPair;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        KeyPairGeneratorExample example = new KeyPairGeneratorExample();
        KeyPair keyPair = example.generateKeyPair();
        // 获取公钥
        System.out.println("Public Key: " + keyPair.getPublic());
        // 获取私钥
        System.out.println("Private Key: " + keyPair.getPrivate());
    }
}

3.2 加密数据

一般情况下,我们使用公钥来加密数据。在Java中,可以使用Cipher类来进行加密操作。以下是代码示例:

import javax.crypto.Cipher;
import java.security.Key;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;

public class EncryptExample {

    public byte[] encryptData(String data, Key publicKey) throws Exception {
        // 创建 Cipher 对象
        Cipher cipher = Cipher.getInstance("RSA");
        // 初始化 Cipher 对象
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        // 加密数据
        byte[] encryptedData = cipher.doFinal(data.getBytes());
        return encryptedData;
    }

    public static void main(String[] args) throws Exception {
        KeyPairGeneratorExample keyPairGeneratorExample = new KeyPairGeneratorExample();
        KeyPair keyPair = keyPairGeneratorExample.generateKeyPair();
        EncryptExample encryptExample = new EncryptExample();
        // 获取公钥
        Key publicKey = keyPair.getPublic();
        
        String data = "Hello, world!";
        byte[] encryptedData = encryptExample.encryptData(data, publicKey);
        System.out.println("Encrypted Data: " + new String(encryptedData));
    }
}

3.3 解密数据

解密数据需要使用私钥。以下是代码示例:

import javax.crypto.Cipher;
import java.security.Key;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;

public class DecryptExample {

    public String decryptData(byte[] encryptedData, Key privateKey) throws Exception {
        // 创建 Cipher 对象
        Cipher cipher = Cipher.getInstance("RSA");
        // 初始化 Cipher 对象
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        // 解密数据
        byte[] decryptedData = cipher.doFinal(encryptedData);
        return new String(decryptedData);
    }

    public static void main(String[] args) throws Exception {
        KeyPairGeneratorExample keyPairGeneratorExample = new KeyPairGeneratorExample();
        KeyPair keyPair = keyPairGeneratorExample.generateKeyPair();
        DecryptExample decryptExample = new DecryptExample();
        // 获取私钥
        Key privateKey = keyPair.getPrivate();
        
        String data = "Hello, world!";
        byte[] encryptedData = encryptExample.encryptData(data, publicKey);
        String decryptedData = decryptExample.decryptData(encryptedData, privateKey);
        System.out.println("Decrypted Data: " + decryptedData);
    }
}

4. 总结

通过以上步骤,我们可以实现Java中的公私钥加解密。首先,我们需要生成一对公私钥,然后使用公钥对数据进行加密,最后使用私钥对加密后的数据进行解密。这样可以保证数据的机密性和安全性。