Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Message digest from file (http://www.go4expert.com/forums/message-digest-file-t20755/)

low1988 25Jan2010 13:24

Message digest from file
 
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:

  1. import java.net.*;

  2. import java.io.*;

  3. import java.security.*;

  4. import javax.crypto.*;

  5. public class messagedigestsend {

  6. public static void main( String [ ] args) throws Exception{

  7.         byte ttl= (byte) 0;

  8.         try

  9.           {

  10.                 MulticastSocket mSocket = new MulticastSocket();

  11.                 MulticastSocket mSocket2 = new MulticastSocket();

  12.                 InetAddress mAddr = InetAddress.getByName("224.0.0.1");

  13.                 String sendString = "M";

  14.                 byte[] plainText = sendString.getBytes("UTF8");

  15.                     //

  16.                     // get a message digest object using the MD5 algorithm

  17.                     MessageDigest messageDigest = MessageDigest.getInstance("MD5");

  18.                     //

  19.                     // print out the provider used

  20.                     System.out.println( "\n" + messageDigest.getProvider().getInfo() );

  21.                     //

  22.                     // calculate the digest and print it out

  23.                     messageDigest.update( plainText);

  24.                     String messagetransfer = new String( messageDigest.digest(), "UTF8");

  25.                         byte[] digestText = messagetransfer.getBytes("UTF8");

  26.                     System.out.println( "\nPlain text sent is: "+sendString);

  27.                     System.out.println( "\nDigest message sent is: "+messagetransfer);

  28.                         //byte [ ] buffer = sendString.getBytes();

  29.                         DatagramPacket dp = new DatagramPacket(plainText, plainText.length, mAddr, 5000);

  30.                         mSocket.send(dp);

  31.                         DatagramPacket dp2 = new DatagramPacket(digestText, digestText.length, mAddr, 5001);

  32.                         mSocket2.send(dp2);

  33.           }

  34.         catch (SocketException se)

  35.           {

  36.                 System.out.println("Socket Exception : " + se);

  37.           }

  38.         catch (IOException e)

  39.           {

  40.                 System.out.println("Exception : " + e);

  41.           }

  42.         }//end of main

  43. }// end of class definition




All times are GMT +5.5. The time now is 23:08.