| | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLDecoder; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.KeyFactory; |
| | | import java.security.PrivateKey; |
| | | import java.security.PublicKey; |
| | |
| | | * @return |
| | | */ |
| | | public String encryptedData(String plainText, String pub_key) { |
| | | String publicKeyString = "公钥的Base64编码字符串"; // 前端传递的公钥字符串 |
| | | // 将公钥Base64编码字符串转换为PublicKey对象 |
| | | log.error("需要加密的数据:{}", plainText); |
| | | try { |
| | | |
| | | byte[] publicKeyBytes = Base64.getDecoder().decode(pub_key); |
| | |
| | | |
| | | // 将加密后的数据转换为Base64编码的字符串 |
| | | String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); |
| | | System.out.println("加密后的数据:" + encryptedText); |
| | | 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; |
| | | } catch (Exception e) { |
| | | log.error("加密失败了:{}", e.getMessage()); |
| | |
| | | return null; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String decodedString = null; |
| | | try { |
| | | decodedString = URLDecoder.decode("0902%E4%BB%BB%E5%8A%A1%E6%B5%8B%E8%AF%95--------", "UTF-8"); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | System.out.println(decodedString); |
| | | } |
| | | |
| | | } |