| | |
| | | public class RSAPublicKeyExample { |
| | | |
| | | |
| | | /** |
| | | * 数据解密 |
| | | * |
| | | * @param encryptedData |
| | | * @return |
| | | */ |
| | | public String decryptedData(String encryptedData, String pri_key) { |
| | | String privateKeyString = "私钥的Base64编码字符串"; // 后端私钥的Base64编码字符串 |
| | | |
| | | public static String encryptedData(String plainText, String pubKey) { |
| | | try { |
| | | // 将私钥Base64编码字符串转换为PrivateKey对象 |
| | | byte[] privateKeyBytes = Base64.getDecoder().decode(pri_key); |
| | | PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); |
| | | KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| | | PrivateKey privateKey = keyFactory.generatePrivate(keySpec); |
| | | |
| | | // 使用私钥解密数据 |
| | | Cipher decryptCipher = Cipher.getInstance("RSA"); |
| | | decryptCipher.init(Cipher.DECRYPT_MODE, privateKey); |
| | | byte[] decryptedBytes = decryptCipher.doFinal(Base64.getDecoder().decode(encryptedData)); |
| | | |
| | | // 解密后的数据 |
| | | String decryptedData = new String(decryptedBytes); |
| | | System.out.println("解密后的数据:" + decryptedData); |
| | | return decryptedData; |
| | | } catch (Exception e) { |
| | | log.error("解密报错了:{}", e.getMessage()); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 要加密的明文数据 |
| | | * |
| | | * @param plainText |
| | | * @return |
| | | */ |
| | | public String encryptedData(String plainText, String pub_key) { |
| | | log.error("需要加密的数据:{}", plainText); |
| | | try { |
| | | |
| | | byte[] publicKeyBytes = Base64.getDecoder().decode(pub_key); |
| | | byte[] publicKeyBytes = Base64.getDecoder().decode(pubKey); |
| | | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); |
| | | KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| | | PublicKey publicKey = keyFactory.generatePublic(keySpec); |
| | | |
| | | // 使用公钥加密数据 |
| | | Cipher encryptCipher = Cipher.getInstance("RSA"); |
| | | encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey); |
| | | byte[] encryptedBytes = encryptCipher.doFinal(plainText.getBytes()); |
| | | Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); |
| | | cipher.init(Cipher.ENCRYPT_MODE, publicKey); |
| | | byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); |
| | | |
| | | // 将加密后的数据转换为Base64编码的字符串 |
| | | // Base64 编码(必须保留) |
| | | String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); |
| | | log.error("Base64加密后的数据:{}", encryptedText); |
| | | encryptedText = URLEncoder.encode(encryptedText, StandardCharsets.UTF_8.toString()); |
| | | log.error("URLEncoder编码后的数据:{}", encryptedText); |
| | | String decodedString = URLDecoder.decode(encryptedText, "UTF-8"); |
| | | log.error("URLEncoder解码后的数据:{}", decodedString); |
| | | return encryptedText; |
| | | |
| | | // 可选:如果用于 URL 传输,再进行 URL 编码 |
| | | return URLEncoder.encode(encryptedText, StandardCharsets.UTF_8.toString()); |
| | | } catch (Exception e) { |
| | | log.error("加密失败了:{}", e.getMessage()); |
| | | System.err.println("加密失败: " + e.getMessage()); |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * RSA 私钥解密(解密 Base64 编码后的密文,支持 URL 解码) |
| | | */ |
| | | public static String decryptedData(String encryptedData, String priKey) { |
| | | try { |
| | | // 可选:先 URL 解码(如果加密前做了 URLEncoder) |
| | | String base64Encrypted = URLDecoder.decode(encryptedData, StandardCharsets.UTF_8.toString()); |
| | | |
| | | // Base64 解码 |
| | | byte[] encryptedBytes = Base64.getDecoder().decode(base64Encrypted); |
| | | |
| | | byte[] privateKeyBytes = Base64.getDecoder().decode(priKey); |
| | | PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); |
| | | KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| | | PrivateKey privateKey = keyFactory.generatePrivate(keySpec); |
| | | |
| | | Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); |
| | | cipher.init(Cipher.DECRYPT_MODE, privateKey); |
| | | byte[] decryptedBytes = cipher.doFinal(encryptedBytes); |
| | | |
| | | return new String(decryptedBytes, StandardCharsets.UTF_8); |
| | | } catch (Exception e) { |
| | | System.err.println("解密失败: " + e.getMessage()); |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | String decodedString = null; |
| | | try { |