Results 1 to 5 of 5
  1. #1
    werner291 is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Same inputs, different outputs. (solved)

    Hello, I'm using this function (see below) to encrypt a string.

    For my program, it is important that it always gives the same outputs, but, when run multiple times, it gives me completely different outputs when supplied with exactly the same input string. Anything i'm not resetting or missing?

    Java Code:
    private String encryptPassword(String password) {
            System.out.println(password);
            try{
                byte[] passwordBytes = password.getBytes( "8859_1");
                
                MessageDigest m = MessageDigest.getInstance("MD5");
                m.reset();
                
                for (int i=0; i<1000; i++){
                    m.update(passwordBytes);
                    m.digest();
                } 
                return passwordBytes.toString();
            } catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    	} catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    	}
            return null;
        }
    The console output:

    Teststring
    Teststring
    [B@9eb46b
    Teststring
    Teststring
    [B@156ee8e
    Teststring
    Teststring
    [B@47de8f

    The code calling this function:
    Java Code:
            System.out.println("Teststring");
            System.out.println(encryptPassword("Teststring"));
            System.out.println("Teststring");
            System.out.println(encryptPassword("Teststring"));
            System.out.println("Teststring");
            System.out.println(encryptPassword("Teststring"));
    You can see that the function is supplied with "Teststring" every time, but gives different outputs.
    Last edited by werner291; 12-15-2011 at 07:37 PM. Reason: added "(solved)"

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Same inputs, different outputs.

    Please show what is happening. Show the inputs and the outputs that you are talking about.

  3. #3
    werner291 is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Same inputs, different outputs.

    As you can see, while this function is given the same input ("Teststring"), it gives a different output every time it is called. Why?

    So... This is the function being called:

    Java Code:
    private String encryptPassword(String password) {
            System.out.println(password);
            try{
                byte[] passwordBytes = password.getBytes( "8859_1");
                
                MessageDigest m = MessageDigest.getInstance("MD5");
                m.reset();
                
                for (int i=0; i<1000; i++){
                    m.update(passwordBytes);
                    m.digest();
                } 
                return passwordBytes.toString();
            } catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    	} catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    	}
            return null;
        }
    Console output:

    Teststring
    Teststring
    [B@9eb46b
    Teststring
    Teststring
    [B@156ee8e
    Teststring
    Teststring
    [B@47de8f

    Code calling this function:

    Java Code:
    System.out.println("Teststring");
            System.out.println(encryptPassword("Teststring"));
            System.out.println("Teststring");
            System.out.println(encryptPassword("Teststring"));
            System.out.println("Teststring");
            System.out.println(encryptPassword("Teststring"));
    Last edited by werner291; 12-15-2011 at 06:35 PM.

  4. #4
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Same inputs, different outputs.

    Haven't used MessageDigest before, but my intuition suggests that the reset method is in the wrong place. Put it inside the loop, perhaps?

    EDIT: I've just seen the API. digest() method does a reset() for you....hmmm....

    Oh, I see, what you need is Arrays.toString(passwordBytes). What you are returning is the memory location of the array not the string representation of its contents. That is why it is changing.
    Last edited by 2by4; 12-15-2011 at 07:05 PM.

  5. #5
    werner291 is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Same inputs, different outputs.

    @2by4 (Yup, it works!)

    Now the rest of this project...

Similar Threads

  1. Replies: 26
    Last Post: 12-03-2013, 02:55 PM
  2. Replies: 5
    Last Post: 11-20-2011, 04:44 AM
  3. I want to put text outputs in the GUI console.
    By illinit in forum New To Java
    Replies: 2
    Last Post: 10-19-2011, 11:21 PM
  4. Replies: 8
    Last Post: 09-12-2011, 01:43 PM
  5. Replies: 0
    Last Post: 07-22-2011, 12:31 PM

Posting Permissions

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