package com.xxx;
import android.util.Base64;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class RSAUtil {
private static final String PUB_K = "";
private static final Charset CHARSET = StandardCharsets.UTF_8;
private static final String RSA = "RSA";
private static final String RSA_ECB = "RSA/ECB/PKCS1Padding";
public static void getKeyPair() {
try {
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 = Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);
String privateKeyString = Base64.encodeToString(privateKey.getEncoded(), Base64.DEFAULT);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static String encryptParams(String str) {
return rsaEncode(str, PUB_K);
}
public static String encryptSimple(String str, String publicKey) {
try {
byte[] decoded = Base64.decode(publicKey, Base64.DEFAULT);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(RSA).generatePublic(new X509EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance(RSA_ECB);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = Base64.encodeToString(cipher.doFinal(str.getBytes(CHARSET)), Base64.DEFAULT);
return outStr;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return "";
}
public static String decryptSimple(String str, String privateKey) {
try {
byte[] inputByte = Base64.decode(str.getBytes(CHARSET), Base64.DEFAULT);
byte[] decoded = Base64.decode(privateKey, Base64.DEFAULT);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance(RSA).generatePrivate(new PKCS8EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance(RSA_ECB);
cipher.init(Cipher.DECRYPT_MODE, priKey);
String outStr = new String(cipher.doFinal(inputByte));
return outStr;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return "";
}
public static String rsaEncode(String data, String publicKey) {
byte[] b = data.getBytes();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
byte[] decoded = Base64.decode(publicKey, Base64.DEFAULT);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(RSA).generatePublic(new X509EncodedKeySpec(decoded));
int keySize = pubKey.getModulus().bitLength();
int maxBlock = keySize / 8 - 11;
int inputLen = b.length;
int offSet = 0;
byte[] cache;
int i = 0;
Cipher cipher = Cipher.getInstance(RSA_ECB);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
while (inputLen - offSet > 0) {
if (inputLen - offSet > maxBlock) {
cache = cipher.doFinal(b, offSet, maxBlock);
} else {
cache = cipher.doFinal(b, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * maxBlock;
}
byte[] decryptedData = out.toByteArray();
return Base64.encodeToString(decryptedData, Base64.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return "";
}
public static String rsaDecode(String data, String privateKey) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
byte[] decoded = Base64.decode(privateKey, Base64.DEFAULT);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance(RSA).generatePrivate(new PKCS8EncodedKeySpec(decoded));
int keySize = priKey.getModulus().bitLength();
int maxBlock = keySize / 8;
byte[] b = Base64.decode(data.getBytes(CHARSET), Base64.URL_SAFE);
int inputLen = b.length;
int offSet = 0;
byte[] cache;
int i = 0;
Cipher cipher = Cipher.getInstance(RSA_ECB);
cipher.init(Cipher.DECRYPT_MODE, priKey);
while (inputLen - offSet > 0) {
if (inputLen - offSet > maxBlock) {
cache = cipher.doFinal(b, offSet, maxBlock);
} else {
cache = cipher.doFinal(b, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * maxBlock;
}
byte[] decryptedData = out.toByteArray();
return new String(decryptedData);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}