1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Message digest from file

Discussion in 'Java' started by low1988, Jan 25, 2010.

  1. low1988

    low1988 New Member

    Joined:
    Sep 11, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Anyone could tell me how to tweak these codes so that it could digest the text from file instead of assign letter "M".I had tried so many example of read from file algorithm to implement on it ,but failed.It keeps return the error of incompatibility object type ,since the text to be digest is byte form instead of String

    Code:
    
    [LIST=1]
    [*]import java.net.*;
    [*]import java.io.*;
    [*]import java.security.*;
    [*]import javax.crypto.*;
    [*] 
    [*]public class messagedigestsend {
    [*]public static void main( String [ ] args) throws Exception{
    [*] 
    [*]	byte ttl= (byte) 0;
    [*] 
    [*]	try
    [*]	   {
    [*]		MulticastSocket mSocket = new MulticastSocket();
    [*]		MulticastSocket mSocket2 = new MulticastSocket();
    [*] 
    [*]		InetAddress mAddr = InetAddress.getByName("224.0.0.1");
    [*]		String sendString = "M";
    [*] 
    [*]		byte[] plainText = sendString.getBytes("UTF8");
    [*]		    //
    [*]		    // get a message digest object using the [URL=http://www.go4expert.com/articles/md5-tutorial-t319/]MD5[/URL] algorithm
    [*]		    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    [*]		    //
    [*]		    // print out the provider used
    [*]		    System.out.println( "\n" + messageDigest.getProvider().getInfo() );
    [*]		    //
    [*]		    // calculate the digest and print it out
    [*]		    messageDigest.update( plainText);
    [*]		    String messagetransfer = new String( messageDigest.digest(), "UTF8");
    [*]			byte[] digestText = messagetransfer.getBytes("UTF8");
    [*]		    System.out.println( "\nPlain text sent is: "+sendString);
    [*]		    System.out.println( "\nDigest message sent is: "+messagetransfer);
    [*] 
    [*] 
    [*]			//byte [ ] buffer = sendString.getBytes();
    [*] 
    [*]			DatagramPacket dp = new DatagramPacket(plainText, plainText.length, mAddr, 5000);
    [*]			mSocket.send(dp);
    [*] 
    [*]			DatagramPacket dp2 = new DatagramPacket(digestText, digestText.length, mAddr, 5001);
    [*]			mSocket2.send(dp2);
    [*] 
    [*] 
    [*] 
    [*]	   }
    [*] 
    [*]	catch (SocketException se)
    [*]	   {
    [*]		System.out.println("Socket Exception : " + se);
    [*]	   }
    [*]	catch (IOException e)
    [*]	   {
    [*]	 	System.out.println("Exception : " + e);
    [*]	   }
    [*] 
    [*]	}//end of main
    [*]}// end of class definition
    [/LIST]
    
    
    
     

Share This Page