1、
2、
| import cn.hutool.core.codec.Base64Decoder; |
| import cn.hutool.crypto.asymmetric.KeyType; |
| import cn.hutool.crypto.asymmetric.RSA; |
| import lombok.extern.slf4j.Slf4j; |
| import org.jeecg.common.util.CN; |
| |
| import java.io.BufferedReader; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| import java.security.KeyFactory; |
| import java.security.PrivateKey; |
| import java.security.PublicKey; |
| import java.security.spec.PKCS8EncodedKeySpec; |
| import java.security.spec.X509EncodedKeySpec; |
| |
| @Slf4j |
| public class RSAUtils { |
| private static RSA rsa; |
| |
| static { |
| try { |
| rsa = new RSA(); |
| rsa.setPublicKey(getPublicKeyFromPem("pem/public.pem")); |
| rsa.setPrivateKey(getPrivateKeyFromPem("pem/private.pem")); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| public static void main(String[] args) { |
| String s1 = "大概和我说fadg`12@$%&+%"; |
| String s2 = encrypt(s1); |
| String s3 = decrypt(s2); |
| System.out.printf("======== \ns1=%s \ns2=%s \ns3=%s \ns1.equals(s3)=%s \n", s1, s2, s3, s1.equals(s3)); |
| } |
| |
| public static String encrypt(String data) { |
| String cc = rsa.encryptBase64(data, KeyType.PublicKey); |
| return cc; |
| } |
| |
| public static String decrypt(String data) { |
| String cc = rsa.decryptStr(data, KeyType.PrivateKey); |
| return cc; |
| } |
| |
| public static byte[] encrypt(byte[] data) { |
| byte[] bytes = rsa.encrypt(data, KeyType.PublicKey); |
| return bytes; |
| } |
| |
| public static byte[] decrypt(byte[] data) { |
| byte[] bytes = rsa.decrypt(data, KeyType.PrivateKey); |
| return bytes; |
| } |
| |
| private static PrivateKey getPrivateKeyFromPem(String fileName) { |
| try { |
| String str = getResourceFileContent(fileName); |
| byte[] b = Base64Decoder.decode(str); |
| KeyFactory kf = KeyFactory.getInstance("RSA"); |
| PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(b); |
| PrivateKey privateKey = kf.generatePrivate(keySpec); |
| return privateKey; |
| } catch (Exception e) { |
| throw new RuntimeException(e); |
| } |
| } |
| |
| private static PublicKey getPublicKeyFromPem(String fileName) { |
| try { |
| String str = getResourceFileContent(fileName); |
| byte[] b = Base64Decoder.decode(str); |
| KeyFactory kf = KeyFactory.getInstance("RSA"); |
| X509EncodedKeySpec keySpec = new X509EncodedKeySpec(b); |
| PublicKey pubKey = kf.generatePublic(keySpec); |
| return pubKey; |
| } catch (Exception e) { |
| throw new RuntimeException(e); |
| } |
| } |
| |
| private static String getResourceFileContent(String fileName) { |
| InputStream publicKeyStream = RSAUtils.class.getClassLoader().getResourceAsStream(fileName); |
| try (BufferedReader br = new BufferedReader(new InputStreamReader(publicKeyStream))) { |
| String line; |
| StringBuilder str = new StringBuilder(); |
| while (CN.isNotEmpty((line = br.readLine()))) { |
| if (line.startsWith("-")) { |
| continue; |
| } |
| str.append(line); |
| } |
| return str.toString(); |
| } catch (Exception e) { |
| throw new RuntimeException(e); |
| } |
| } |
| } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2021-05-30 Java Windows下OpenCV环境配置