类 RSAHelper


  • public class RSAHelper
    extends Object
    RSAHelper RSA密钥使用相关
    • 构造器详细资料

      • RSAHelper

        public RSAHelper()
    • 方法详细资料

      • saveKeyPair

        public static boolean saveKeyPair​(Map<String,​Key> keyMap,
                                          String path)
        保存密钥对到指定目录
        参数:
        keyMap - 密钥对
        path - 要保存密钥对文件的路径
        返回:
        保存是否成功
      • convertToPublicKey

        public static RSAPublicKey convertToPublicKey​(String encodedBase64KeyStr)
        将字符串格式公钥转换为RSA公钥对象
        参数:
        encodedBase64KeyStr - 字符串格式公钥
        返回:
        返回RSA公钥对象
      • convertToPrivateKey

        public static RSAPrivateKey convertToPrivateKey​(String encodedBase64KeyStr)
        将字符串格式私钥转换为RSA私钥对象
        参数:
        encodedBase64KeyStr - 字符串格式私钥
        返回:
        RSA私钥对象
      • getRSAPublicKey

        public static RSAPublicKey getRSAPublicKey​(String keyFileContent)
        从指定文件读取字符串格式公钥并转换为公钥对象
        参数:
        keyFileContent - 完整密钥文件文本内容
        返回:
        公钥对象
      • getRSAPrivateKey

        public static RSAPrivateKey getRSAPrivateKey​(String keyFileContent)
        从指定文件读取字符串格式私钥并转换为私钥对象
        参数:
        keyFileContent - 完整密钥文件文本内容
        返回:
        私钥对象
      • decrypt

        public static byte[] decrypt​(PrivateKey key,
                                     byte[] cipherData)
      • decryptFromBase64String

        public static String decryptFromBase64String​(PrivateKey key,
                                                     String base64Ciphertext)
      • encrypt

        public static byte[] encrypt​(PublicKey key,
                                     byte[] plainData)
      • encryptToBase64String

        public static String encryptToBase64String​(PublicKey key,
                                                   String plaintext)
      • encryptToBase64String

        public static String encryptToBase64String​(String publicKeyStr,
                                                   String plaintext)
      • encryptToBase64String

        public static String encryptToBase64String​(String publicKeyStr,
                                                   String plaintext,
                                                   Charset charset)
      • check

        public static boolean check​(String plainText,
                                    String encodedSign,
                                    String encodedPublicKey,
                                    String signAlgorithm)
        公钥验签
        参数:
        plainText - 明文
        encodedSign - 使用Base64编码的签名
        encodedPublicKey - 使用Base64编码的公钥
        signAlgorithm - 签名算法(MD5withRSA/SHA1withRSA)
        返回:
        验签结果
      • check

        public static boolean check​(String plainText,
                                    String encodedSign,
                                    String encodedPublicKey)
      • check

        public static boolean check​(String plainText,
                                    String encodedSign,
                                    PublicKey publicKey,
                                    String signAlgorithm)
        公钥验签
        参数:
        plainText - 明文
        encodedSign - 使用Base64编码的签名
        publicKey - 公钥
        signAlgorithm - 签名算法(MD5withRSA/SHA1withRSA)
        返回:
        验签结果
      • toEncodedString

        public static String toEncodedString​(Key key)