Results 1 to 2 of 2
- 12-15-2011, 04:03 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
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:
Calling code: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; }
Output: (passwords masked, but they are the same)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));
******
[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
- 12-15-2011, 07:54 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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
-
Netbeans axis2 java web service: Results not shown with input variable in URL
By wk3000sg in forum Advanced JavaReplies: 0Last Post: 09-11-2011, 04:34 PM -
Help with 2-D Arrays and Inputs
By CJ1031 in forum New To JavaReplies: 2Last Post: 04-16-2011, 02:56 AM -
How to create this if many inputs?
By sarahannel123 in forum New To JavaReplies: 3Last Post: 05-18-2008, 04:22 PM -
Date Inputs
By hiranya in forum AWT / SwingReplies: 3Last Post: 11-06-2007, 05:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks