密碼系統(tǒng)AES私鑰RSA公鑰的加解密示例
前言
密鑰是成對(duì)存在的,加密和解密是采用不同的密鑰(公開密鑰),也就是非對(duì)稱密鑰密碼系統(tǒng),每個(gè)通信方均需要兩個(gè)密鑰,即公鑰和私鑰,使用公鑰進(jìn)行加密操作,使用私鑰進(jìn)行解密操作。公鑰是公開的,不需要保密,而私鑰是由個(gè)人自己持有,并且必須妥善保管和注意保密。密碼學(xué)里面博大精深,下面的實(shí)例僅供參考
百科的詮釋
公鑰(Public Key)與私鑰(Private Key)是通過一種算法得到的一個(gè)密鑰對(duì)(即一個(gè)公鑰和一個(gè)私鑰),公鑰是密鑰對(duì)中公開的部分,私鑰則是非公開的部分。公鑰通常用于加密會(huì)話密鑰、驗(yàn)證數(shù)字簽名,或加密可以用相應(yīng)的私鑰解密的數(shù)據(jù)。通過這種算法得到的密鑰對(duì)能保證在世界范圍內(nèi)是唯一的。使用這個(gè)密鑰對(duì)的時(shí)候,如果用其中一個(gè)密鑰加密一段數(shù)據(jù),必須用另一個(gè)密鑰解密。比如用公鑰加密數(shù)據(jù)就必須用私鑰解密,如果用私鑰加密也必須用公鑰解密,否則解密將不會(huì)成功。
java使用公私鑰加解密的實(shí)例
僅供參考
/**
* 數(shù)據(jù)加密 plainTextData要加密的字符串
* @param plainTextData
* @return
* @throws Exception
*/
public static Map encrypt(String plainTextData)
throws Exception {
HashMap result = new HashMap();
// keySpec 生成對(duì)稱密鑰
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
SecretKeySpec keySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
// RSA 用對(duì)方公鑰對(duì)‘對(duì)稱密鑰'進(jìn)行加密
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));
// 加密數(shù)據(jù)
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;
}
/**
* 數(shù)據(jù)解密 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";//使用對(duì)方的私鑰解密
cipher.init(Cipher.UNWRAP_MODE,
loadPrivateKeyByStr(loadKeyByFile(keyFilePathName)));
Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
// 解密數(shù)據(jù)
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;
}
/**
* 輸出公私鑰對(duì)
* @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公鑰的加解密示例的詳細(xì)內(nèi)容,更多關(guān)于AES RSA公私鑰加解密的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決spring結(jié)合mybatis時(shí)一級(jí)緩存失效的問題
這篇文章主要介紹了解決spring結(jié)合mybatis時(shí)一級(jí)緩存失效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Java多線程案例實(shí)戰(zhàn)之定時(shí)器的實(shí)現(xiàn)
在Java中可以使用多線程和定時(shí)器來實(shí)現(xiàn)定時(shí)任務(wù),下面這篇文章主要給大家介紹了關(guān)于Java多線程案例之定時(shí)器實(shí)現(xiàn)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Java數(shù)據(jù)庫(kù)連接PreparedStatement的使用詳解
這篇文章主要介紹了Java數(shù)據(jù)庫(kù)連接PreparedStatement的使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Java模擬計(jì)算機(jī)的整數(shù)乘積計(jì)算功能示例
這篇文章主要介紹了Java模擬計(jì)算機(jī)的整數(shù)乘積計(jì)算功能,簡(jiǎn)單分析了計(jì)算機(jī)數(shù)值進(jìn)制轉(zhuǎn)換與通過位移進(jìn)行乘積計(jì)算的原理,并結(jié)合具體實(shí)例給出了java模擬計(jì)算機(jī)成績(jī)運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
詳解SpringMVC使用MultipartFile實(shí)現(xiàn)文件的上傳
本篇文章主要介紹了SpringMVC使用MultipartFile實(shí)現(xiàn)文件的上傳,本地的文件上傳到資源服務(wù)器上,比較好的辦法就是通過ftp上傳。這里是結(jié)合SpringMVC+ftp的形式上傳的,有興趣的可以了解一下。2016-12-12

