公鑰與私鑰
非對(duì)稱加密與對(duì)稱加密主要的區(qū)別在于非對(duì)稱加密在加密和解密時(shí)采用不同的密鑰,一個(gè)公開,稱為公鑰,可以通過(guò)非安全通道發(fā)放,另一個(gè)保密,稱為私鑰,由發(fā)放者自己保存。公鑰與私鑰成對(duì)出現(xiàn),用公鑰加密的數(shù)據(jù),只能由私鑰解密,用私鑰加密的數(shù)據(jù),只能由公鑰解密。
非對(duì)稱加密解決了對(duì)稱加密的密鑰分配與傳遞問題,并提高了算法的安全性,但是加解密速度比對(duì)稱加密慢很多,所以通常用非對(duì)稱加密傳遞對(duì)稱加密的密鑰,而使用對(duì)稱加密傳遞數(shù)據(jù)
RSA算法
RSA算法是一種典型的非對(duì)稱加密算法,也是目前最有影響力的非對(duì)稱加密算法,該算法于1977年由羅納德·李維斯特(Ron Rivest)、阿迪·薩莫爾(Adi Shamir)和倫納德·阿德曼(Leonard Adleman)一起提出,RSA就是他們?nèi)齻€(gè)人的名字簡(jiǎn)寫。RSA算法的密鑰為512到65535位且必須為64位的倍數(shù)。
兩種使用場(chǎng)景
加密: 不希望別人看到我的消息,只有我才能解密,所以可得出公鑰負(fù)責(zé)加密,私鑰負(fù)責(zé)解密;
簽名:不希望有人冒充我發(fā)消息,只有我才能發(fā)布這個(gè)簽名,所以可得出私鑰負(fù)責(zé)簽名,公鑰負(fù)責(zé)驗(yàn)證。
注意
- 采用base64編碼公鑰私鑰字節(jié)數(shù)組,以無(wú)亂碼的方式存儲(chǔ)公鑰私鑰字符串
- 采用base64編碼密文字節(jié)數(shù)組,得到無(wú)亂碼字符串方便網(wǎng)絡(luò)傳輸
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* Created by tingkl on 2017/9/5.
*/
public class RSA {
private static String src = "tingkl security rsa";
private static String publicKeyBase64;
private static String privateKeyBase64;
static {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
publicKeyBase64 = Base64.encodeBase64String(rsaPublicKey.getEncoded());
privateKeyBase64 = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
System.out.println("Public Key:" + Base64.encodeBase64String(rsaPublicKey.getEncoded()));
System.out.println("Private Key:" + Base64.encodeBase64String(rsaPrivateKey.getEncoded()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
@Test
public void jdkRSA() {
try {
//1.私鑰加密,公鑰解密----加密
PrivateKey privateKey = getPrivateKey(Base64.decodeBase64(privateKeyBase64));
byte[] encryptBytesToSend = encrypt(src, privateKey);
String encryptBytesBase64 = Base64.encodeBase64String(encryptBytesToSend);
System.out.println("私鑰加密---send:" + encryptBytesBase64);
//2.私鑰加密,公鑰解密----解密
byte[] encryptBytesReceived = Base64.decodeBase64(encryptBytesBase64);
PublicKey publicKey = getPublicKey(Base64.decodeBase64(publicKeyBase64));
String result = decrypt(encryptBytesReceived, publicKey);
System.out.println("公鑰解密---receive:" + result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
}
private static String decrypt(byte[] encryptBytesReceived, Key key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(encryptBytesReceived);
return new String(result);
}
private static byte[] encrypt(String src, Key key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(src.getBytes());
return result;
}
private static PrivateKey getPrivateKey(byte[] bytes) throws NoSuchAlgorithmException, InvalidKeySpecException {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(bytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
return privateKey;
}
private static PublicKey getPublicKey(byte[] bytes) throws NoSuchAlgorithmException, InvalidKeySpecException {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(bytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
return publicKey;
}
@Test
public void jdkRSA2() {
try {
//1.公鑰加密,私鑰解密----加密
PublicKey publicKey = getPublicKey(Base64.decodeBase64(publicKeyBase64));
byte[] encryptBytesToSend = encrypt(src, publicKey);
String encryptBytesBase64 = Base64.encodeBase64String(encryptBytesToSend);
System.out.println("公鑰加密---send:" + encryptBytesBase64);
//2.公鑰加密,私鑰解密----解密
byte[] encryptBytesReceived = Base64.decodeBase64(encryptBytesBase64);
PrivateKey privateKey = getPrivateKey(Base64.decodeBase64(privateKeyBase64));
String result = decrypt(encryptBytesReceived, privateKey);
System.out.println("私鑰解密---receive:" + result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
}
}