1

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.");
    }
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
3vge
  • 197
  • 1
  • 2
  • 15
  • Check the format of the file you received. Common formats for public keys are PEM or DER. In any of these case use the proper code / library (bouncycastle) to read the data. In any other case: You unfortunately must parse the key data properly, simply assuming that it is written using Java serialization probably will not help. – Sebastian Oct 29 '15 at 10:19
  • @Sebastian i have PublickKey.sql format, can i change it to PublickKey.DER format ? i haven't private key, someone simple sent me his public key and need encrypt some text with this public key. – 3vge Oct 30 '15 at 06:53
  • 1
    normally .sql files contain SQL statements and thus are plain text files. However basically your question is the same as me asking you: "What is the content of the file bla.bla in my PC"? You can not answer that question and same way nobody can answer your question. Try at first to open file the in a text editor. What does it say (if you have something like ----- BEGIN ... you probably have a PEM file)? If the file is a binary file chances are that it is in ASN1. Try a free ASN1 online decoder to verify that. If both does not work ask the person which sends you the data about the format. – Sebastian Oct 30 '15 at 08:17
  • @Sebastian thank you for replay. i get those code : ** SEQUENCE(2 elem) SEQUENCE(2 elem) OBJECT IDENTIFIER1.2.840.113549.1.1.1 NULL BIT STRING(1 elem) SEQUENCE(2 elem) INTEGER(2048 bit) 219141285409297498915725504721243209206548563015863651025246791881712… INTEGER65537** – 3vge Oct 30 '15 at 12:47
  • i'm confused :( ok i'll asked him send me Public key ( which has like ------BEGIN that, a normal format). thank you <3 – 3vge Oct 30 '15 at 12:59
  • If you have obtained that as output from an ASN1 decoder, the rsa public key is in DER encoded form. Just read the key using the proper library. Check for example http://stackoverflow.com/questions/29565772/decode-asn1-sequence-to-rsa-public-key-using-java – Sebastian Nov 02 '15 at 12:54
  • i found it on this link : http://stackoverflow.com/questions/13990181/rsa-2048-encryption-decryption-exception – 3vge Nov 04 '15 at 11:34
  • @Sebastian i have on question.. when i encrypted text (e.x. "Hello Word") with the same public key, every time it's generating different encrypted text, someone who sent me his public key how he could open different encrypted text ? Is it normally in every encryption generate different text ? – 3vge Nov 04 '15 at 13:33
  • Actually I'm not totally sure about encryption but there are for example some algorithms which additionally use some kind of entropy which gives different signatures for the same input on each invocation. May be the same for encryption. You seem to be quite unfamiliar with cryptography. You should get someone you can ask or read a book to get basic knowledge. – Sebastian Nov 05 '15 at 13:26

1 Answers1

0

it happens when there is a difference in reading and writing data using streams.It seems key has been written other than ObjectOutputStream.

See This

java.io.StreamCorruptedException: invalid stream header: 54657374

Community
  • 1
  • 1
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
  • thank u for replay, but i don't know how he had generated this key, he simple send me public key and i want use this for encrypt some data. – 3vge Oct 29 '15 at 10:02
  • @3vge You't know how he had generated this key - how should we know? – Sebastian Oct 30 '15 at 08:19
  • @Sebastian he said : " We generated RSA public and private key, i'll send you RSA Public key, i have it Secret key, if you encrypt some text with this Public key, then i can decrypt your text." Is it possible to generate RSA key in different way ? – 3vge Oct 30 '15 at 13:12
  • 2
    It's not a matter iof generating the file it is just a matter of representation. Open a .jpg in a hex editor and you will only see bytes, no colors. Only interpreting the binary representation in a specific format (.jpg) yields you a picture. Same is with cryptographic stuff, there are several commonly used formats (PEM, DER, PKCS...). You have to read data in the same format it has been written or you'l only get some useless binary data. Ask him about the format or check the documentatiopn of the tool he used to create the file. – Sebastian Nov 02 '15 at 12:59