Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 06-16-2004, 07:57 AM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
MD5 Ecrypt with Java

What is the say to MD5 encrypt a string with Java?
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 06-16-2004, 08:31 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
You can use the javax.crypto package, or you can download and use this LGPL package, which I prefer because it'll spit back hex instead of bytes.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 06-16-2004, 08:32 AM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
well here is what i came up with after searching, .. but i want it to be like the 32 character hash that the php md5() function creates.
Code:
public static String encryptString(String str){ byte[] originalByteArray = str.getBytes(); String returnString = ""; try{ MessageDigest digest = MessageDigest.getInstance("MD5"); digest.reset(); digest.update(originalByteArray); byte[]arr = digest.digest(); returnString = arr.toString(); }catch(Exception e){ System.out.println("Error:encryptString()"); return returnString; } return returnString; }
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 06-16-2004, 08:32 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
oops, .. posted at the same time .. thanks i'll check that out.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 06-16-2004, 11:17 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
ok, well here is answers i found in another forum regarding making the hash look exactly like php:
Quote:
MD5 is a 128-bit hash.
128 / 8 = 16 bytes
16 bytes, converted to hexadecimal, will give 32 characters.
So convert your MD5 output to hexadecimal
Code:
import java.util.*; import java.io.*; import java.security.*; public class Test { public static String hex(byte[] array) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < array.length; ++i) { sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1,3)); } return sb.toString(); } public static String md5 (String message) { try { MessageDigest md = MessageDigest.getInstance("MD5"); return hex (md.digest(message.getBytes("CP1252"))); } catch (NoSuchAlgorithmException e) { } catch (UnsupportedEncodingException e) { } return null; } public static void main(String[] args) { System.out.println (md5 ("Hello, world!")); } }
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 07-20-2004, 05:59 AM   #6 (permalink)
SpaceMonkey
Registered User
 
SpaceMonkey's Avatar
 
Join Date: Jul 2004
Location: Rochester, NY
Posts: 7
SpaceMonkey is on a distinguished road
Lightbulb

If you remove the .toUpperCase(). portion of the code in the hex() function, then the output of the hashing function will be compatible with the output format of Unix/Linux versions of md5sum as well as this JavaScript function for computing MD5 hashes client side (for implementing CHAP login systems)
__________________
SpaceMonkey is offline   Reply With Quote
Old 07-20-2004, 06:05 AM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
oh nice! finding the javascript version has been in the back of my mind for a while now. thanks space!
__________________
testing 1 2 3
sde is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Developer Position in Kansas City justplaindoug Java 0 10-29-2004 11:52 AM
JAVA Hobbyists Wanted emteam Java 0 08-29-2004 11:27 AM
IBM urges Sun to make Java open source redhead Code Newbie News 0 03-01-2004 10:39 PM
Java Class Basics: Static Methods bdl Java 0 02-29-2004 02:25 PM
What is OOP and Java? srt42 Java 0 04-07-2003 01:21 AM


All times are GMT -8. The time now is 04:09 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle