|
|
|
|
@ -2,8 +2,10 @@ package com.ynxbd.common.helper.common; |
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.apache.commons.codec.binary.Base64; |
|
|
|
|
import org.apache.commons.lang3.ObjectUtils; |
|
|
|
|
|
|
|
|
|
import javax.crypto.Cipher; |
|
|
|
|
import java.net.URLEncoder; |
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.security.*; |
|
|
|
|
import java.security.interfaces.RSAPrivateKey; |
|
|
|
|
@ -100,6 +102,8 @@ public class RSAHelper { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* RSA私钥解密 |
|
|
|
|
* |
|
|
|
|
@ -132,11 +136,44 @@ public class RSAHelper { |
|
|
|
|
return new String(cipher.doFinal(inputByte)); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
log.error("[RSA]解密失败:[{}]" + e.getMessage()); |
|
|
|
|
log.error("[RSA]解密失败:[{}]", e.getMessage()); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String URLEncode(String data) { |
|
|
|
|
try { |
|
|
|
|
if (ObjectUtils.isEmpty(data)) return null; |
|
|
|
|
return URLEncoder.encode(data, String.valueOf(StandardCharsets.UTF_8)); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 加密方法(PKCS#1 v1.5填充)
|
|
|
|
|
public static String enPKCS1Padding(String plaintext, PublicKey publicKey) throws Exception { |
|
|
|
|
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); |
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey); |
|
|
|
|
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8)); |
|
|
|
|
return java.util.Base64.getEncoder().encodeToString(encryptedBytes); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 解密方法
|
|
|
|
|
public static String dePKCS1Padding(String encryptedText, PrivateKey privateKey) throws Exception { |
|
|
|
|
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); |
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, privateKey); |
|
|
|
|
byte[] decryptedBytes = cipher.doFinal(java.util.Base64.getDecoder().decode(encryptedText)); |
|
|
|
|
return new String(decryptedBytes, StandardCharsets.UTF_8); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 去除空格、制表符、换行符等连续或多个空白符
|
|
|
|
|
public static String replaceKey(String key) { |
|
|
|
|
if (key == null) return null; |
|
|
|
|
return key.replaceAll("\\s+", ""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception { |
|
|
|
|
for (int i = 0; i < 100; i++) { |
|
|
|
|
|