- 生成密钥对
- 公钥加密私钥解密
- 私钥加密公钥解密
- 测试用例
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.security.*;
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 RSAEncrypt {
private static final String ALGORITHM = "RSA";
private static final int MAX_ENCRYPT_BLOCK = 117;
private static final int MAX_DECRYPT_BLOCK = 128;
private static Map<Integer, String> keyMap = new HashMap<Integer, String>();
public static final Integer PUBLICKEY = 0;
public static final Integer PRIVATEKEY = 1;
public RSAEncrypt() {
}
public static String encryptPublic(String str, String publicKey) throws Exception {
byte[] decoded = Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
Cipher cipher = getCipher(1, pubKey);
return splitEncrypt(str, cipher, pubKey.getModulus());
}
public static String encryptPrivate(String str, String privateKey) throws Exception {
byte[] decoded = Base64.decodeBase64(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
Cipher cipher = getCipher(1, priKey);
return splitEncrypt(str, cipher, priKey.getModulus());
}
public static String decryptPublic(String str, String publicKey) throws Exception {
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
byte[] decoded = Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
Cipher cipher = getCipher(2, pubKey);
return splitDecrypt(str, cipher, pubKey.getModulus());
}
public static String decryptPrivate(String str, String privateKey) throws Exception {
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
byte[] decoded = Base64.decodeBase64(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
Cipher cipher = getCipher(2, priKey);
return splitDecrypt(str, cipher, priKey.getModulus());
}
private static String splitDecrypt(String str, Cipher cipher, BigInteger modulus) throws Exception {
byte[] bytes = Base64.decodeBase64(str);
int inputLen = bytes.length;
int offLen = 0;
int i = 0;
ByteArrayOutputStream byteArrayOutputStream;
byte[] cache;
for(byteArrayOutputStream = new ByteArrayOutputStream(); inputLen - offLen > 0; offLen = 128 * i) {
if (inputLen - offLen > 128) {
cache = cipher.doFinal(bytes, offLen, 128);
} else {
cache = cipher.doFinal(bytes, offLen, inputLen - offLen);
}
byteArrayOutputStream.write(cache);
++i;
}
byteArrayOutputStream.close();
cache = byteArrayOutputStream.toByteArray();
return new String(cache);
}
private static Cipher getCipher(int model, Key key) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(model, key);
return cipher;
}
private static String splitEncrypt(String str, Cipher cipher, BigInteger modulus) throws Exception {
byte[] bytes = str.getBytes();
int inputLen = bytes.length;
int offLen = 0;
int i = 0;
ByteArrayOutputStream bops;
byte[] cache;
for(bops = new ByteArrayOutputStream(); inputLen - offLen > 0; offLen = 117 * i) {
if (inputLen - offLen > 117) {
cache = cipher.doFinal(bytes, offLen, 117);
} else {
cache = cipher.doFinal(bytes, offLen, inputLen - offLen);
}
bops.write(cache);
++i;
}
bops.close();
cache = bops.toByteArray();
String encodeToString = Base64.encodeBase64String(cache);
return encodeToString;
}
public static void genKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024,new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
keyMap.put(PUBLICKEY ,publicKeyString);
keyMap.put(PRIVATEKEY ,privateKeyString);
}
public static void main(String[] args) throws Exception {
String str = "{\"test\":\"001\"}";
System.out.println("参数:" + str);
genKeyPair();
System.out.println("公钥:" + keyMap.get(PUBLICKEY));
System.out.println("私钥:" + keyMap.get(PRIVATEKEY));
String encrypt = RSAEncrypt.encryptPublic(str, keyMap.get(PUBLICKEY));
System.out.println("公钥加密:" + encrypt);
String decrypt = RSAEncrypt.decryptPrivate(encrypt, keyMap.get(PRIVATEKEY));
System.out.println("私钥解密:" + decrypt);
String encryptPrivate = RSAEncrypt.encryptPrivate(str, keyMap.get(PRIVATEKEY));
System.out.println("私钥加密:" + encryptPrivate);
String decryptPublic = RSAEncrypt.decryptPublic(encryptPrivate, keyMap.get(PUBLICKEY));
System.out.println("公钥解密:" + decryptPublic);
}
}
~~