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 2 of 2

Thread: My new questions

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My new questions

    hello
    I create program for Encrypted / Decrypted file. I have problem with code for Decrypted file. File its don’t be save normal. I get this error
    File Decrypted.
    Exception in thread "main" java.io.IOException: Stream Closed
    at java.io.FileOutputStream.writeBytes(Native Method)
    at java.io.FileOutputStream.write(FileOutputStream.ja va:313)
    at preprosti.arhiv5.odkrij(arhiv5.java:101)
    at preprosti.Preprosti.main(Preprosti.java:35)
    C:\Users\igor\AppData\Local\NetBeans\Cache\10.0\ex ecutor-snippets\run.xml:111: The following error occurred while executing this line:
    C:\Users\igor\AppData\Local\NetBeans\Cache\10.0\ex ecutor-snippets\run.xml:94: Java returned: 1
    BUILD FAILED (total time: 0 seconds)

    My code

     
    package preprosti;
     
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.security.AlgorithmParameters;
    import java.security.SecureRandom;
    import java.security.spec.KeySpec;
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.PBEKeySpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.spec.KeySpec;
    import java.util.Base64;
     
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.PBEKeySpec;
    import javax.crypto.spec.SecretKeySpec;
     
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.spec.InvalidKeySpecException;
    import javax.crypto.NoSuchPaddingException;
     
     
    /**
     * klicem arhiv 4
     * @author igor
     */
    public class arhiv5 {
     private static String superKey = "boooooooooom!!!!";
    private static String salt = "ssshhhhhhhhhhh!!!!";    
    // code for Encrypted file
      public void skrij(String[] args) throws Exception {
    		FileInputStream inFile = new FileInputStream("C:\\Users\\igor\\Documents\\NetBeansProjects\\preprosti\\jaslice.jpg");
    		FileOutputStream outFile = new FileOutputStream("C:\\Users\\igor\\Documents\\NetBeansProjects\\preprosti\\jaslice2.jpg");
     
                     byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            IvParameterSpec ivspec = new IvParameterSpec(iv);
     
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
            KeySpec spec = new PBEKeySpec(superKey.toCharArray(), salt.getBytes(), 65536, 256);
            SecretKey tmp = factory.generateSecret(spec);
            SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
     
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
    		byte[] input = new byte[64];
    		int bytesRead;
     
    		while ((bytesRead = inFile.read(input)) != -1) {
    			byte[] output = cipher.update(input, 0, bytesRead);
    			if (output != null)
    				outFile.write(output);
    		}
     
    		byte[] output = cipher.doFinal();
    		if (output != null)
    			outFile.write(output);
     
    		inFile.close();
    		outFile.flush();
    		outFile.close();
     
    		System.out.println("File Encrypted.");
    			}
     
     
    // code for Decrypted file
    public void odkrij(String[] args)throws Exception {
     
    byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            IvParameterSpec ivspec = new IvParameterSpec(iv);
                  	SecretKeyFactory factory = SecretKeyFactory
    		.getInstance("PBKDF2WithHmacSHA1");
    		KeySpec spec = new PBEKeySpec(superKey.toCharArray(), salt.getBytes(), 65536, 256);
    		SecretKey tmp = factory.generateSecret(spec);
    		SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    		cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
    		FileInputStream fis = new FileInputStream("C:\\Users\\igor\\Documents\\NetBeansProjects\\preprosti\\jaslice2.jpg");
    		FileOutputStream fos = new FileOutputStream("C:\\Users\\igor\\Documents\\NetBeansProjects\\preprosti\\jaslicee3.jpg");
    byte[] in = new byte[64];
    		int read;
    		while ((read = fis.read(in)) != -1) {
    			byte[] output = cipher.update(in, 0, read);
    			if (output != null)
    				fos.write(output);
                            fos.flush();
    		fos.close();
    		System.out.println("File Decrypted.");
    		}
        }
     
      }
    Last edited by pingusteam; September 5th, 2022 at 12:19 PM.

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

    Default Re: My new questions

    It looks like the call to the close method is inside of the while loop.
    It should be outside of the loop to only close the file one time, not every time the loop iterates.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Questions!
    By bboyinmartin in forum Java Theory & Questions
    Replies: 2
    Last Post: July 16th, 2013, 02:03 AM
  2. List of my Java3D Questions, and Proguard questions
    By Zachary1234 in forum Java SE APIs
    Replies: 0
    Last Post: November 16th, 2012, 09:40 PM
  3. A few questions
    By elatechris777 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 5th, 2012, 06:33 PM
  4. Need help with some questions!
    By goha14 in forum Java Theory & Questions
    Replies: 6
    Last Post: March 27th, 2012, 08:01 PM
  5. Few questions
    By CSUTD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2011, 09:23 PM