我真的很想在Android java源代码中嵌入公钥。
由于它们是Object的,我不明白如何在代码中包含公钥。
我试图创建一些最终的字符串,但没有工作
任何帮助,将不胜感激。
也许这是一个糟糕的IDE,因为我没有看到多讲了
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();
}
}
2011-09-14
Erik