Class CypherUtil

java.lang.Object
cn.sinozg.applet.common.utils.CypherUtil

public class CypherUtil extends Object
加密工具类,获取随机数。
  • Field Details

    • AES_ALGORITHM

      public static final String AES_ALGORITHM
      See Also:
    • RSA_ALGORITHM

      public static final String RSA_ALGORITHM
      See Also:
    • PKCS7

      private static final String PKCS7
      See Also:
    • ENCODER

      private static final org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder ENCODER
    • log

      private static final org.slf4j.Logger log
  • Constructor Details

    • CypherUtil

      private CypherUtil()
  • Method Details

    • signature

      public static boolean signature(boolean sha2, String signature, String... params)
      签名判断 参数排序拼接后 签名再与原始的签名比较
      Parameters:
      sha2 - 是否为 sha2
      signature - 签名字符串
      params - 要签名的数据
      Returns:
      是否与签名一致
    • signMessage

      public static String signMessage(boolean sha2, String... params)
      签名
      Parameters:
      sha2 - sha2 是否为 sha2
      params - 要签名的数据
      Returns:
      签名值
    • wxDecrypt

      public static String wxDecrypt(String encryptedData, String sessionKey, String iv)
      解密微信用户信息
      Parameters:
      encryptedData - 加密数据
      sessionKey - 解密秘钥
      iv - 初始向量
      Returns:
      解密以后的数据
    • encoder

      public static String encoder(String rawPassword)
      加密信息
      Parameters:
      rawPassword - 原始密码
      Returns:
      加密后的密码
    • matches

      public static boolean matches(CharSequence rawPassword, String encodedPassword)
      密码匹配
      Parameters:
      rawPassword - 原始密码
      encodedPassword - 加密后的密码
      Returns:
      是否匹配
    • md5Upper

      public static String md5Upper(String input)
      md5 加密返回大写
      Parameters:
      input - 要加密的数据
      Returns:
      md5
    • md5

      public static String md5(String input)
      md5 加密
      Parameters:
      input - 要加密的数据
      Returns:
      md5
    • decryptJson

      public static byte[] decryptJson(String content, String rsaPrivateKey, String aesKey) throws Exception
      解密前端传过来的加密数据

      先用RSA私钥(java)解密前端传过来的aes加密信息,得到前端生成的随机aes key

      再用 aes key解密得到数据

      Parameters:
      content - 加密数据
      rsaPrivateKey - RSA私钥
      aesKey - aes 加密后的数据
      Returns:
      解密后的数据
      Throws:
      Exception
    • encrypt

      public static Object encrypt(jakarta.servlet.http.HttpServletRequest request, Object data) throws IOException
      加密数据返回到前端
      Parameters:
      request - request
      data - 请求参数
      Returns:
      请求参数或者加密后的数据
      Throws:
      IOException
    • encrypt

      private static AesRsaEncrypt encrypt(String content, String rsaPublicKey) throws Exception
      随机生成16位的 aes私钥

      用aes 私钥加密json得到加密信息

      用前端传过来的 RSA 公钥加密 aes私钥 得到aes私钥加密信息

      Parameters:
      content - 加密信息
      rsaPublicKey - 前端传过来的RSA公钥
      Returns:
      加密信息
      Throws:
      Exception
    • rsaKey

      public static Key rsaKey(String key, boolean isPublic) throws Exception
      通过字符串等到 key对象
      Parameters:
      key - key的字符串
      isPublic - 是否为公钥
      Returns:
      对象
      Throws:
      Exception - 异常
    • decrypt

      public static byte[] decrypt(String transformation, Key key, String input)
      解密数据
      Parameters:
      transformation - 加密方式
      key - 密钥
      input - 数据
      Returns:
      返回操作后的结果
    • encrypt

      public static String encrypt(String transformation, Key key, String input)
      加密数据
      Parameters:
      transformation - 加密方式
      key - 密钥
      input - 数据
      Returns:
      返回操作后的结果
    • doFinalRsa

      private static byte[] doFinalRsa(String transformation, int opMode, Key key, byte[] input)
      rsa 加密或者解密数据 分段
      Parameters:
      transformation - 加密方式
      opMode - 加密、解密
      key - 密钥
      input - 数据
      Returns:
      返回操作后的结果
    • doFinalAes

      public static byte[] doFinalAes(String transformation, boolean decrypt, byte[] input, Key aesKey)
      aes 签名
      Parameters:
      transformation - 签名方法
      decrypt - 加密或者解密
      input - 文本信息
      aesKey - 密钥
      Returns:
      加解密后的字节信息
    • doFinalAes

      public static byte[] doFinalAes(String transformation, boolean decrypt, byte[] input, byte[] aesKey, byte[] ivKey)
      aes 签名
      Parameters:
      transformation - 签名方法
      decrypt - 加密或者解密
      input - 文本信息
      aesKey - 密钥
      ivKey - 密钥偏移量
      Returns:
      加解密后的字节信息
    • doFinalAes

      private static byte[] doFinalAes(String transformation, boolean decrypt, byte[] input, byte[] aesKey, byte[] ivKey, Key aesPriKey)
      aes 签名
      Parameters:
      transformation - 签名方法
      decrypt - 加密或者解密
      input - 文本信息
      aesKey - 密钥
      ivKey - 偏移量
      aesPriKey - 密钥
      Returns:
      加解密后的字节信息
    • getAesKey

      private static String getAesKey() throws Exception
      获取随机的 aes密钥 必须是 8的倍数
      Returns:
      密钥
      Throws:
      Exception
    • getKeyPair

      public static KeyPair getKeyPair() throws Exception
      生成秘钥对
      Returns:
      rsa密钥对
      Throws:
      Exception - 异常