[英]Java - Encrypt String with existing PublicKey.sql
我不想生成一个新的公共密钥 ,有人将其公共密钥(类型PublicKey.sql
)发送给我,然后我将用该密钥加密一些文本并将其保存在Oracle DataBase
。
我找到了一种读取公共密钥的方法,但是却遇到了这个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)
密钥读取方法:
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.");
}
}
当使用流读取和写入数据有所不同时,会发生这种情况。似乎已写入密钥而不是ObjectOutputStream。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.