Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: java.io.StreamCorruptedException: invalid stream header

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java.io.StreamCorruptedException: invalid stream header

    Hello,

    I am trying to encrypt a message using RSA algo but getting the following error:

    java.io.StreamCorruptedException: invalid stream header
    at java.io.ObjectInputStream.readStreamHeader(ObjectI nputStream.java:753)
    at java.io.ObjectInputStream.<init>(ObjectInputStream .java:268)

                 public static final String PUBLIC_KEY_FILE = "C:/TXT.key";
     
                 final String originalText = "Text to be encrypted ";
    	      ObjectInputStream inputStream = null;
    	      FileInputStream f=  new FileInputStream(PUBLIC_KEY_FILE);
    	      //  getting error on the below line
    	      inputStream = new ObjectInputStream(f);      
                  final PublicKey publicKey = (PublicKey) inputStream.readObject();
    	      final byte[] cipherText = encrypt(originalText, publicKey);


    Please guide.

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java.io.StreamCorruptedException: invalid stream header

    Can you post a small, complete program that compiles, executes and shows the problem.
    Also post the compete text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.io.StreamCorruptedException: invalid stream header

    package in.javadigest.encryption;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.Key;
     
     
    import javax.crypto.Cipher;
     
    public class EncryptionUtil {
     
    	  /**
    	   * String to hold name of the encryption algorithm.
    	   */
    	  public static final String ALGORITHM = "RSA";
     
    	  /**
    	   * String to hold the name of the private key file.
    	   */
    	  public static final String PRIVATE_KEY_FILE = "C:/TXT2.key";
     
    	  /**
    	   * String to hold name of the public key file.
    	   */
    	  public static final String PUBLIC_KEY_FILE = "C:/TXT.key";
     
    	  /**
    	   * Generate key which contains a pair of private and public key using 1024
    	   * bytes. Store the set of keys in Prvate.key and Public.key files.
    	   * 
    	   * @throws NoSuchAlgorithmException
    	   * @throws IOException
    	   * @throws FileNotFoundException
    	   */
    	  public static void generateKey() {
    	    try {
    	      final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
    	      keyGen.initialize(1024);
    	      final KeyPair key = keyGen.generateKeyPair();
     
    	      File privateKeyFile = new File(PRIVATE_KEY_FILE);
    	      File publicKeyFile = new File(PUBLIC_KEY_FILE);
     
    	      // Create files to store public and private key
    	      if (privateKeyFile.getParentFile() != null) {
    	        privateKeyFile.getParentFile().mkdirs();
    	      }
    	      privateKeyFile.createNewFile();
     
    	      if (publicKeyFile.getParentFile() != null) {
    	        publicKeyFile.getParentFile().mkdirs();
    	      }
    	      publicKeyFile.createNewFile();
     
    	      // Saving the Public key in a file
    	      ObjectOutputStream publicKeyOS = new ObjectOutputStream(
    	          new FileOutputStream(publicKeyFile));
    	      publicKeyOS.writeObject(key.getPublic());
    	      publicKeyOS.close();
     
    	      // Saving the Private key in a file
    	      ObjectOutputStream privateKeyOS = new ObjectOutputStream(
    	          new FileOutputStream(privateKeyFile));
    	      privateKeyOS.writeObject(key.getPrivate());
    	      privateKeyOS.close();
    	    } catch (Exception e) {
    	      e.printStackTrace();
    	    }
     
    	  }
     
    	  /**
    	   * The method checks if the pair of public and private key has been generated.
    	   * 
    	   * @return flag indicating if the pair of keys were generated.
    	   */
    	  public static boolean areKeysPresent() {
     
    	    File privateKey = new File(PRIVATE_KEY_FILE);
    	    File publicKey = new File(PUBLIC_KEY_FILE);
     
    	    if (privateKey.exists() && publicKey.exists()) {
    	      return true;
    	    }
    	    return false;
    	  }
     
    	  /**
    	   * Encrypt the plain text using public key.
    	   * 
    	   * @param text
    	   *          : original plain text
    	   * @param key
    	   *          :The public key
    	   * @return Encrypted text
    	   * @throws java.lang.Exception
    	   */
    	  public static byte[] encrypt(String text, PublicKey key) {
    	    byte[] cipherText = null;
    	    try {
    	      // get an RSA cipher object and print the provider
    	      final Cipher cipher = Cipher.getInstance(ALGORITHM);
    	      // encrypt the plain text using the public key
    	      cipher.init(Cipher.ENCRYPT_MODE, key);
    	      cipherText = cipher.doFinal(text.getBytes());
    	    } catch (Exception e) {
    	      e.printStackTrace();
    	    }
    	    return cipherText;
    	  }
     
    	  /**
    	   * Decrypt text using private key.
    	   * 
    	   * @param text
    	   *          :encrypted text
    	   * @param key
    	   *          :The private key
    	   * @return plain text
    	   * @throws java.lang.Exception
    	   */
    	  public static String decrypt(byte[] text, PrivateKey key) {
    	    byte[] dectyptedText = null;
    	    try {
    	      // get an RSA cipher object and print the provider
    	      final Cipher cipher = Cipher.getInstance(ALGORITHM);
     
    	      // decrypt the text using the private key
    	      cipher.init(Cipher.DECRYPT_MODE, key);
    	      dectyptedText = cipher.doFinal(text);
     
    	    } catch (Exception ex) {
    	      ex.printStackTrace();
    	    }
     
    	    return new String(dectyptedText);
    	  }
     
     
     
     
    	  /**
    	   * Test the EncryptionUtil
    	   */
    	  public static void main(String[] args) {
     
    	    try {
     
    	      // Check if the pair of keys are present else generate those.
    	      if (!areKeysPresent()) {
    	        // Method generates a pair of keys using the RSA algorithm and stores it
    	        // in their respective files
    	        generateKey();
    	      }
     
    	      final String originalText = "Text to be encrypted ";
    	      ObjectInputStream inputStream = null;
    	   FileInputStream f=  new FileInputStream(PUBLIC_KEY_FILE);
    	      // Encrypt the string using the public key
    	      inputStream = new ObjectInputStream(f);
    	      final PublicKey publicKey = (PublicKey) inputStream.readObject();
    	      final byte[] cipherText = encrypt(originalText, publicKey);
     
     
    	      // Decrypt the cipher text using the private key.
    	      inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
    	      final PrivateKey privateKey = (PrivateKey) inputStream.readObject();
    	      final String plainText = decrypt(cipherText, privateKey);
     
    	      // Printing the Original, Encrypted and Decrypted Text
    	      System.out.println("Original Text: " + originalText);
    	      System.out.println("Encrypted Text: " +cipherText.toString());
    	      System.out.println("Decrypted Text: " + plainText);
     
    	    } catch (Exception e) {
    	      e.printStackTrace();
    	    }
    	  }
    	}

    Error:java.io.StreamCorruptedException: invalid stream header
    at java.io.ObjectInputStream.readStreamHeader(ObjectI nputStream.java:753)
    at java.io.ObjectInputStream.<init>(ObjectInputStream .java:268)
    at in.javadigest.encryption.EncryptionUtil.main(Encry ptionUtil.java:172)
    Last edited by MHZ; July 10th, 2014 at 07:19 AM. Reason: code tag

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java.io.StreamCorruptedException: invalid stream header

    The code works for me:
    Running: java -client -cp . EncryptionUtil

    Original Text: Text to be encrypted
    Encrypted Text: [B@15696b7 <<<<<<<<<<<< Use Arrays.toString() here
    Decrypted Text: Text to be encrypted

    0 error(s)
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.io.StreamCorruptedException: invalid stream header

    Strange. Then why its not working for me? I am using Myeclipse tool.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java.io.StreamCorruptedException: invalid stream header

    I use an enhanced editor that uses the command line.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.io.StreamCorruptedException: invalid stream header

    Thanks.
    It worked out when I run it through command prompt.
    Can you help me if I want to encrypt and decrypt by using fixed keys?

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: java.io.StreamCorruptedException: invalid stream header

    There's nothing wrong with the code you posted. What help do you need?

  9. #9
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.io.StreamCorruptedException: invalid stream header

    Please convert the following C# code to Java code.

    StreamReader reader = new StreamReader(filename);
    String modulus64 = reader.ReadLine();
    String exponent64 = reader.ReadLine();

    Thanks

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: java.io.StreamCorruptedException: invalid stream header

    We'll get right on that.

Similar Threads

  1. Set Footer and Header in Document using Java
    By raghu3.ankam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2014, 07:01 AM
  2. How to extract and Parse email header in OBPM using java
    By kennethdizon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2014, 05:11 AM
  3. Help required in creating header file using java
    By Phoenix23 in forum Java Native Interface
    Replies: 8
    Last Post: March 28th, 2013, 08:05 AM
  4. could not create audio stream from input stream
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 2nd, 2011, 02:08 AM
  5. Urgent: invalid stream header: 5B47656E
    By DanBrown in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 28th, 2011, 07:30 AM

Tags for this Thread