RSA是一種使用非對稱加密的算法,以前總認爲只有公鑰加密,私鑰解密的,現在看到了一篇關於私鑰加密,公鑰解密的文章(懷疑人生~),現把代碼貼在下面:(正確的方式是公鑰加密,私鑰解密,私鑰加簽,公鑰驗籤(:-加密是爲了防止信息被泄露,而簽名是爲了防止信息被篡改-: ))
package coms.nas;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class RSAUtil {
public static String data = "12345";
public static BASE64Encoder base64Encoder = new BASE64Encoder();
public static BASE64Decoder base64Decoder = new BASE64Decoder();
// 公鑰
private static String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/OpVr+aQu6B3stSUgsLcZWpaxatset8zTqat1FF543hoECcTnRqDXKwfX09J+RLCc/1fbITt0s4wUUwJNU7lKJSTGZp5/xHcEiFJjTa+XY6pQHQKvvZjAQMkyzC3H5tmaNTapKYJOAWw7u1dxcRNFdD3k5E+EiqSnlo30u7SLCwIDAQAB";
// 私鑰
private static String privateKeyStr = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL86lWv5pC7oHey1JSCwtxlalrFq2x63zNOpq3UUXnjeGgQJxOdGoNcrB9fT0n5EsJz/V9shO3SzjBRTAk1TuUolJMZmnn/EdwSIUmNNr5djqlAdAq+9mMBAyTLMLcfm2Zo1Nqkpgk4BbDu7V3FxE0V0PeTkT4SKpKeWjfS7tIsLAgMBAAECgYBicjt4geV3TIITWVJK2Q76G3vWzIcP8lmdYgzl0l2sZdMI3yqiUeb9vqZkAyWrYZt2x7GoGxyrwL9Nu0pFGuQZFaZIrHRj6LoNq/dgGUpN5zviXUDq2RrhhP7dW4Zc2UbbZqtTzn4jgv8/dviT+LACBmbavojjbb6YZHO/YDml2QJBAPWWu7SkyqfHSDOBBYWyI0GON2ApqTOIsENpQ572IvjNzT8TcXsNRr1hy4o5JfJN4KutBSsJkxAv3+nCc7pvRo0CQQDHVefkgjyuCyQjTtm8WPeIP7Ny8Rul44SmoyaSOANiPufsjIAPvxtNwyvkyUKtI7AMx6XrAWltRMWWiByVH533AkBp87fTfWz46V7a6YTqYyoWtDZrxE19MDFrQ9SqleIMmS09UzQYNGgaeECJx5H5cWPGbQTXxm+uAhmGDiBDhJJZAkEAu84SR1b1OL1CdQmrVyszPGlX9ul3NRphNmbsxkKD3aKK/HF7jlptrRw/VLTSXzIKgl/v0LRp0gtDZgojc9RwDQJBAJ2d0E9huqG9yP0bA9q0lIFwqJogLnoRvQCkNW6hATUrA5b7lrZYniPbwRfSALW2jgweTeTaeouPBHPWbVz/ws8=";
private static RSAUtil ourInstance = new RSAUtil();
public static RSAUtil getInstance() {
return ourInstance;
}
// 生成密鑰對
private void generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator;
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 獲取公鑰,並以base64格式打印出來
PublicKey publicKey = keyPair.getPublic();
publicKeyStr = new String(base64Encoder.encode(publicKey.getEncoded()));
// 獲取私鑰,並以base64格式打印出來
PrivateKey privateKey = keyPair.getPrivate();
privateKeyStr = new String(base64Encoder.encode(privateKey.getEncoded()));
}
// 將base64編碼後的公鑰字符串轉成PublicKey實例
private static PublicKey getPublicKey(String publicKey) throws Exception {
byte[] keyBytes = base64Decoder.decodeBuffer(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
}
// 將base64編碼後的私鑰字符串轉成PrivateKey實例
private static PrivateKey getPrivateKey(String privateKey) throws Exception {
byte[] keyBytes = base64Decoder.decodeBuffer(privateKey);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}
// 公鑰加密
public static String encryptByPublicKey(String content) throws Exception {
// 獲取公鑰
PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = cipher.doFinal(content.getBytes());
String cipherStr = base64Encoder.encode(cipherText);
return cipherStr;
}
// 私鑰加密
public static String encryptByPrivateKey(String content) throws Exception {
// 獲取私鑰
PrivateKey privateKey = getPrivateKey(privateKeyStr);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] cipherText = cipher.doFinal(content.getBytes());
String cipherStr = base64Encoder.encode(cipherText);
return cipherStr;
}
// 私鑰解密
public static String decryptByPrivateKey(String content) throws Exception {
// 獲取私鑰
PrivateKey privateKey = getPrivateKey(privateKeyStr);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cipherText = base64Decoder.decodeBuffer(content);
byte[] decryptText = cipher.doFinal(cipherText);
return new String(decryptText);
}
// 公鑰解密
public static String decryptByPublicKey(String content) throws Exception {
// 獲取公鑰
PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] cipherText = base64Decoder.decodeBuffer(content);
byte[] decryptText = cipher.doFinal(cipherText);
return new String(decryptText);
}
public static void main(String[] args) throws Exception {
System.out.println("初始數據:"+data);
// 公鑰加密
String encryptedBytes = encryptByPublicKey(data);
System.out.println("公鑰加密後:" + encryptedBytes);
// 私鑰解密
String decryptedBytes = decryptByPrivateKey(encryptedBytes);
System.out.println("私鑰解密後:" + decryptedBytes);
// 私鑰加密
String encryptedBytes2 = encryptByPrivateKey(data);
System.out.println("私鑰加密後:" + encryptedBytes2);
// 公鑰解密
String decryptedBytes2 = decryptByPublicKey(encryptedBytes2);
System.out.println("公鑰解密後:" + decryptedBytes2);
}
}