JAVA中使用RSA通过秘钥文件对字符串进行加密解密
技术交流群: 233513714
//字符串进行加密算法的名称 public static final String ALGORITHM = "RSA"; //字符串进行加密填充的名称 public static final String PADDING = "RSA/NONE/NoPadding"; //字符串持有安全提供者的名称 public static final String PROVIDER = "BC";
//私钥文件路径(RSAUtil是RSA工具类的类名) public static final String PRIVATE_KEY_FILE = RSAUtil.class.getClassLoader().getResource("").getPath() + "key" + "private_response_key_1.key"; //公钥文件路径
public static final String PUBLIC_KEY_FILE = RSAUtil.class.getClassLoader().getResource("").getPath() + "key" + "public_request_key_1.key"; /** * 测试加密解密 */ public void rsaTest(String str) { log.info("[要加密解密的参数:{}]", str); try { String cipherText = encrypt(str); String plainText = decrypt(cipherText); log.info("[加密后的参数为:{}]", cipherText); log.info("[解密后的参数为:{}]", plainText); } catch (Exception e) { log.info("[RSA加密解密出现异常:{}]", e); } } /** * 将字符串进行RSA加密 * * @param text * @return */ public static String encrypt(String text) { String cipherTextBase64 = ""; try { ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE)); PublicKey publicKey = (PublicKey) inputStream.readObject(); Security.addProvider(new BouncyCastleProvider()); Cipher cipher = Cipher.getInstance(PADDING, PROVIDER); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherText = cipher.doFinal(text.getBytes()); Base64 base64 = new Base64(); cipherTextBase64 = base64.encodeToString(cipherText); } catch (Exception e) { log.info("[字符串进行RSA加密出现异常:{}]", e); } return cipherTextBase64; }
/** * 将字符串进行RSA解密 * * @param str * @return */ public static String decrypt(String str) { byte[] dectyptedText = null; try { ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE)); PrivateKey privateKey = (PrivateKey) inputStream.readObject(); Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); Cipher cipher = Cipher.getInstance(PADDING, PROVIDER); cipher.init(Cipher.DECRYPT_MODE, privateKey); Base64 base64 = new Base64(); byte[] text = base64.decode(str); dectyptedText = cipher.doFinal(text); } catch (Exception e) { log.info("[字符串进行RSA解密出现异常:{}]", e); } return new String(dectyptedText); }
/** * 判断秘钥文件是否存在 * * @return */ public static boolean areKeysPresent() { File privateKey = new File(PRIVATE_KEY_FILE); File publicKey = new File(PUBLIC_KEY_FILE); if (privateKey.exists() && publicKey.exists()) { return true; } return false; } /** * 生成公钥文件和私钥文件 */ public static void generateKey() { try { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM, PROVIDER); keyGen.initialize(1024); final KeyPair key = keyGen.generateKeyPair(); File privateKeyFile = new File(PRIVATE_KEY_FILE); File publicKeyFile = new File(PUBLIC_KEY_FILE); if (privateKeyFile.getParentFile() != null) { privateKeyFile.getParentFile().mkdirs(); } privateKeyFile.createNewFile(); if (publicKeyFile.getParentFile() != null) { publicKeyFile.getParentFile().mkdirs(); } publicKeyFile.createNewFile(); ObjectOutputStream publicKeyOS = new ObjectOutputStream(new FileOutputStream(publicKeyFile)); publicKeyOS.writeObject(key.getPublic()); publicKeyOS.close(); ObjectOutputStream privateKeyOS = new ObjectOutputStream(new FileOutputStream(privateKeyFile)); privateKeyOS.writeObject(key.getPrivate()); privateKeyOS.close(); } catch (Exception e) { log.info("[生成公钥文件和私钥文件出现异常{}]", e); } }
标签:
JAVA中使用RSA通过秘钥文件对字符串进行加密解密
, java中rsa
, rsa加密
, rsa解密
, java中rsa加密解密
, java中读取rsa公钥文件
, java中读取rsa私钥文件
, java中生成rsa秘钥文件
, java秘钥文件
, java读取秘钥文件
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示