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

Thread: Encryption problem with CipherInputStream/CipherOutputStream

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Encryption problem with CipherInputStream/CipherOutputStream

    Hi I have the following code I tested to encrypt/decrypt file and all is good. It works correctly for full file encryption and now I want to test partial encryption of 100000 bytes. I tried read the 100000 from a text file full of "qwerty" text and encrypt it in another file and I am able to decrypt this file back to the 100000 bytes of "qwerty" output to a new test file.

    So it seems like the partial encryption is ok, all I need is implement CipherInputStream to decrypt the first 100000 bytes write to a output file and do a normal FileInputStream and write to the output write. Theoretically I should have implemented the partial file encryption/decryption.

    Now, when I open the output file, I saw unreadable character at EOL. What is wrong, how can I correct it?

    EOF -> "qwertyqwertyqwertyqwertyqwertyqwerty±„w'çø]uX\œaQ2ËIõ“ܱõþËÜTN³'"

    Cheers.
    Last edited by cjbon; July 31st, 2012 at 08:21 PM.


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Any idea? I am about to give up...

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    ...I was warned because of spoon-feeding

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Why is this call spoon feeding? I coded it myself but couldn't find an answer, I posted in Stackoverflow and Google group no one knew the answer so I guess it is not a simple question?

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    cjbon
    I believe you. Because I don't have the full code I try to optimize your code as following. Try it.
    ...
    			out.write(salt);
    			boolean pEncrypt = true;
    			synchronized (buffer) {
    				while (true) {
    					int amountRead = in.read(buffer);
    					if (amountRead == -1)  break;
    					if (pEncrypt) {
    						cos.write(buffer, 0, amountRead);
    						pEncrypt = false;
    					} else {
    						out.write(buffer, 0, amountRead);
    					}
    				}
    			}
                    ....
    		int b = bis.read(salt);
    		if (b == -1) throw new Exception("No \"salt\" found");
    		PBEParameterSpec spec = new PBEParameterSpec(salt, ITERATIONCOUNT);
    		Cipher cipher = Cipher.getInstance(ALGORITHM2);
    		cipher.init(Cipher.DECRYPT_MODE, key, spec);
     
    		CipherInputStream in = new CipherInputStream(bis, cipher);
     
    		boolean pEncrypt = true;
    		synchronized (buffer) {
    			while (true) {
    				if (pEncrypt) {
    					b = in.read(buffer);
    					pEncrypt = false;
    				} else {
    					b = bis.read(buffer);
    				}
    				if (b == -1) break;
    				out.write(buffer, 0, b);
    			}
    		}
    ...
    and try to print the number of byte-read so that you can verify the validity?

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

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Hi Voodoo, thanks for being the first one trying to help

    I tried your code, it is exactly the same. Also, it is the full code already. I have been using this for a long time to encrypt/decrypt files but what I wanted to do now is to partially encrypt/decrypt just the first 100000 bytes for performance. The statement "pEncrypt = false" is to control the partial encryption.

    The test files are:
    qwerty.txt 616320 bytes (original file)
    qwerty.enc 616344 bytes (encrypted)
    qwerty.ret 616316 bytes (decrypted)

    File qwerty.txt were filled with words "qwerty" and I did manage to encrypt and decrypt back the first 100000 bytes by checking qwerty.ret. It is at the end of the file I have "qwertyqwertyqwertyqwertyqwertyqwerty±„w'çø]uX\œaQ2ËIõ“ܱõþËÜTN³'".

    I am not sure if CipherInput/OutputStream add something like key/salt at the beginning and end of the first and because I switch the CipherInputStream after 100000 bytes to FileInputStream and something were not being clean up/decoded correctly? I am not sure am I able to do it this way or maybe it will never work.

    I do hope it will work and just need some tweaking.
    Last edited by cjbon; July 16th, 2012 at 05:56 AM.

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    If the encryption/decryption of first 10000 bytes fail, then I will never being able to decrypt it back. I tried to output the 10000 bytes to another file and decrypt qwerty.enc first 100000 bytes to another files and I get the correct value. So I can confirmed this part works correctly except the EOL which is the part not being encrypted or decryted mainly read and write same code as normal file copying process.
    Last edited by cjbon; July 16th, 2012 at 05:57 AM.

  8. #8
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Hey cjbon
    have you noticed that qwerty.ret 616316 bytes (decrypted) is exactly about 28 characters (or bytes) shorter than qwerty.enc 616344 bytes (encrypted)?

    The tail ±„w'çø]uX\œaQ2ËIõ“ܱõþËÜTN³' is something that you look at. Because I don't have an app to run your code so I suspect that this "tail" has to be something to do with the Key or it could be the key itself. Talking about tweaking. If you find out by comparing the key with this tail that this tail is the key then you know how to get rid of it. Normally I look into the sources (SDK) and verify my assumption. But my SDK is an old version

  9. #9
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    me again. I believe the problem of your "partial" encryption and decryption is the switch from pEncrypt from true to false or vice-versa. The encryption or decryption process normally takes care about the correct content (that is why you get the correct result. Now you suddenly switch from en/de to unen/unde the "caring process" is put aside...so you get the ugly tail

  10. #10
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Very interesting, I tried adding 3 more qwerty word so:
    qwerty.txt 616338 bytes
    qwerty.enc 616362 bytes
    qwerty.ret 616334 bytes

    So it seems consistent, qwerty.ret is always 4 bytes short of the original. If it is more, I just have to discharge it... running out of brain juice...

  11. #11
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Quote Originally Posted by Voodoo View Post
    me again. I believe the problem of your "partial" encryption and decryption is the switch from pEncrypt from true to false or vice-versa. The encryption or decryption process normally takes care about the correct content (that is why you get the correct result. Now you suddenly switch from en/de to unen/unde the "caring process" is put aside...so you get the ugly tail
    Yes, I also suspected this. That is why I can encrypt/decrypt correctly if I do not do partial encryption or if the file is smaller than 100000 bytes meaning it always encrypt fully. So this will never work or a possible tweak....

  12. #12
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Quote Originally Posted by cjbon View Post
    Yes, I also suspected this. That is why I can encrypt/decrypt correctly if I do not do partial encryption or if the file is smaller than 100000 bytes meaning it always encrypt fully. So this will never work or a possible tweak....
    uh oh...if you know the consistence (4 bytes(txt or 28 bytes/Enc) then you can "shred" this tail Why not?

  13. #13
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    I can't shred because the decrypted one is 4 bytes less not more

    I just did a log of the buffer read:
    07-16 11:43:05.640: I/enctop(4914): 100000 -> first read, the encrypted part
    07-16 11:43:05.709: I/encbot(4914): 516338 -> read the rest
    07-16 11:43:08.510: I/dectop(4914): 100000 -> decrypt this part
    07-16 11:43:08.560: I/decbot(4914): 516333 -> read the rest

    Aha! The encrypted part seems ok but why is the "decbot" only 516333? Something is very wrong here.

    qwerty.txt 616338
    qwerty.enc 616362
    qwerty.ret 616334

  14. #14
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Now I realized another new thing, when I decrypt I do not have to decrypt exactly 100000 bytes, the CipherInputStream seems to have some mechanism to figure it out the encrypted data and decrypt correctly. But if I set it more than 100000 bytes (for experimenting) it will stop running.

  15. #15
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Quote Originally Posted by cjbon View Post
    Now I realized another new thing, when I decrypt I do not have to decrypt exactly 100000 bytes, the CipherInputStream seems to have some mechanism to figure it out the encrypted data and decrypt correctly. But if I set it more than 100000 bytes (for experimenting) it will stop running.
    Hi cjbon
    I took a look at the sources of CipherInputStream Source for javax.crypto.CipherInputStream (GNU Classpath 0.95 Documentation) and CipherOutputStream CipherOutputStream.java - Source Code Display - Jarvana and find it's very interesting. If you scrutinize the method int read(byte[], int, int) you'll see the read operation depend on the tag isStream = cipher.getBlockSize () == 1;...see it for yourself Probably you should check the cipher.getBlockSize () to see how big your cipher is...

  16. #16
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    cjbon
    I studied the CipherInputStream and your code... I believe to know where the problem lies
    Look at this piece of your code
    ...
    bis = new FileInputStream(secretFile);
    ...
    CipherInputStream in = new CipherInputStream(bis, cipher);
    in is also based on bis. If you check the CipherInputStream class and its read(byte[], int, int) you see:
    a) len = super.read(buf, off, len) by isStream = true, or
    b) nextBlock() is invoked....
    Now let look at this method you find byte[] buf = new byte[cipher.getBlockSize ()] and int l = in.read (buf)...then outBuffer = cipher.update (buf, 0, l). That means the outBuffer could be different to your 100000. These 2 lines confirm that:
    ...
    int l = Math.min (outBuffer.length - outOffset, len - count);
    System.arraycopy (outBuffer, outOffset, buf, count+off, l);
    ...
    As I said, if you run into troubles that look weird you'd look at the JAVA sources and ponder...
    Ciao

  17. #17
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Hi Voodoo master,

    These are cool stuff and a little too much for me to digest.. so do you think it is possible to get it working?

  18. #18
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Hi Voodoo,

    I have solved it!

    I was thinking about the CipherInputStream and the salting/key that it might be left out during the switching.
    So instead of encrypting the top 100000 bytes, I divide the files into block and only encrypt the very last block so I switch in CipherInputStream instead of switching out and it seems to works well.

    =)

  19. #19
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Quote Originally Posted by cjbon View Post
    Hi Voodoo,

    I have solved it!

    I was thinking about the CipherInputStream and the salting/key that it might be left out during the switching.
    So instead of encrypting the top 100000 bytes, I divide the files into block and only encrypt the very last block so I switch in CipherInputStream instead of switching out and it seems to works well.

    =)
    Hi cjbon,
    glad to hear that you found out the solution for yourself. This time I "won't" spoon-feed you
    That's what I also thought about. As I gave the hint about the CipherInputStream source + the in/bis relation (instead of "spoon-feeding!). The problem exist only when in was read and it could happen that some "more data than needed" was read and stored, then when you switch to bis and read bis the unread rest will be loaded. so you miss the "stored data" and get the gap of 4 or n bytes .
    Ciao and have fun!

  20. The Following User Says Thank You to Voodoo For This Useful Post:

    cjbon (July 17th, 2012)

  21. #20
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encryption problem with CipherInputStream/CipherOutputStream

    Thanks buddy, see you around!

Similar Threads

  1. Simple encryption/ decryption problem
    By searchformeaning in forum Loops & Control Statements
    Replies: 2
    Last Post: May 7th, 2012, 01:34 AM
  2. Replies: 2
    Last Post: September 23rd, 2011, 11:54 AM
  3. SMS Encryption Testing
    By ants_dj07 in forum Java ME (Mobile Edition)
    Replies: 5
    Last Post: March 27th, 2011, 11:14 AM
  4. Hello.. need help in encryption process
    By apisz8701 in forum Member Introductions
    Replies: 0
    Last Post: February 13th, 2011, 05:03 AM
  5. Java Encryption
    By xxcorrosionxx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 9th, 2011, 04:12 AM

Tags for this Thread