java加解密之RSA使用
來源:程序員人生 發布時間:2016-09-30 10:53:57 閱讀次數:2667次
最近為了分析1段要求流,不能不去研究1下RSA加密。
首先,強調1點:密鑰的“鑰”讀“yue”,不是“yao”,額。。。
網上關于RSA的原理1抓1大把的,這里只是簡單說說我的理解:
1. 兩個足夠大的互質數p, q;
2. 用于模運算的模 n=p*q;
3. 公鑰KU(e, n)中的e滿足 1<e< (p⑴)(q⑴),且與(p⑴)(q⑴)互質;
4. 密鑰KR(d, n)中的d滿足 d*e % (p⑴)(q⑴)= 1,%是取余運算。
由于公鑰是公然的,所以我知道了e和n,那末根據2,3,4式子的關系,我們只要從n的值推出p, q的值則可計算出d的值,也就可以找到密鑰。
但是,關鍵就在這里, n=p*q,如果兩個互質數p和q足夠大,那末根據目前的計算機技術和其他工具,至今也沒能從n分解出p和q,這是數學上的1個困難,也正是這個困難成了RSA加密至今被廣泛使用的緣由。換句話說,只要密鑰長度n足夠大(1般1024足矣),基本上不可能從公鑰信息推出私鑰信息。
好了,這里作為研究的隨筆,記錄1下java如何使用,以下主要有3種方法,基本大同小異,只是獲得公鑰私鑰的途徑不1樣就是了:
方法1:
利用KeyPairGenerator直接生成公鑰和密鑰,1般私鑰保存給服務端,公鑰交給客戶端。
public class RSACryptography {
public static String data="hello world";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
KeyPair keyPair=genKeyPair(1024);
//獲得公鑰,并以base64格式打印出來
PublicKey publicKey=keyPair.getPublic();
System.out.println("公鑰:"+new String(Base64.getEncoder().encode(publicKey.getEncoded())));
//獲得私鑰,并以base64格式打印出來
PrivateKey privateKey=keyPair.getPrivate();
System.out.println("私鑰:"+new String(Base64.getEncoder().encode(privateKey.getEncoded())));
//公鑰加密
byte[] encryptedBytes=encrypt(data.getBytes(), publicKey);
System.out.println("加密后:"+new String(encryptedBytes));
//私鑰解密
byte[] decryptedBytes=decrypt(encryptedBytes, privateKey);
System.out.println("解密后:"+new String(decryptedBytes));
}
//生成密鑰對
public static KeyPair genKeyPair(int keyLength) throws Exception{
KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
return keyPairGenerator.generateKeyPair();
}
//公鑰加密
public static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception{
Cipher cipher=Cipher.getInstance("RSA");//java默許"RSA"="RSA/ECB/PKCS1Padding"
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(content);
}
//私鑰解密
public static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception{
Cipher cipher=Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(content);
}
}
運行結果:

方法2:
實際上,方法1只是用來生成密鑰就OK了,生成的密鑰需要保存到本地文件中,所以1般不會在客戶端調用KeyPairGenerator進行密鑰的生成操作。
這里,我們可以將方法1得到的密鑰保存到文件,下次我們直接讀取就能夠了。我假定以String的情勢保存在文件內,那末接下來直接使用讀取到的String生成密鑰便可。
固然,你也能夠使用openssl來生成也能夠,不過我覺得麻煩就不弄了。
public class RSACryptography {
public static String data="hello world";
public static String publicKeyString="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCISLP98M/56HexX/9FDM8iuIEQozy6kn2JMcbZS5/BhJ+U4PZIChJfggYlWnd8NWn4BYr2kxxyO8Qgvc8rpRZCkN0OSLqLgZGmNvoSlDw80UXq90ZsVHDTOHuSFHw8Bv//B4evUNJBB8g9tpVxr6P5EJ6FMoR/kY2dVFQCQM4+5QIDAQAB";
public static String privateKeyString="MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIhIs/3wz/nod7Ff/0UMzyK4gRCjPLqSfYkxxtlLn8GEn5Tg9kgKEl+CBiVad3w1afgFivaTHHI7xCC9zyulFkKQ3Q5IuouBkaY2+hKUPDzRRer3RmxUcNM4e5IUfDwG//8Hh69Q0kEHyD22lXGvo/kQnoUyhH+RjZ1UVAJAzj7lAgMBAAECgYAVh26vsggY0Yl/Asw/qztZn837w93HF3cvYiaokxLErl/LVBJz5OtsHQ09f2IaxBFedfmy5CB9R0W/aly851JxrI8WAkx2W2FNllzhha01fmlNlOSumoiRF++JcbsAjDcrcIiR8eSVNuB6ymBCrx/FqhdX3+t/VUbSAFXYT9tsgQJBALsHurnovZS1qjCTl6pkNS0V5qio88SzYP7lzgq0eYGlvfupdlLX8/MrSdi4DherMTcutUcaTzgQU20uAI0EMyECQQC6il1Kdkw8Peeb0JZMHbs+cMCsbGATiAt4pfo1b/i9/BO0QnRgDqYcjt3J9Ux22dPYbDpDtMjMRNrAKFb4BJdFAkBMrdWTZOVc88IL2mcC98SJcII5wdL3YSeyOZto7icmzUH/zLFzM5CTsLq8/HDiqVArNJ4jwZia/q6Fg6e8KO2hAkB0EK1VLF/ox7e5GkK533Hmuu8XGWN6I5bHnbYd06qYQyTbbtHMBrFSaY4UH91Qwd3u9gAWqoCZoGnfT/o03V5lAkBqq8jZd2lHifey+9cf1hsHD5WQbjJKPPIb57CK08hn7vUlX5ePJ02Q8AhdZKETaW+EsqJWpNgsu5wPqsy2UynO";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//獲得公鑰
PublicKey publicKey=getPublicKey(publicKeyString);
//獲得私鑰
PrivateKey privateKey=getPrivateKey(privateKeyString);
//公鑰加密
byte[] encryptedBytes=encrypt(data.getBytes(), publicKey);
System.out.println("加密后:"+new String(encryptedBytes));
//私鑰解密
byte[] decryptedBytes=decrypt(encryptedBytes, privateKey);
System.out.println("解密后:"+new String(decryptedBytes));
}
//將base64編碼后的公鑰字符串轉成PublicKey實例
public static PublicKey getPublicKey(String publicKey) throws Exception{
byte[ ] keyBytes=Base64.getDecoder().decode(publicKey.getBytes());
X509EncodedKeySpec keySpec=new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
}
//將base64編碼后的私鑰字符串轉成PrivateKey實例
public static PrivateKey getPrivateKey(String privateKey) throws Exception{
byte[ ] keyBytes=Base64.getDecoder().decode(privateKey.getBytes());
PKCS8EncodedKeySpec keySpec=new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}
//公鑰加密
public static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception{
Cipher cipher=Cipher.getInstance("RSA");//java默許"RSA"="RSA/ECB/PKCS1Padding"
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(content);
}
//私鑰解密
public static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception{
Cipher cipher=Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(content);
}
}
運行結果:

方法3:
除保存密鑰字符串以外,其他的做法1般是只保存 模n(modulus),公鑰和私鑰的e和d(exponent)。
其中,n, e, d可以這樣獲得到,獲得到后可以保存到本地文件中。
//獲得公鑰
RSAPublicKey publicKey=(RSAPublicKey) getPublicKey(publicKeyString);
BigInteger modulus1=publicKey.getModulus();
BigInteger exponent1=publicKey.getPublicExponent();
//獲得私鑰
RSAPrivateKey privateKey=(RSAPrivateKey) getPrivateKey(privateKeyString);
BigInteger modulus2=privateKey.getModulus();
BigInteger exponent2=privateKey..getPrivateExponent();
這里,假定我已從文件中讀取到了modulus和exponent:
public class RSACryptography {
public static String data="hello world";
public static String modulusString="95701876885335270857822974167577168764621211406341574477817778908798408856077334510496515211568839843884498881589280440763139683446418982307428928523091367233376499779842840789220784202847513854967218444344438545354682865713417516385450114501727182277555013890267914809715178404671863643421619292274848317157";
public static String publicExponentString="65537";
public static String privateExponentString="15118200884902819158506511612629910252530988627643229329521452996670429328272100404155979400725883072214721713247384231857130859555987849975263007110480563992945828011871526769689381461965107692102011772019212674436519765580328720044447875477151172925640047963361834004267745612848169871802590337012858580097";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//由n和e獲得公鑰
PublicKey publicKey=getPublicKey(modulusString, publicExponentString);
//由n和d獲得私鑰
PrivateKey privateKey=getPrivateKey(modulusString, privateExponentString);
//公鑰加密
byte[] encryptedBytes=encrypt(data.getBytes(), publicKey);
System.out.println("加密后:"+new String(encryptedBytes));
//私鑰解密
byte[] decryptedBytes=decrypt(encryptedBytes, privateKey);
System.out.println("解密后:"+new String(decryptedBytes));
}
//將base64編碼后的公鑰字符串轉成PublicKey實例
public static PublicKey getPublicKey(String modulusStr, String exponentStr) throws Exception{
BigInteger modulus=new BigInteger(modulusStr);
BigInteger exponent=new BigInteger(exponentStr);
RSAPublicKeySpec publicKeySpec=new RSAPublicKeySpec(modulus, exponent);
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(publicKeySpec);
}
//將base64編碼后的私鑰字符串轉成PrivateKey實例
public static PrivateKey getPrivateKey(String modulusStr, String exponentStr) throws Exception{
BigInteger modulus=new BigInteger(modulusStr);
BigInteger exponent=new BigInteger(exponentStr);
RSAPrivateKeySpec privateKeySpec=new RSAPrivateKeySpec(modulus, exponent);
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(privateKeySpec);
}
//公鑰加密
public static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception{
Cipher cipher=Cipher.getInstance("RSA");//java默許"RSA"="RSA/ECB/PKCS1Padding"
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(content);
}
//私鑰解密
public static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception{
Cipher cipher=Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(content);
}
}
運行結果:

這里3種方式總結起來也就是
1,.KeyPairGenerator獲得key;
2. String獲得key;
3. modulus和exponent獲得key。
但是,當加密的數據太長的時候需要
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈