Reputation: 23
I got this script somewhere of the internet, what it should do is encrypt the text into md5 and show the result (I think) I'm new to Java and can only do a little php and python. I setup JDK so I can run it in cmd. What I type in is >javac MD5.java Then it runs the code and C:\file> show up again as if nothing happend. It doesn't print out the hash to me does anyone know why?
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
public static String getMD5(String input) {
byte[] source;
try {
//Get byte according by specified coding.
source = input.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
source = input.getBytes();
}
String result = null;
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(source);
//The result should be one 128 integer
byte temp[] = md.digest();
char str[] = new char[16 * 2];
int k = 0;
for (int i = 0; i < 16; i++) {
byte byte0 = temp[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
result = new String(str);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println(getMD5("test"));
}
}
Upvotes: 0
Views: 109
Reputation: 159874
javac MD5.java
compiles the code. To run the application use
java MD5
Upvotes: 6