Results 1 to 4 of 4
Thread: Help in Java Encryption
- 04-24-2008, 02:18 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
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
I've done the try catch too, i just ommited it for simplicityJava 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();
Thank you in advance
- 04-24-2008, 04:37 AM #2
Why are you reading a "Encrypted Content" with scanner and adding \n on it ??
If possibe paste the gwc.decryptdont worry newbie, we got you covered.
- 04-24-2008, 05:56 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
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?
Java 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; }
- 04-24-2008, 06:11 AM #4
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)Last edited by roots; 04-24-2008 at 06:14 AM.
dont worry newbie, we got you covered.
Similar Threads
-
Encryption/Decryption
By Echilon in forum New To JavaReplies: 2Last Post: 03-24-2009, 11:58 AM -
encryption
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 02-06-2008, 10:27 AM -
Encryption/Decryption Through AOP
By SirRawlins in forum Advanced JavaReplies: 0Last Post: 12-19-2007, 03:22 PM -
Java encryption
By soul_krasty in forum Advanced JavaReplies: 3Last Post: 08-14-2007, 02:44 PM -
Encryption in java
By Ed in forum Advanced JavaReplies: 1Last Post: 07-09-2007, 01:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks