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 MD5 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

This is a program that could digest the M letter with MD5 algorithm.What is i want to digest the text from the text file .Something regarding to the FileInputStream but i have no idea how to embed into it .Please ,show it to me

Recommended Answers

All 3 Replies

OK ,this time i had tweak some of the code ,it was successfully compiled but display the logical error in cmd "Exception in thread "main" java.lang.NullPointer Exception.......why ?

import java.net.*;
import java.io.*;
import java.security.*;
import javax.crypto.*;

public class messagedigestsend {
static String strLine;
public static void main( String [ ] args) throws Exception{

 


 try{
    // Open the file that is the first 
    // command line parameter
    FileInputStream fstream = new FileInputStream("textfile.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
        
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   
    {
    
     System.out.println (strLine);
    
    }
    //Close the input stream
        in.close();
        }
        catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
    }







	byte ttl= (byte) 0;

	try
	   {
		MulticastSocket mSocket = new MulticastSocket();
		MulticastSocket mSocket2 = new MulticastSocket();

		InetAddress mAddr = InetAddress.getByName("224.0.0.1");
		String sendString = strLine;


		byte[] plainText = sendString.getBytes("UTF8");
		    //
		    // get a message digest object using the MD5 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

anyway to correct it ?

From: http://www.google.co.in/search?q=NullPointerException

NullPointerException
Thrown when an application attempts to use null in a case where an object is required. These include:

* Calling the instance method of a null object.
* Accessing or modifying the field of a null object.
* Taking the length of null as if it were an array.
* Accessing or modifying the slots of null as if it were an array.
* Throwing null as if it were a Throwable value.

Applications should throw instances of this class to indicate other illegal uses of the null object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.