package com.lazyrodi.rsa;
import java.security.InvalidKeyException;
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.SecureRandom;
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 RSA {
private final static int KEY_SIZE = 2048;
private static Cipher mCipher = null;
public static void init() {
if (mCipher == null) {
try {
mCipher = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
}
}
}
public static KeyPair makeKeyPair() {
KeyPairGenerator keyPairGenerator = null;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(KEY_SIZE, new SecureRandom());
return keyPairGenerator.genKeyPair();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static byte[] encryptRSA(String plainText, String strKey) {
if (plainText == null || strKey == null) {
System.out.println("Log : plainText or strKey is null.");
return null;
}
try {
PublicKey publicKey = getPublicKey(strKey);
if (mCipher == null || publicKey == null) {
System.out.println("Log : cipher or publicKey is null.");
return null;
}
mCipher.init(Cipher.ENCRYPT_MODE, publicKey);
return mCipher.doFinal(plainText.getBytes());
} catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException e) {
e.printStackTrace();
}
return null;
}
public static byte[] decryptRSA(String encryptedText, String strKey) {
if (encryptedText == null || strKey == null) {
System.out.println("Log : encryptedText or strKey is null.");
return null;
}
Cipher cipher;
try {
cipher = Cipher.getInstance("RSA");
if (cipher == null) {
System.out.println("Log : cipher is null.");
return null;
}
cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(strKey));
return cipher.doFinal(hexStrToByteArr(encryptedText));
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
return null;
}
private static PublicKey getPublicKey(String strKey) {
X509EncodedKeySpec spec = new X509EncodedKeySpec(hexStrToByteArr(strKey));
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(spec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
}
return null;
}
private static PrivateKey getPrivateKey(String strKey) {
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(hexStrToByteArr(strKey));
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(spec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
}
return null;
}
public static byte[] hexStrToByteArr(String hex) {
if (hex == null || hex.length() == 0) {
return null;
}
byte[] byteArr = new byte[hex.length() / 2];
for (int i = 0; i < byteArr.length; i++) {
byteArr[i] = (byte)Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
}
return byteArr;
}
public static String byteArrToHexStr(byte[] byteArr) {
if (byteArr == null || byteArr.length == 0) {
return null;
}
StringBuilder hexString = new StringBuilder(byteArr.length + 2);
for (int i = 0; i < byteArr.length; i++) {
String hexNum = "0" + Integer.toHexString(0xff & byteArr[i]);
hexString.append(hexNum.substring(hexNum.length() - 2));
}
return hexString.toString();
}
}