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

    Default variable results using same inputs

    Hello, when encrypting a password, I noticed my encryption function produces different outputs even with the same inputs.
    Question: What causes them to be different every time?

    This is the function I use to encrypt my passwords:

    Java Code:
        private String encryptPassword(String password) {
            
            try{
                byte[] passwordBytes = password.getBytes();
                
                MessageDigest m = MessageDigest.getInstance("MD5");
                m.reset();
                
                for (int i=0; i<1000; i++){
                    m.update(passwordBytes);
                    m.digest();
                } 
                return passwordBytes.toString();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
    	}
            return null;
        }
    Calling code:
    Java Code:
    System.out.println(password);
            System.out.println(encryptPassword(password));
            System.out.println(password);
            System.out.println(encryptPassword(password));
            System.out.println(password);
            System.out.println(encryptPassword(password));
    Output: (passwords masked, but they are the same)

    ******
    [B@47cf34
    ******
    [B@746d6d
    ******
    [B@1bc26ee

    Output from running the program a second time, no changes to the code or inputs:

    ******
    [B@1bc26ee
    ******
    [B@1a00355
    ******
    [B@3af8a6

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: variable results using same inputs

    You are returning the toString() value of an array which does not reflect the contents of that array, rather properties of the array instance itself (which will be different for every call to the method). Try creating a new String with the given byte array. And note that m.digest returns something.

Similar Threads

  1. Replies: 0
    Last Post: 09-11-2011, 05:34 PM
  2. Help with 2-D Arrays and Inputs
    By CJ1031 in forum New To Java
    Replies: 2
    Last Post: 04-16-2011, 03:56 AM
  3. How to create this if many inputs?
    By sarahannel123 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 05:22 PM
  4. Date Inputs
    By hiranya in forum AWT / Swing
    Replies: 3
    Last Post: 11-06-2007, 06:11 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
  •