Class RSA

All Implemented Interfaces:
Serializable, Decryptor, Encryptor

public class RSA extends Crypto
RSA公钥/私钥/签名加密解密

由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密, 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全

Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • RSA

      public RSA()
      构造,生成新的私钥公钥对
    • RSA

      public RSA(String rsaAlgorithm)
      构造,生成新的私钥公钥对
      Parameters:
      rsaAlgorithm - 自定义RSA算法,例如RSA/ECB/PKCS1Padding
    • RSA

      public RSA(String privateKey, String publicKey)
      构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
      Parameters:
      privateKey - 私钥Hex或Base64表示
      publicKey - 公钥Hex或Base64表示
    • RSA

      public RSA(String rsaAlgorithm, String privateKey, String publicKey)
      构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
      Parameters:
      rsaAlgorithm - 自定义RSA算法,例如RSA/ECB/PKCS1Padding
      privateKey - 私钥Hex或Base64表示
      publicKey - 公钥Hex或Base64表示
    • RSA

      public RSA(byte[] privateKey, byte[] publicKey)
      构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
      Parameters:
      privateKey - 私钥
      publicKey - 公钥
    • RSA

      public RSA(BigInteger modulus, BigInteger privateExponent, BigInteger publicExponent)
      构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
      Parameters:
      modulus - N特征值
      privateExponent - d特征值
      publicExponent - e特征值
    • RSA

      public RSA(PrivateKey privateKey, PublicKey publicKey)
      构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
      Parameters:
      privateKey - 私钥
      publicKey - 公钥
    • RSA

      public RSA(String rsaAlgorithm, PrivateKey privateKey, PublicKey publicKey)
      构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
      Parameters:
      rsaAlgorithm - 自定义RSA算法,例如RSA/ECB/PKCS1Padding
      privateKey - 私钥
      publicKey - 公钥
  • Method Details

    • generatePrivateKey

      public static PrivateKey generatePrivateKey(BigInteger modulus, BigInteger privateExponent)
      生成RSA私钥
      Parameters:
      modulus - N特征值
      privateExponent - d特征值
      Returns:
      PrivateKey
    • generatePublicKey

      public static PublicKey generatePublicKey(BigInteger modulus, BigInteger publicExponent)
      生成RSA公钥
      Parameters:
      modulus - N特征值
      publicExponent - e特征值
      Returns:
      PublicKey
    • encrypt

      public byte[] encrypt(byte[] data, KeyType keyType)
      Description copied from interface: Encryptor
      加密
      Specified by:
      encrypt in interface Encryptor
      Overrides:
      encrypt in class Crypto
      Parameters:
      data - 被加密的bytes
      keyType - 私钥或公钥 KeyType
      Returns:
      加密后的bytes
    • decrypt

      public byte[] decrypt(byte[] bytes, KeyType keyType)
      Description copied from interface: Decryptor
      解密
      Specified by:
      decrypt in interface Decryptor
      Overrides:
      decrypt in class Crypto
      Parameters:
      bytes - 被解密的bytes
      keyType - 私钥或公钥 KeyType
      Returns:
      解密后的bytes
    • initCipher

      protected void initCipher()
      Description copied from class: Crypto
      初始化Cipher,默认尝试加载BC库
      Overrides:
      initCipher in class Crypto