Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (307)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Error reading file  (Read 692 times)
0 Members and 1 Guest are viewing this topic.
Offline xslaught

Junior Newbie





« Posted 2012-01-24 23:09:37 »

I have a file called privatekey.der in the same directory as a static class called KeyReader.

I'm having issues with reading the file. It's giving me invalid stream header. I was wondering how you can go about reading the file.

This is my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
package server.encryption;

import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class KeyReader {

   public static PrivateKey getPrivateKey(InputStream in) throws Exception {
//      System.err.println(filename);
//      File f = new File(filename);
     //DataInputStream dis = new DataInputStream(is);
     ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
      BigInteger m = (BigInteger) oin.readObject();
      BigInteger e = (BigInteger) oin.readObject();
      RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
       KeyFactory fact = KeyFactory.getInstance("RSA");
       PrivateKey privateKey = fact.generatePrivate(keySpec);
       oin.close();
       return privateKey;
//      PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
//      KeyFactory kf = KeyFactory.getInstance("RSA");
//      return kf.generatePrivate(spec);
  }
   
   public static byte[] rsaDecrypt(byte[] data) throws Exception {
      System.err.println(KeyReader.class.getResourceAsStream("privatekey.der").toString());
      PrivateKey privateKey = getPrivateKey(KeyReader.class.getResourceAsStream("privatekey.der"));
      Cipher cipher = Cipher.getInstance("RSA");
      cipher.init(Cipher.DECRYPT_MODE, privateKey);
      byte[] cipherData = cipher.doFinal(data);
      return cipherData;
   }
   
   public static byte[] aesDecrypt(byte[]data, byte[]key) throws Exception {
      SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, cipher.getParameters());
      byte[] cipherData = cipher.doFinal(data);
      return cipherData;
   }
   
}
Offline Shazer2

Junior Member


Medals: 3


Aspiring developer.


« Reply #1 - Posted 2012-01-24 23:28:37 »

Nevermind me, didn't see that you mentioned the error

~Shazer2 Smiley

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Offline ra4king

JGO Kernel


Medals: 292
Projects: 2


I'm the King!


« Reply #2 - Posted 2012-01-24 23:30:59 »

If it didn't find the file, getResourceAsStream(...) would return null, which would cause the constructor for ObjectInputStream to throw an IOException.

This error could only happen if you didn't previously write to the file using a ObjectOutputStream.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Stranger

Junior Member


Medals: 3



« Reply #3 - Posted 2012-01-25 09:31:21 »

Maybe error caused by opening stream twice:

1  
2  
3  
4  
5  
public static byte[] rsaDecrypt(byte[] data) throws Exception {
      System.err.println(KeyReader.class.getResourceAsStream("privatekey.der").toString());
      PrivateKey privateKey = getPrivateKey(KeyReader.class.getResourceAsStream("privatekey.der"));
      ...
   }

Anton
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 463
Projects: 4


Hand over your head.


« Reply #4 - Posted 2012-01-25 14:02:17 »

Oh come on! Can't you guys read?
Quote
It's giving me invalid stream header.
This means that it has opened the stream without any problem. It's just that the data in the 'header' (first N bytes) is not what the ObjectInputStream was expecting.

I mean, reading a DER file and expecting it to return BigIntegers upon calling ObjectInputStream.readObject() is silly.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline ra4king

JGO Kernel


Medals: 292
Projects: 2


I'm the King!


« Reply #5 - Posted 2012-01-25 14:12:31 »

Which is why I pointed out he didn't write to the file previously with an ObjectOutputStream since the first 4 bytes of the stream have to be 0xAECD (I do believe that's correct).

I don't know what a DER file is Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
mrbenebob (21 views)
2013-06-19 14:55:23

BrassApparatus (31 views)
2013-06-19 08:52:37

NegativeZero (35 views)
2013-06-19 03:31:52

NegativeZero (36 views)
2013-06-19 03:24:09

Jesse_Attard (41 views)
2013-06-18 22:03:02

HeroesGraveDev (77 views)
2013-06-15 23:35:23

Vermeer (78 views)
2013-06-14 20:08:06

davedes (79 views)
2013-06-14 16:03:55

alaslipknot (71 views)
2013-06-13 07:56:31

Roquen (93 views)
2013-06-12 04:12:32
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!