Package tech.mgl.core.tools
Class MGL_RSAUtils
java.lang.Object
tech.mgl.core.tools.MGL_RSAUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byteasc_to_bcd(byte asc) static byte[]ASCII_To_BCD(byte[] ascii, int asc_len) ASCII码转BCD码static byte[]asciiToBCD(byte[] asciiBytes) static byte[]asciiToBCD(String asciiString) static Stringbcd2Str(byte[] bytes) BCD转字符串static Stringdecrypt(MGL_RSADecrypt rsa) 私钥解密static Stringencrypt(MGL_RSAEncrypt rsa) encrypt By PublicKeystatic StringgetPublicKeyString(byte[] data) static byte[][]splitArray(byte[] data, int len) 拆分数组static String[]splitString(String string, int len) 拆分字符串
-
Constructor Details
-
MGL_RSAUtils
public MGL_RSAUtils()
-
-
Method Details
-
getPublicKeyString
-
encrypt
encrypt By PublicKey- Parameters:
rsa- rsa- Returns:
- String
- Throws:
Exception- Exception
-
asciiToBCD
public static byte[] asciiToBCD(byte[] asciiBytes) -
decrypt
私钥解密加了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
-
asc_to_bcd
public static byte asc_to_bcd(byte asc) -
bcd2Str
BCD转字符串 -
splitString
拆分字符串 -
splitArray
public static byte[][] splitArray(byte[] data, int len) 拆分数组
-