Results 1 to 4 of 4
  1. #1
    wtver87 is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default 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
    Java 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

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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.

  3. #3
    wtver87 is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default

    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;
    	}

  4. #4
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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 07:14 AM.
    dont worry newbie, we got you covered.

Similar Threads

  1. Encryption/Decryption
    By Echilon in forum New To Java
    Replies: 2
    Last Post: 03-24-2009, 12:58 PM
  2. encryption
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 02-06-2008, 11:27 AM
  3. Encryption/Decryption Through AOP
    By SirRawlins in forum Advanced Java
    Replies: 0
    Last Post: 12-19-2007, 04:22 PM
  4. Java encryption
    By soul_krasty in forum Advanced Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 PM
  5. Encryption in java
    By Ed in forum Advanced Java
    Replies: 1
    Last Post: 07-09-2007, 02:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •