密码系统AES私钥RSA公钥加解密的方法
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,今天小编给大家分享一下密码系统AES私钥RSA公钥加解密的方法的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下
千家信息网最后更新 2025年11月14日密码系统AES私钥RSA公钥加解密的方法
今天小编给大家分享一下密码系统AES私钥RSA公钥加解密的方法的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
前言
密钥是成对存在的,加密和解密是采用不同的密钥(公开密钥),也就是非对称密钥密码系统,每个通信方均需要两个密钥,即公钥和私钥,使用公钥进行加密操作,使用私钥进行解密操作。公钥是公开的,不需要保密,而私钥是由个人自己持有,并且必须妥善保管和注意保密。密码学里面博大精深,下面的实例仅供参考
百科的诠释
公钥(Public Key)与私钥(Private Key)是通过一种算法得到的一个密钥对(即一个公钥和一个私钥),公钥是密钥对中公开的部分,私钥则是非公开的部分。公钥通常用于加密会话密钥、验证数字签名,或加密可以用相应的私钥解密的数据。通过这种算法得到的密钥对能保证在世界范围内是唯一的。使用这个密钥对的时候,如果用其中一个密钥加密一段数据,必须用另一个密钥解密。比如用公钥加密数据就必须用私钥解密,如果用私钥加密也必须用公钥解密,否则解密将不会成功。
java使用公私钥加解密的实例
仅供参考
/** * 数据加密 plainTextData要加密的字符串 * @param plainTextData * @return * @throws Exception */ public static Map encrypt(String plainTextData) throws Exception { HashMap result = new HashMap(); // keySpec 生成对称密钥 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); SecretKey secretKey = keyGenerator.generateKey(); SecretKeySpec keySpec = new SecretKeySpec(secretKey.getEncoded(), "AES"); // RSA 用对方公钥对'对称密钥'进行加密 Cipher cipher = Cipher.getInstance("RSA"); String keyFilePathName = pertery.getProperty("bsbank_Key_path")+"PublicKey.keystore"; cipher.init(Cipher.WRAP_MODE, loadPublicKeyByStr(loadKeyByFile(keyFilePathName))); byte[] wrappedKey = cipher.wrap(keySpec); result.put("wrappedKey", Base64.encodeBase64String(wrappedKey)); // 加密数据 cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] encryptedData = cipher.doFinal(plainTextData.getBytes("UTF-8")); result.put("encryptedData", Base64.encodeBase64String(encryptedData)); return result; } /** * 数据解密 encryptedData * @param encryptedData * @return * @throws Exception */ public static Map decrypt(Map encryptedData) throws Exception { // 获取密钥 byte[] wrappedKey = Base64.decodeBase64(encryptedData.get("wrappedKey") .toString()); HashMap result = new HashMap(); // RSA解密密钥 Cipher cipher = Cipher.getInstance("RSA"); String keyFilePathName = pertery.getProperty("bsbank_Key_path")+"privateKey.keystore";//使用对方的私钥解密 cipher.init(Cipher.UNWRAP_MODE, loadPrivateKeyByStr(loadKeyByFile(keyFilePathName))); Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY); // 解密数据 cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptedData = cipher.doFinal(Base64.decodeBase64(encryptedData .get("encryptedData").toString())); result.put("decryptedData", new String(decryptedData, "UTF-8")); result.put("wrappedKey", Base64.encodeBase64String(wrappedKey)); return result; } private static String loadKeyByFile(String filePathName) throws Exception { BufferedReader br = null; StringBuilder sb = new StringBuilder(); try { br = new BufferedReader(new FileReader(filePathName)); String readLine = null; while ((readLine = br.readLine()) != null) { sb.append(readLine); } } catch (Exception e) { throw e; } finally { if (null != br) { br.close(); } } return sb.toString(); } private static RSAPublicKey loadPublicKeyByStr(String publicKeyStr) throws Exception { RSAPublicKey publicKey = null; try { byte[] buffer = Base64.decodeBase64(publicKeyStr); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (Exception e) { logger.error("failed to load pubKey", e); throw e; } return publicKey; } private static RSAPrivateKey loadPrivateKeyByStr(String privateKeyStr) throws Exception { RSAPrivateKey privateKey = null; try { byte[] buffer = Base64.decodeBase64(privateKeyStr); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (Exception e) { logger.error("failed to loadPrivateKeyByStr", e); throw e; } return privateKey; } /** * 输出公私钥对 * @param filePath * @throws Exception */ private static void genKeyPair(String filePath) throws Exception { KeyPairGenerator keyPairGen = null; try { keyPairGen = KeyPairGenerator.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { logger.error("failed to do key gen", e); throw e; } keyPairGen.initialize(1024, new SecureRandom()); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); try { String publicKeyString = Base64.encodeBase64String(publicKey .getEncoded()); String privateKeyString = Base64.encodeBase64String(privateKey .getEncoded()); FileWriter pubfw = new FileWriter(filePath + "/PublicKey.keystore"); FileWriter prifw = new FileWriter(filePath + "/PrivateKey.keystore"); BufferedWriter pubbw = new BufferedWriter(pubfw); BufferedWriter pribw = new BufferedWriter(prifw); pubbw.write(publicKeyString); pribw.write(privateKeyString); pubbw.flush(); pubbw.close(); pubfw.close(); pribw.flush(); pribw.close(); prifw.close(); } catch (IOException e) { logger.error("failed to genKeypair", e); } }以上就是"密码系统AES私钥RSA公钥加解密的方法"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。
密钥
公钥
加密
数据
密码
知识
篇文章
系统
对称
方法
不同
仅供参考
公私
内容
实例
对方
算法
部分
面的
UTF-8
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
电脑连接天路云打印服务器
服务器2t硬盘
扫服务器教程
楼月软件开发有限公司
服务好质量好的聊天软件开发
软件开发access
excel列标记数据库
广州纷享网络技术有限公司
钢铁行业网络安全工作方案厂家
打印机服务器属性
网络安全主题班会课件ppt
发生网络安全事件时规定
人脸识别和网络安全论文
本科做软件开发前景如何
删除打印服务器默认表单
苏州云服务器控制中心
全球细胞生物学的数据库
2017三级网络技术考纲
access数据库打表格
宿豫区自动化网络技术参考价格
水利网络安全管理办法(试行
吃鸡怎么改服务器
美国有5g网络技术吗
还原数据库出现错误
北邮网络安全学院研究生导师官网
访问数据库可以查资料吗
网络安全教育课案例
软件开发西安公司
服务器系统自动运行
获取邮箱服务器地址