All Implemented Interfaces:
Serializable, Decryptor, Encryptor
Direct Known Subclasses:
ECIES, RSA

public class Crypto extends AbstractCrypto<Crypto>
非对称加密算法
 1、签名:使用私钥加密,公钥解密。
 用于让所有公钥所有者验证私钥所有者的身份并且用来防止私钥所有者发布的内容被篡改,但是不用来保证内容不被他人获得。

 2、加密:用公钥加密,私钥解密。
 用于向公钥所有者发布信息,这个信息可能被他人篡改,但是无法被他人获得。
 
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • cipher

      protected Cipher cipher
      Cipher负责完成加密或解密工作
    • encryptBlockSize

      protected int encryptBlockSize
      加密的块大小
    • decryptBlockSize

      protected int decryptBlockSize
      解密的块大小
  • Constructor Details

    • Crypto

      public Crypto(org.miaixz.bus.core.lang.Algorithm algorithm)
      构造,创建新的私钥公钥对
      Parameters:
      algorithm - Algorithm
    • Crypto

      public Crypto(org.miaixz.bus.core.lang.Algorithm algorithm, KeyPair keyPair)
      构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
      Parameters:
      algorithm - 算法
      keyPair - 密钥对,包含私钥和公钥,如果为null,则生成随机键值对
    • Crypto

      public Crypto(String algorithm)
      构造,创建新的私钥公钥对
      Parameters:
      algorithm - 算法
    • Crypto

      public Crypto(String algorithm, KeyPair keyPair)
      构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
      Parameters:
      algorithm - 算法
      keyPair - 密钥对,包含私钥和公钥,如果为null,则生成随机键值对
    • Crypto

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

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

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

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

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

    • getEncryptBlockSize

      public int getEncryptBlockSize()
      获取加密块大小
      Returns:
      加密块大小
    • setEncryptBlockSize

      public void setEncryptBlockSize(int encryptBlockSize)
      设置加密块大小
      Parameters:
      encryptBlockSize - 加密块大小
    • getDecryptBlockSize

      public int getDecryptBlockSize()
      获取解密块大小
      Returns:
      解密块大小
    • setDecryptBlockSize

      public void setDecryptBlockSize(int decryptBlockSize)
      设置解密块大小
      Parameters:
      decryptBlockSize - 解密块大小
    • getAlgorithmParameterSpec

      public AlgorithmParameterSpec getAlgorithmParameterSpec()
      获取AlgorithmParameterSpec 在某些算法中,需要特别的参数,例如在ECIES中,此处为IESParameterSpec
      Returns:
      AlgorithmParameterSpec
    • setAlgorithmParameterSpec

      public Crypto setAlgorithmParameterSpec(AlgorithmParameterSpec algorithmParameterSpec)
      设置AlgorithmParameterSpec 在某些算法中,需要特别的参数,例如在ECIES中,此处为IESParameterSpec
      Parameters:
      algorithmParameterSpec - AlgorithmParameterSpec
      Returns:
      this
    • setRandom

      public Crypto setRandom(SecureRandom random)
      设置随机数生成器,可自定义随机数种子
      Parameters:
      random - 随机数生成器,可自定义随机数种子
      Returns:
      this
    • init

      public Crypto init(String algorithm, KeyPair keyPair)
      Description copied from class: Asymmetric
      初始化 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密(签名)或者解密(校验)
      Overrides:
      init in class Asymmetric<Crypto>
      Parameters:
      algorithm - 算法
      keyPair - 密钥对,包括私钥和公钥
      Returns:
      this
    • encrypt

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

      public byte[] decrypt(byte[] data, KeyType keyType)
      Description copied from interface: Decryptor
      解密
      Parameters:
      data - 被解密的bytes
      keyType - 私钥或公钥 KeyType
      Returns:
      解密后的bytes
    • getCipher

      public Cipher getCipher()
      获得加密或解密器
      Returns:
      加密或解密
    • initCipher

      protected void initCipher()
      初始化Cipher,默认尝试加载BC库