Class MGL_RSAUtils

java.lang.Object
tech.mgl.core.tools.MGL_RSAUtils

public class MGL_RSAUtils extends Object
  • Constructor Details

    • MGL_RSAUtils

      public MGL_RSAUtils()
  • Method Details

    • getPublicKeyString

      public static String getPublicKeyString(byte[] data)
    • encrypt

      public static String encrypt(MGL_RSAEncrypt rsa) throws Exception
      encrypt By PublicKey
      Parameters:
      rsa - rsa
      Returns:
      String
      Throws:
      Exception - Exception
    • asciiToBCD

      public static byte[] asciiToBCD(byte[] asciiBytes)
    • decrypt

      public static String decrypt(MGL_RSADecrypt rsa) throws Exception
      私钥解密

      加了if判断,否则会报错BadPaddingException

      Parameters:
      rsa - rsa
      Returns:
      String
      Throws:
      Exception - 但是明文长度不能超过密钥长度。比如 Java 默认的 RSA 加密实现不允许明文长度超过密钥长度减去 11(单位是字节,也就是 byte)。也就是说,如果我们定义的密钥(我们可以通过 java.security.KeyPairGenerator.initialize(int keysize) 来定义密钥长度)长度为 1024(单位是位,也就是 bit),生成的密钥长度就是 1024位 / 8位/字节 = 128字节,那么我们需要加密的明文长度不能超过 128字节 - 11 字节 = 117字节。也就是说,我们最大能将 117 字节长度的明文进行加密,否则会出问题(抛诸如 javax.crypto.IllegalBlockSizeException: Data must not be longer than 53 bytes 的异常)。 而 BC 提供的加密算法能够支持到的 RSA 明文长度最长为密钥长度。
    • ASCII_To_BCD

      public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len)
      ASCII码转BCD码
    • asciiToBCD

      public static byte[] asciiToBCD(String asciiString)
    • asc_to_bcd

      public static byte asc_to_bcd(byte asc)
    • bcd2Str

      public static String bcd2Str(byte[] bytes)
      BCD转字符串
    • splitString

      public static String[] splitString(String string, int len)
      拆分字符串
    • splitArray

      public static byte[][] splitArray(byte[] data, int len)
      拆分数组