I don't want to generate a new public key, someone send me his public key (type PublicKey.sql
) then I'll encrypt some text with this key and save it in a Oracle DataBase
.
I found a method to read public key but I'm getting this Exception
:
java.io.StreamCorruptedException: invalid stream header: 33303832
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at ge.georgiancard.app.main.App.readKeyFromFile(App.java:36)
at ge.georgiancard.app.main.App.rsaEncrypt(App.java:67)
Method to read key :
public static Key readKeyFromFile() throws IOException {
InputStream in = new FileInputStream(PUBLIC_KEY_FILE);
ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
try {
BigInteger m = (BigInteger) oin.readObject();
BigInteger e = (BigInteger) oin.readObject();
KeyFactory fact = KeyFactory.getInstance("RSA");
return fact.generatePublic(new RSAPublicKeySpec(m, e));
} catch (Exception e) {
throw new RuntimeException("Spurious serialisation error", e);
} finally {
oin.close();
System.out.println("Closed reading file.");
}
}