Friday 6 September 2013

How to encode Text message into SHA1 and MD5 in java

Encode text in SHA1

import java.security.*;
import java.math.*;

public class SHA1 {
    public static void main(String args[]) throws Exception{
        String txt="Hello Ramsis";
        MessageDigest m=MessageDigest.getInstance("SHA1");
        m.update(txt.getBytes(),0,txt.length());
        System.out.println("SHA1 Code: "+new BigInteger(1,m.digest()).toString(16));
    }
}


//--------------------------------------------------------------------------------------------------

Encode text in MD5

import java.security.*;
import java.math.*;

public class MD5 {
    public static void main(String args[]) throws Exception{
        String txt="Aman";
        MessageDigest m=MessageDigest.getInstance("MD5");
        m.update(txt.getBytes(),0,txt.length());
        System.out.println("MD5 Code: "+new BigInteger(1,m.digest()).toString(16));
    }
}


No comments:

Post a Comment