简体   繁体   English

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

[英]Java - Encrypt String with existing PublicKey.sql

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 . 我不想生成一个新的公共密钥 ,有人将其公共密钥(类型PublicKey.sql )发送给我,然后我将用该密钥加密一些文本并将其保存在Oracle DataBase

I found a method to read public key but I'm getting this Exception : 我找到了一种读取公共密钥的方法,但是却遇到了这个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.");
    }
}

it happens when there is a difference in reading and writing data using streams.It seems key has been written other than ObjectOutputStream. 当使用流读取和写入数据有所不同时,会发生这种情况。似乎已写入密钥而不是ObjectOutputStream。

See This 看这个

java.io.StreamCorruptedException: invalid stream header: 54657374 java.io.StreamCorruptedException:无效的流头:54657374

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM