Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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 1030 times)
0 Members and 1 Guest are viewing this topic.
Offline xslaught

Junior Newbie





« Posted 2012-01-24 22: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 Devvie


Medals: 3


Aspiring developer.


« Reply #1 - Posted 2012-01-24 22: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: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-01-24 22: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

Senior Devvie


Medals: 7



« Reply #3 - Posted 2012-01-25 08: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: 829
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2012-01-25 13: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
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-01-25 13: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.

 

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

The first screenshot will be displayed as a thumbnail.

xFryIx (52 views)
2014-11-13 11:34:49

digdugdiggy (34 views)
2014-11-12 20:11:50

digdugdiggy (28 views)
2014-11-12 20:10:15

digdugdiggy (23 views)
2014-11-12 20:09:33

kovacsa (46 views)
2014-11-07 18:57:14

TehJavaDev (50 views)
2014-11-03 21:04:50

BurntPizza (49 views)
2014-11-03 17:54:52

moogie (65 views)
2014-11-03 05:22:04

CopyableCougar4 (63 views)
2014-11-01 22:36:41

DarkCart (148 views)
2014-11-01 13:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 21:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 21:36:02

List of Learning Resources
by Longor1996
2014-08-16 09:40:00

List of Learning Resources
by SilverTiger
2014-08-05 18:33:27

Resources for WIP games
by CogWheelz
2014-08-01 15:20:17

Resources for WIP games
by CogWheelz
2014-08-01 15:19:50

List of Learning Resources
by SilverTiger
2014-07-31 15:29:50

List of Learning Resources
by SilverTiger
2014-07-31 15:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!