gpt4 book ai didi

Java - 使用现有的 PublicKey.sql 加密字符串

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:36 26 4
gpt4 key购买 nike

我不想生成新的公钥,有人向我发送他的公钥(类型 PublicKey.sql),然后我将使用此 key 加密一些文本并将其保存在 Oracle DataBase 中。

我找到了一种读取公钥的方法,但我收到了此异常:

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)

读取 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.");
}
}

最佳答案

当使用流读取和写入数据存在差异时,就会发生这种情况。似乎 key 已写入除 ObjectOutputStream 之外的其他内容。

看这个

java.io.StreamCorruptedException: invalid stream header: 54657374

关于Java - 使用现有的 PublicKey.sql 加密字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33410550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com