package org.yood.rsa.util; import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException; public class BestRSA { public static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0kgVXWgRcOLyFbfHNKK55syz/" +
"GhhMzGLPFj5AzczanP7NSCQEPPYhehMYhGUZI+Du8WS1yfOQ6SUYIYKZRuLUSSm4" +
"ScsjhY+1LED9S+LDzDWCWnQah4Q4z0owNRySJqCSO4uA+ARg954n6kbXIg4hl2LM" +
"UYelcocj1lBkymjGaQIDAQAB"; public static String privateKey = "MIICXQIBAAKBgQC0kgVXWgRcOLyFbfHNKK55syz/GhhMzGLPFj5AzczanP7NSCQE" +
"PPYhehMYhGUZI+Du8WS1yfOQ6SUYIYKZRuLUSSm4ScsjhY+1LED9S+LDzDWCWnQa" +
"h4Q4z0owNRySJqCSO4uA+ARg954n6kbXIg4hl2LMUYelcocj1lBkymjGaQIDAQAB" +
"AoGBAJftSCsv5P1HC29kiw8oDGz/EXvuE8YCLZy5xVU3EwZZE5Xa/drHA/w0vv5g" +
"3sMhSe8hEbBWo+UoFPrRrxWR6+RwU0Mfrn9kAbSmmK5M8aoOG+fx/++mhmwH0fuN" +
"BWcMZbcAti0n3DBFzhXjcmVqaSM9Z6eFREnX+d639cfwXEuRAkEAxlAY9wedTPdC" +
"sTwtCpuPn5bNA+vBoTS+GcrEcP5H5ComWXxbPyvY8nCE1PCln/+FIddTo5TnDBAl" +
"rondH3gS1QJBAOkYrmmChIYwAzRKjkaGRPyCJ+VLYIZ+yQis3udUDmOWxP3xKnBV" +
"3j+BvD1GqhVBEHxPRBVWDKQ4+4AlWg2YZ0UCQQCoUQNfZ48S7Com07RG9JGKGiwX" +
"z9mgQWu36Gmina/W8A9slCe+DjEsVGPpd/abPfx5JJFQnekcS/gvk8OiGf+hAkBU" +
"XYKC3aJpjucZ/gIQtHgzjEg0TVvaUfkhErB6uYmbse4Km0bo8atHAQXIh9L3bl0x" +
"IN9beZGJvrYIY3x8fzShAkADMutFaI6q6buVGkqGR6pNe6V1XDBdKPGvC5xhTgiS" +
"3lTO5GjdHqW4+9SyR97USTyxRZXEWMSD/73ZQE592Mfy"; private static String algorithm = "RSA";
private static final int MAX_ENCRYPT_BLOCK = 117;
private static final int MAX_DECRYPT_BLOCK = 128;
private static String data = "1234561234561234561234561234561234561234561234561234561234561234561234561\n" +
"2345612345612345612345612345612345612345612345612345612345612345612345612\n" +
"4561234561234561234561234561234561234561234561234561234561234561234561234\n" +
"5612345612345612345612345612345612345612345612345612345612345612345612345\n" +
"6123456123456123456123456123456123456123456123456123456123456123456123456\n" +
"1234561234561234561234561234561234561234561234561234561234561234561234561\n" +
"2345612345612345612345612345612345612345612345612345612345612345612345612\n" +
"3456123456123456123456123456123456123456123456123456123456123456123456123\n" +
"4561234561234561234561234561234561234561234561234561234561234561234561234\n" +
"5612345612345612345612345612345612345612345612345612345612345612345612345\n" +
"6123456123456123456123456123456123456123456123456\n"; public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, IOException {
String test = testEncrypt(privateKey, data);
System.out.println(test);
String testDecrypt = testDecrypt(publicKey, test);
System.out.println(testDecrypt); } /**
* 加密
*/ public static String testEncrypt(String key, String data) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, IOException {
byte[] decode = Base64.getDecoder().decode(key);
RSAPrivateKeyStructure asn1PrivKey = new RSAPrivateKeyStructure((ASN1Sequence) ASN1Sequence.fromByteArray(decode));
RSAPrivateKeySpec rsaPrivKeySpec = new RSAPrivateKeySpec(asn1PrivKey.getModulus(), asn1PrivKey.getPrivateExponent());
KeyFactory kf = KeyFactory.getInstance(algorithm);
PrivateKey generatePrivate = kf.generatePrivate(rsaPrivKeySpec);
Cipher ci = Cipher.getInstance(algorithm);
ci.init(Cipher.ENCRYPT_MODE, generatePrivate); byte[] bytes = data.getBytes();
int inputLen = bytes.length;
int offLen = 0;//偏移量
int i = 0;
ByteArrayOutputStream bops = new ByteArrayOutputStream();
while (inputLen - offLen > 0) {
byte[] cache;
if (inputLen - offLen > 117) {
cache = ci.doFinal(bytes, offLen, 117);
} else {
cache = ci.doFinal(bytes, offLen, inputLen - offLen);
}
bops.write(cache);
i++;
offLen = 117 * i;
}
bops.close();
byte[] encryptedData = bops.toByteArray();
String encodeToString = Base64.getEncoder().encodeToString(encryptedData);
return encodeToString;
} /**
* 解密
*/
public static String testDecrypt(String key, String data) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException, IOException {
byte[] decode = Base64.getDecoder().decode(key);
// PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(decode); //java底层 RSA公钥只支持X509EncodedKeySpec这种格式
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(decode);
KeyFactory kf = KeyFactory.getInstance(algorithm);
PublicKey generatePublic = kf.generatePublic(x509EncodedKeySpec);
Cipher ci = Cipher.getInstance(algorithm);
ci.init(Cipher.DECRYPT_MODE, generatePublic); byte[] bytes = Base64.getDecoder().decode(data);
int inputLen = bytes.length;
int offLen = 0;
int i = 0;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while (inputLen - offLen > 0) {
byte[] cache;
if (inputLen - offLen > 128) {
cache = ci.doFinal(bytes, offLen, 128);
} else {
cache = ci.doFinal(bytes, offLen, inputLen - offLen);
}
byteArrayOutputStream.write(cache);
i++;
offLen = 128 * i;
}
byteArrayOutputStream.close();
byte[] byteArray = byteArrayOutputStream.toByteArray();
return new String(byteArray);
}
}