How to do RSA encryption of byte array with base-64 encoded public key?
After reading the couple of articles( of google search ) on how to do RSA encryption in Java, fou
this is how you can generate Public and Private key pair below is the function to store them on hard dist
enter code here
public static void GenerateKeyPair()
{
try{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(),
RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(),
RSAPrivateKeySpec.class);
saveToFile("public.key", pub.getModulus(),
pub.getPublicExponent());
saveToFile("private.key", priv.getModulus(),
priv.getPrivateExponent());
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void saveToFile(String fileName,
BigInteger mod, BigInteger exp) throws Exception {
ObjectOutputStream oout = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(fileName)));
try {
oout.writeObject(mod);
oout.writeObject(exp);
} catch (Exception e) {
throw new Exception("error", e);
} finally {
oout.close();
}
}