RSA
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
public class RSA {
private static String str = "中国";
public static void main(String[] args) {
jdkRSA();
}
public static void jdkRSA() {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
System.out.println("public key : " + Base64.encodeBase64String(rsaPublicKey.getEncoded()));
System.out.println("private key : " + Base64.encodeBase64String(rsaPrivateKey.getEncoded()));
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(str.getBytes());
System.out.println("加密后的内容 : " + Base64.encodeBase64String(result));
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
result = cipher.doFinal(result);
System.out.println(new String(result));
x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
result = cipher.doFinal(str.getBytes());
System.out.println("加密后的内容 : " + Base64.encodeBase64String(result));
pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
keyFactory = KeyFactory.getInstance("RSA");
privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
result = cipher.doFinal(result);
System.out.println(new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
EncryptRSAUtil
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.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
public class EncryptRSAUtil {
public static Map<String, String> generateKeyPair(){
Map<String, String> map = new HashMap<String, String>();
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
String publicKey = Base64.encodeBase64String(rsaPublicKey.getEncoded());
String privateKey = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
map.put("publicKey", publicKey );
map.put("privateKey", privateKey );
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return map;
}
public static String encrypt(String str,String rsaPublicKey){
String returnStr = "";
try {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( Base64.decodeBase64(rsaPublicKey) );
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(str.getBytes());
returnStr = Base64.encodeBase64String(result);
} catch (Exception e) {
e.printStackTrace();
}
return returnStr;
}
public static String decrypt(String str,String rsaPrivateKey){
String returnStr = "";
try {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec( Base64.decodeBase64(rsaPrivateKey) );
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] result = cipher.doFinal( Base64.decodeBase64(str) );
returnStr = new String(result);
} catch (Exception e) {
e.printStackTrace();
}
return returnStr;
}
public static void main(String[] args) {
Map<String, String> map = generateKeyPair();
String publicKey = map.get("publicKey");
String privateKey = map.get("privateKey");
System.out.println( "Public Key : " + publicKey );
System.out.println( "Private Key : " + privateKey );
String str = "hello world";
String encryptStr = encrypt(str, publicKey);
String dencryptStr = decrypt(encryptStr, privateKey);
System.out.println( "Encrypt : " + encryptStr );
System.out.println( "Dencrypt : " + dencryptStr );
String dencryptStr2 = decrypt("dDPyb7f6h0ZN/t1jbdk5BpjF6b7bCgAJXr0phSJ7facP08dXRfXiZDgnDjQ5mivOwV/NcPIo7eqr4ycunWQx4g==",
"MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAg8oxhsh8ZvH1ryojXI5gSk6ny0TKUVJibq7a2HuxtIH8cLPVZZD9zRCbon1uMDy6rEHlRS+7skkyoVWtU1Yx7wIDAQABAkBJ6+fLyji9JYrBLcmX6ORpyaSkh3lUE/nSS5HFS9xpepABiAUUoAv/U0w/a/ljiMUntxl9k5iPfDxm6xAnThEBAiEA0G/48jh9yxQQUL5A3Pssx1lFTifflev412cc5u7noT8CIQCh3MuOIEf0KWDt41f45GAPguz7ee2Miw7PpSNjQ0uTUQIgWvoUobywgzwdpvBzpUBomhZH3sYem/RkJ7iU7+Mk7McCIEgAcOgzQe7B7WVFCWAlLvoXcpXXQi2PujHyMtC4IclRAiEApFFDNUqmNrwIbBg0BhYFOOi4u3DoVCWPPUwptJ5eVQ0=");
System.out.println( "通过密钥解密 : " + dencryptStr2);
}
}