Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Error reading file  (Read 345 times)
0 Members and 2 Guests are viewing this topic.
Offline xslaught

JGO n00b
*

Posts: 3



« on: 2012-01-24 17: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

Jr. Member
**

Posts: 66
Medals: 3



« Reply #1 on: 2012-01-24 17: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
Online ra4king

JGO Kernel
*****

Posts: 3154
Medals: 196


I'm the King!


« Reply #2 on: 2012-01-24 17: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! Go get 'em!
Offline Stranger

JGO n00b
*

Posts: 42
Medals: 1



« Reply #3 on: 2012-01-25 03: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 Kernel
*****

Posts: 5866
Medals: 255


Hand over your head.


« Reply #4 on: 2012-01-25 08: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
Online ra4king

JGO Kernel
*****

Posts: 3154
Medals: 196


I'm the King!


« Reply #5 on: 2012-01-25 08: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]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.073 seconds with 19 queries.