In the code below im creating public
and Private Keys for encryption.
I really would like to embed the Public Key in the Android java source code.
Since they are Object’s I don’t understand how to include the Public Key in the code.
I have tried to create some final String but that did not work
Any help would be appreciated.
Maybe this is a bad ide since i dont see much talk about it
public 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 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();
}
}
answering my own question.