RSA 基于因子分解
package com.encode;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* Created by chris on 2019/2/23.
*/
public class RSA {
private static int keySize = 512;
private static RSAPublicKey rsaPublicKey;
private static RSAPrivateKey rsaPrivateKey;
static {
// 初始化
try {
KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
rsa.initialize(keySize);
KeyPair keyPair = rsa.generateKeyPair();
rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 私钥加密
* @param src
* @return
* @throws Exception
*/
public static String privateKeyEncode(String src) throws Exception {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory rsa = KeyFactory.getInstance("RSA");
PrivateKey privateKey = rsa.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(src.getBytes());
return Base64.encodeBase64String(result);
}
/**
* 私钥解密
* @param src
* @return
* @throws Exception
*/
public static String privateKeyDecode(String src) throws Exception {
byte[] bytes = Base64.decodeBase64(src);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory rsa = KeyFactory.getInstance("RSA");
PrivateKey privateKey = rsa.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(bytes);
return new String(result);
}
/**
* 公钥加密
* @param src
* @return
* @throws Exception
*/
public static String publicKeyEncode(String src) throws Exception {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
KeyFactory rsa = KeyFactory.getInstance("RSA");
PublicKey publicKey = rsa.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(src.getBytes());
return Base64.encodeBase64String(result);
}
/**
* 公钥解密
* @param src
* @return
* @throws Exception
*/
public static String publicKeyDecode(String src) throws Exception {
byte[] bytes = Base64.decodeBase64(src);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
KeyFactory rsa = KeyFactory.getInstance("RSA");
PublicKey publicKey = rsa.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(bytes);
return new String(result);
}
/**
* 获取公钥
* @return
*/
public static String getPublicKey() {
return Base64.encodeBase64String(rsaPublicKey.getEncoded());
}
/**
* 获取私钥
* @return
*/
public static String getPrivateKey() {
return Base64.encodeBase64String(rsaPrivateKey.getEncoded());
}
public static void main(String[] args) throws Exception {
System.out.println("public key:" + getPublicKey());
System.out.println("private key:" + getPrivateKey());
String src = "hello world rsa ";
String s = privateKeyEncode(src);
System.out.println(s);
System.out.println(s.length());
String s1 = publicKeyDecode(s);
System.out.println(s1);
}
}