Class RsaUtil

java.lang.Object
com.sure.tool.crypto.RsaUtil

public class RsaUtil extends Object
RSA 加解密工具类(RSA/ECB/OAEPWithSHA-256AndMGF1Padding),无状态静态方法、线程安全,参考 Hutool 的 RSA 设计。

安全设计(2026-09-10 升级,随 CodeQL 安全扫描告警修复):

  • 采用 OAEP-SHA256 填充(替代 PKCS#1 v1.5,消除 Bleichenbacher padding oracle 风险);
  • 密钥长度强制不低于 2048 位(拒绝 1024 及以下的弱密钥)。
单次加密明文上限 = 密钥位数 / 8 - 2 * 32 - 2 字节(2048 位约 190 字节),超出请自行分段或改用混合加密。
Since:
0.1.0
Author:
suretool
  • Field Details

    • DEFAULT_KEY_SIZE

      public static final int DEFAULT_KEY_SIZE
      默认密钥位数
      See Also:
    • MIN_KEY_SIZE

      public static final int MIN_KEY_SIZE
      允许的最小密钥位数(低于此值直接拒绝,弱密钥)
      See Also:
  • Method Details

    • generateKeyPair

      public static KeyPair generateKeyPair()
      生成密钥对(2048 位)。
      Returns:
      密钥对
    • generateKeyPair

      public static KeyPair generateKeyPair(int keySize)
      生成密钥对。
      Parameters:
      keySize - 密钥位数(强制不小于 MIN_KEY_SIZE,推荐 2048 及以上)
      Returns:
      密钥对
      Throws:
      IllegalArgumentException - keySize 小于 2048
    • encrypt

      public static byte[] encrypt(byte[] data, Key key)
      RSA 加密(OAEP-SHA256)。
      Parameters:
      data - 明文
      key - 公钥或私钥
      Returns:
      密文字节
    • decrypt

      public static byte[] decrypt(byte[] data, Key key)
      RSA 解密(OAEP-SHA256)。
      Parameters:
      data - 密文字节
      key - 与加密相反的公钥或私钥
      Returns:
      明文字节
    • encryptBase64

      public static String encryptBase64(String data, PublicKey publicKey)
      公钥加密为 Base64。
      Parameters:
      data - 明文(UTF-8)
      publicKey - 公钥
      Returns:
      Base64 密文
    • decryptBase64

      public static String decryptBase64(String base64, PrivateKey privateKey)
      私钥解密 Base64 密文。
      Parameters:
      base64 - Base64 密文
      privateKey - 私钥
      Returns:
      明文(UTF-8)
    • encryptHex

      public static String encryptHex(String data, PublicKey publicKey)
      公钥加密为十六进制。
      Parameters:
      data - 明文(UTF-8)
      publicKey - 公钥
      Returns:
      十六进制密文
    • decryptHex

      public static String decryptHex(String hex, PrivateKey privateKey)
      私钥解密十六进制密文。
      Parameters:
      hex - 十六进制密文
      privateKey - 私钥
      Returns:
      明文(UTF-8)
    • getPublicKeyBase64

      public static String getPublicKeyBase64(KeyPair keyPair)
      公钥转 Base64(X.509)。
      Parameters:
      keyPair - 密钥对
      Returns:
      Base64 公钥
    • getPrivateKeyBase64

      public static String getPrivateKeyBase64(KeyPair keyPair)
      私钥转 Base64(PKCS#8)。
      Parameters:
      keyPair - 密钥对
      Returns:
      Base64 私钥
    • parsePublicKey

      public static PublicKey parsePublicKey(String base64)
      Base64 解析公钥。
      Parameters:
      base64 - Base64 公钥
      Returns:
      公钥
    • parsePrivateKey

      public static PrivateKey parsePrivateKey(String base64)
      Base64 解析私钥。
      Parameters:
      base64 - Base64 私钥
      Returns:
      私钥
    • sign

      public static String sign(String data, PrivateKey privateKey)
      RSA 签名(SHA256withRSA,Base64 输出)。
      Parameters:
      data - 原文
      privateKey - 私钥
      Returns:
      Base64 签名
    • verify

      public static boolean verify(String data, PublicKey publicKey, String signature)
      RSA 验签(SHA256withRSA,Base64 输入)。
      Parameters:
      data - 原文
      publicKey - 公钥
      signature - Base64 签名
      Returns:
      是否通过
    • signHex

      public static String signHex(String data, PrivateKey privateKey)
      RSA 签名(SHA256withRSA,Hex 输出)。
      Parameters:
      data - 原文
      privateKey - 私钥
      Returns:
      Hex 签名
    • verifyHex

      public static boolean verifyHex(String data, PublicKey publicKey, String signature)
      RSA 验签(SHA256withRSA,Hex 输入)。
      Parameters:
      data - 原文
      publicKey - 公钥
      signature - Hex 签名
      Returns:
      是否通过
    • signBytes

      public static byte[] signBytes(String data, PrivateKey privateKey)
      RSA 签名,返回原始字节。
      Parameters:
      data - 原文
      privateKey - 私钥
      Returns:
      签名字节
    • verifyBytes

      public static boolean verifyBytes(String data, PublicKey publicKey, byte[] signature)
      RSA 验签,字节输入。
      Parameters:
      data - 原文
      publicKey - 公钥
      signature - 签名字节
      Returns:
      是否通过