Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-24-2008, 04:18 AM
Member
 
Join Date: Apr 2008
Posts: 2
wtver87 is on a distinguished road
Help in Java Encryption
Hi everyone,

I've tried a lot of way but none of them works. What I'm trying to do is I'm trying to encrypt a string and store it to a file, then read that encryption and decrypt it so my program can use that string again. The storage is just using a FileOutputStream and I'm reading using FileInputStream, I can write it fine, but after I'm done reading the file and try to decrypt it, it give me javax.crypto.BadPaddingException

Here's a snippet of my code
Code:
FileInputStream fis = new FileInputStream(path +"\\dat" + i + ".txt"); Scanner sc = new Scanner(fis); String curr = ""; if (sc.hasNextLine()) curr = sc.nextLine(); while(sc.hasNextLine()) { curr += '\n' + sc.nextLine(); } System.out.println("Curr is: " + new String(curr)); data[i] = new String(gwc.decrypt(curr)); sc.close();
I've done the try catch too, i just ommited it for simplicity

Thank you in advance
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-24-2008, 06:37 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 286
roots is on a distinguished road
Why are you reading a "Encrypted Content" with scanner and adding \n on it ??

If possibe paste the gwc.decrypt
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 07:56 AM
Member
 
Join Date: Apr 2008
Posts: 2
wtver87 is on a distinguished road
Hi roots,

Thanks for the reply, I'm reading "The encrypted content" with scanner and adding '\n' on it is because sometimes the encryption result with something like this ~!@#$%%\n&^%$, which will be read by the scanner as the end of the input session, therefore what I did is when it's detected, add a '\n' to recover what Scanner has ignored + next token. Is there a better way of doing this? Is there a way I can say just take the whole thing till end of file?

Code:
byte[] decrypt(String input) { System.out.println(input); return action(input, Cipher.DECRYPT_MODE); } private byte[] action(String input, int mode) { try { cipher.init(mode, key); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch(Exception e) { System.out.println("Unknown error in Cipher init"); } byte [] enres = null; try { enres = cipher.doFinal(input.getBytes()); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch(Exception e) { System.out.println("Unknown error in crypting process"); } return enres; }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-24-2008, 08:11 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 286
roots is on a distinguished road
What you are doing is byte -> String -> byte[]
read as byte[] from inputstream and "update" that to Cipher. and at the end doFinal with empty byte to collect the output.
Respective javadoc should help you

Cipher (Java 2 Platform SE v1.4.2)[],%20int,%20int)
InputStream (Java 2 Platform SE v1.4.2)[],%20int,%20int)
__________________
dont worry newbie, we got you covered.

Last edited by roots : 04-24-2008 at 08:14 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
encryption Joe2003 Advanced Java 2 02-06-2008 12:27 PM
Encryption/Decryption Echilon New To Java 0 12-27-2007 03:55 PM
Encryption/Decryption Through AOP SirRawlins Advanced Java 0 12-19-2007 05:22 PM
Java encryption soul_krasty Advanced Java 3 08-14-2007 04:44 PM
Encryption in java Ed Advanced Java 1 07-09-2007 03:47 AM


All times are GMT +3. The time now is 09:27 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org