Skip to content

理解 RSA 加解密算法

RSA 是一种非对称加密算法,广泛应用于安全数据传输。它利用一对密钥(公钥和私钥)来实现加密和解密操作。本文将详细介绍 RSA 加解密的原理及其在 Java 中的实现。

1. RSA 加密原理

RSA 加密算法基于两个大素数的数学特性。它使用公钥加密数据,而只能用对应的私钥解密。以下是 RSA 加密的基本步骤:

  1. 选择两个大素数 p 和 q:这些素数用于生成密钥。
  2. 计算 n = p * q:n 用作模数。
  3. 计算 φ(n) = (p-1) * (q-1):这是欧拉函数。
  4. 选择一个小整数 e:1 < e < φ(n),且 e 与 φ(n) 互质。
  5. 计算 d:d 是 e 的模 φ(n) 的乘法逆元,即 d * e ≡ 1 (mod φ(n))。
  6. 公钥 (e, n):用于加密。
  7. 私钥 (d, n):用于解密。

2. RSA 解密原理

RSA 解密使用私钥来解密由公钥加密的数据。解密过程如下:

  1. 接收加密消息 C
  2. 使用私钥 d 和模数 n 计算 M = C^d mod n:得到明文 M。

3. 使用示例

加密过程

假设我们要加密消息 "Hello":

  1. 将消息转换为数字表示。
  2. 使用公钥 (e, n) 计算密文 C = M^e mod n。

解密过程

使用私钥 (d, n) 计算明文 M = C^d mod n。

Java 示例代码

以下是使用 Java 实现 RSA 加密和解密的示例代码:

java
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;

public class RSAExample {
    public static void main(String[] args) throws Exception {
        // 生成密钥对
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        keyPairGen.initialize(2048);
        KeyPair pair = keyPairGen.generateKeyPair();
        PublicKey publicKey = pair.getPublic();
        PrivateKey privateKey = pair.getPrivate();

        // 数据加密
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        String message = "Hello, World!";
        byte[] encryptedMessage = cipher.doFinal(message.getBytes());
        System.out.println("Encrypted Message: " + new String(encryptedMessage));

        // 数据解密
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedMessage = cipher.doFinal(encryptedMessage);
        System.out.println("Decrypted Message: " + new String(decryptedMessage));
    }
}

解释:

  1. 使用 KeyPairGenerator 生成RSA密钥对。
  2. 使用公钥加密消息。
  3. 使用私钥解密消息。

4. 从私钥获取公钥的方法

从私钥 (d, n) 获取公钥 (e, n) 的过程如下:

  1. 已知 d 和 n。
  2. 计算 e,使得 d * e ≡ 1 (mod φ(n))。

Java 示例代码

以下是使用 Java 从私钥获取公钥的示例代码:

java
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.math.BigInteger;

public class RSAKeyExample {
    public static void main(String[] args) throws Exception {
        // 生成密钥对
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        keyPairGen.initialize(2048);
        KeyPair pair = keyPairGen.generateKeyPair();
        PrivateKey privateKey = pair.getPrivate();

        // 从私钥中提取公钥
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPrivateCrtKeySpec privSpec = keyFactory.getKeySpec(privateKey, RSAPrivateCrtKeySpec.class);
        BigInteger modulus = privSpec.getModulus();
        BigInteger publicExponent = privSpec.getPublicExponent();
        
        RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modulus, publicExponent);
        PublicKey publicKey = keyFactory.generatePublic(pubSpec);

        System.out.println("Public Key: " + publicKey);
    }
}

解释:

  1. 使用 KeyPairGenerator 生成 RSA 密钥对。
  2. 提取私钥的模数和指数。
  3. 生成公钥对象。

5. 总结

RSA 是一种强大的加密算法,通过使用公钥和私钥实现数据的安全传输。本文详细介绍了 RSA 的加解密原理、使用示例、从私钥获取公钥的方法,并提供了 Java 的示例代码。

6. 参考

上次更新于: