rsa私钥加密 公钥解密,rsa+aes加密

  rsa私钥加密 公钥解密,rsa+aes加密

  00-1010前言百科释义使用公钥和私钥的java加密和解密示例

  00-1010密钥成对存在,使用不同的密钥(公钥)进行加密和解密,即非对称密钥密码体制。每个通信方需要两个密钥,即公钥和私钥。公钥用于加密,私钥用于解密。公钥是公开的,不需要保密,而私钥是个人自己持有的,必须妥善保管和保密。密码学博大精深,以下例子仅供参考。

  00-1010公钥和私钥是通过算法获得的密钥对(即公钥和私钥)。公钥是密钥对的公开部分,而私钥是非公开部分。公钥通常用于加密会话密钥、验证数字签名或加密可以用相应的私钥解密的数据。该算法得到的密钥对可以保证在世界上是唯一的。使用这个密钥对时,如果一段数据用一个密钥加密,就必须用另一个密钥解密。例如,用公钥加密的数据必须用私钥解密。如果用私钥加密,也必须用公钥解密,否则解密不会成功。

  00-1010仅供参考。

  /* * *数据加密要加密的plainTextData字符串* @ param plain text data * @ return * @ throws exception */public static map encrypt(string plain text data)throws exception { hashmap result=new hashmap();//keySpec生成对称密钥key generator key generator=key generator . getinstance( AES );key generator . init(128);secret key secret key=key generator . generate key();SecretKeySpec key spec=new SecretKeySpec(secretkey . get encoded(), AES );//RSA用对方的公钥加密‘对称密钥’。cipher cipher=cipher . getinstance( RSA );string keyfile pathname=pertery . getproperty( bs bank _ Key _ path ) public Key . keystore ;cipher.init(密码。WRAP_MODE,loadPublicKeyByStr(loadKeyByFile(keyfile pathname));byte[]wrapped key=cipher . wrap(key spec);result.put(wrappedKey ,base 64 . encode base 64 string(wrap pedkey));//加密数据cipher=cipher . getinstance( AES );cipher.init(密码。ENCRYPT_MODE,key spec);byte[]encrypted data=cipher . do final(plain text data . getbytes( UTF-8 );result.put(encryptedData ,base 64 . encode base 64 string(encrypted data));返回结果;}/* * *数据解密加密数据* @ param加密数据* @ return * @ throws exception */public static map decrypt(map encrypted data)throws exception {//Get key byte[]wrapped key=base 64 . decode base 64(encrypted data . Get( wrapped key )。tostring());HashMap结果=new HashMap();//RSA解密密钥cipher=cipher . getinstance( RSA );string Key file pathname=pertery . getproperty( bs bank _ Key _ path ) private ke

  y.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公钥的加解密示例的详细内容,更多关于AES RSA公私钥加解密的资料请关注盛行IT其它相关文章!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: