Module bus.crypto

Class Crypto

java.lang.Object
org.miaixz.bus.crypto.builtin.symmetric.Crypto
All Implemented Interfaces:
Serializable, Decryptor, Encryptor
Direct Known Subclasses:
AES, ChaCha20, DES, SM4, TDEA, ZUC

public class Crypto extends Object implements Encryptor, Decryptor, Serializable
对称加密算法 在对称加密算法中,数据发信方将明文(原始数据)和加密密钥一起经过特殊加密算法处理后,使其变成复杂的加密密文发送出去。 收信方收到密文后,若想解读原文,则需要使用加密用过的密钥及相同算法的逆算法对密文进行解密,才能使其恢复成可读明文。 在对称加密算法中,使用的密钥只有一个,发收信双方都使用这个密钥对数据进行加密和解密,这就要求解密方事先必须知道加密密钥。
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • Crypto

      public Crypto(org.miaixz.bus.core.lang.Algorithm algorithm)
      构造,使用随机密钥
      Parameters:
      algorithm - Algorithm
    • Crypto

      public Crypto(String algorithm)
      构造,使用随机密钥
      Parameters:
      algorithm - 算法,可以是"algorithm/mode/padding"或者"algorithm"
    • Crypto

      public Crypto(org.miaixz.bus.core.lang.Algorithm algorithm, byte[] key)
      构造
      Parameters:
      algorithm - 算法 Algorithm
      key - 自定义KEY
    • Crypto

      public Crypto(org.miaixz.bus.core.lang.Algorithm algorithm, SecretKey key)
      构造
      Parameters:
      algorithm - 算法 Algorithm
      key - 自定义KEY
    • Crypto

      public Crypto(String algorithm, byte[] key)
      构造
      Parameters:
      algorithm - 算法
      key - 密钥
    • Crypto

      public Crypto(String algorithm, SecretKey key)
      构造
      Parameters:
      algorithm - 算法
      key - 密钥
    • Crypto

      public Crypto(String algorithm, SecretKey key, AlgorithmParameterSpec paramsSpec)
      构造
      Parameters:
      algorithm - 算法
      key - 密钥
      paramsSpec - 算法参数,例如加盐等
  • Method Details

    • init

      public Crypto init(String algorithm, SecretKey key)
      初始化
      Parameters:
      algorithm - 算法
      key - 密钥,如果为null自动生成一个key
      Returns:
      SymmetricCrypto的子对象,即子对象自身
    • getSecretKey

      public SecretKey getSecretKey()
      获得对称密钥
      Returns:
      获得对称密钥
    • getCipher

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

      public Crypto setAlgorithmParameterSpec(AlgorithmParameterSpec algorithmParameterSpec)
      设置AlgorithmParameterSpec,通常用于加盐或偏移向量
      Parameters:
      algorithmParameterSpec - AlgorithmParameterSpec
      Returns:
      this
    • setIv

      public Crypto setIv(IvParameterSpec iv)
      设置偏移向量
      Parameters:
      iv - IvParameterSpec偏移向量
      Returns:
      自身
    • setIv

      public Crypto setIv(byte[] iv)
      设置偏移向量
      Parameters:
      iv - 偏移向量,加盐
      Returns:
      自身
    • setRandom

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

      public Crypto setMode(org.miaixz.bus.core.lang.Algorithm.Type mode)
      初始化模式并清空数据
      Parameters:
      mode - 模式枚举
      Returns:
      this
    • setMode

      public Crypto setMode(org.miaixz.bus.core.lang.Algorithm.Type mode, byte[] salt)
      初始化模式并清空数据
      Parameters:
      mode - 模式枚举
      salt - 加盐值,用于
      Returns:
      this
    • update

      public byte[] update(byte[] data)
      更新数据,分组加密中间结果可以当作随机数 第一次更新数据前需要调用setMode(Algorithm.Type)初始化加密或解密模式,然后每次更新数据都是累加模式
      Parameters:
      data - 被加密的bytes
      Returns:
      update之后的bytes
    • doFinal

      public byte[] doFinal()
      完成多部分加密或解密操作,具体取决于此密码的初始化方式。
      Returns:
      带有结果的新缓冲区
    • doFinalHex

      public String doFinalHex()
      完成多部分加密或解密操作,具体取决于此密码的初始化方式。
      Returns:
      带有结果的新缓冲区
    • updateHex

      public String updateHex(byte[] data)
      更新数据,分组加密中间结果可以当作随机数 第一次更新数据前需要调用setMode(Algorithm.Type)初始化加密或解密模式,然后每次更新数据都是累加模式
      Parameters:
      data - 被加密的bytes
      Returns:
      update之后的hex数据
    • encrypt

      public byte[] encrypt(byte[] data)
      Description copied from interface: Encryptor
      加密
      Specified by:
      encrypt in interface Encryptor
      Parameters:
      data - 被加密的bytes
      Returns:
      加密后的bytes
    • encrypt

      public byte[] encrypt(byte[] data, byte[] salt)
      加密
      Parameters:
      data - 被加密的bytes
      salt - 加盐值,如果为null不设置,否则生成带Salted__头的密文数据
      Returns:
      加密后的bytes
    • encrypt

      public void encrypt(InputStream data, OutputStream out, boolean isClose) throws org.miaixz.bus.core.lang.exception.InternalException
      Description copied from interface: Encryptor
      加密,针对大数据量,可选结束后是否关闭流
      Specified by:
      encrypt in interface Encryptor
      Parameters:
      data - 被加密的字符串
      out - 输出流,可以是文件或网络位置
      isClose - 是否关闭流
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常
    • decrypt

      public byte[] decrypt(byte[] bytes)
      Description copied from interface: Decryptor
      解密
      Specified by:
      decrypt in interface Decryptor
      Parameters:
      bytes - 被解密的bytes
      Returns:
      解密后的bytes
    • decrypt

      public void decrypt(InputStream data, OutputStream out, boolean isClose) throws org.miaixz.bus.core.lang.exception.InternalException
      Description copied from interface: Decryptor
      解密,针对大数据量,结束后不关闭流
      Specified by:
      decrypt in interface Decryptor
      Parameters:
      data - 加密的字符串
      out - 输出流,可以是文件或网络位置
      isClose - 是否关闭流,包括输入和输出流
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常