类 RsaCrypto


  • public class RsaCrypto
    extends Object
    RSA 非对称加密/解密
    • 构造器详细资料

      • RsaCrypto

        public RsaCrypto()
    • 方法详细资料

      • init

        public static Map<String,​byte[]> init()
        初始化密钥

        注意这里是生成密钥对 KeyPair,再由密钥对获取公私钥

        返回:
        密钥对
      • sign

        public static String sign​(String privateKey,
                                  byte[] data)
        用私钥对信息生成数字签名
        参数:
        privateKey - 私钥
        data - 加密数据
        返回:
        数字签名
      • sign

        public static byte[] sign​(String algorithmName,
                                  PrivateKey privateKey,
                                  byte[] data)
      • verify

        public static boolean verify​(byte[] data,
                                     String publicKey,
                                     String sign)
        校验数字签名
        参数:
        data - 加密数据
        publicKey - 公钥
        sign - 数字签名
        返回:
        校验成功返回true,失败返回 false
      • encryptByPublicKey

        public static byte[] encryptByPublicKey​(byte[] data,
                                                String key)
        公钥加密
        参数:
        data - 待加密数据
        key - 公钥
        返回:
        加密后的字节数组
      • decryptByPublicKey

        public static byte[] decryptByPublicKey​(byte[] data,
                                                String key)
        公钥解密
        参数:
        data - 加密数据
        key - 公钥
        返回:
        解密后的字节数组
      • encryptByPrivateKey

        public static byte[] encryptByPrivateKey​(byte[] data,
                                                 String key)
        私钥加密
        参数:
        data - 待加密数据
        key - 私钥
        返回:
        加密后的字节数组
      • decryptByPrivateKey

        public static byte[] decryptByPrivateKey​(byte[] data,
                                                 String key)
        私钥解密
        参数:
        data - 加密数据
        key - 私钥
        返回:
        解密后的字节数组
      • getPublicKey

        public static String getPublicKey​(Map<String,​byte[]> map)
        获取公钥
        参数:
        map - 个字符串到字节数组的映射,包含密钥对等信息
        返回:
        公钥
      • getPrivateKey

        public static String getPrivateKey​(Map<String,​byte[]> map)
        获取私钥
        参数:
        map - 个字符串到字节数组的映射,包含密钥对等信息
        返回:
        私钥
      • getKey

        public static String getKey​(String name,
                                    Map<String,​byte[]> map)
        获取指定名称的密钥
        参数:
        name - 密钥名称
        map - 密钥映射表
        返回:
        密钥的 Base64 编码
      • getKeyPair

        public static Map<String,​byte[]> getKeyPair​(KeyPairGenerator generator,
                                                          String publicKey,
                                                          String privateKey)
        生成一对密钥,并返回密钥对的 Base64 编码
        参数:
        generator - 密钥生成器
        publicKey - 公钥名称
        privateKey - 私钥名称
        返回:
        密钥对的 Base64 编码
      • getKeyPair

        public static Map<String,​byte[]> getKeyPair​(String algorithmName,
                                                          int keySize,
                                                          String publicKey,
                                                          String privateKey)
        生成密钥对
        参数:
        algorithmName - 算法
        keySize - 密钥长度
        publicKey - 公钥文件路径
        privateKey - 私钥文件路径
        返回:
        密钥对