import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.*;
import java.security.*;
import java.lang.*;
import java.math.*;
import java.util.*;
import java.security.spec.*;
class md5
{
public static void main(String args[]) throws Exception
{
DataInputStream ds=new DataInputStream(System.in);
System.out.println("Enter the Message to be hashed....");
String m=ds.readLine();
BigInteger hshm;
hshm=hshcall(m);
System.out.println("After Hashing "+m+" we get....");
System.out.println(hshm.toString());
rsa1(hshm.toString());
}
static public BigInteger hshcall(String M){
byte[] toMessageDigest = M.getBytes();
byte[] hash_message = new byte[20];
BigInteger h;
try{
MessageDigest md4 = MessageDigest.getInstance("MD5");
md4.update(toMessageDigest);
hash_message = md4.digest();
}
catch (NoSuchAlgorithmException nsae) {
System.err.println("Caught NSAException: " + nsae.getMessage());
}
h = new BigInteger(hash_message);
return h.abs();
}
public static void rsa1(String message1)
{
BigInteger p = new BigInteger ( 5,10, new Random () ) ;
BigInteger q = new BigInteger ( 5,10, new Random () ) ;
BigInteger n = p.multiply ( q ) ;
BigInteger e,d;
System.out.println ( "P = " + p + "\nQ = " + q + "\nN = " + n ) ;
BigInteger t = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)) ;
do
{
e = new BigInteger( 5,10, new Random() ) ;
} while ( t.gcd(e) == BigInteger.ONE ) ;
d = e.modInverse ( t ) ;
System.out.println ( "E = " + e + "\nD = " + d ) ;
//System.out.println("Enter message");
//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String message=message1;
System.out.println("Encrypted Message");
int i ;
byte[] temp = new byte[1] ;
byte[] digits = message.getBytes() ;
BigInteger[] bigdigits = new BigInteger[digits.length] ;
for( i = 0 ; i < bigdigits.length ; i++ )
{
temp[0] = digits[i] ;
bigdigits[i] = new BigInteger( temp ) ;
}
BigInteger[] encrypted = new BigInteger[bigdigits.length] ;
for( i = 0 ; i < bigdigits.length ; i++ )
{
encrypted[i] = bigdigits[i].modPow( e, n ) ;
System.out.print(encrypted[i]);
}
String s=encrypted.toString();
System.out.println(s);
char[] charArray1 = new char[encrypted.length] ;
for( i = 0 ; i < charArray1.length ; i++ )
charArray1[i] = (char) (encrypted[i].intValue());
//System.out.println("\n \n");
System.out.println( new String(charArray1)) ;
System.out.println("\n\nDecrypted Message");
BigInteger[] decrypted = new BigInteger[encrypted.length] ;
for( i = 0 ; i < decrypted.length ; i++ )
decrypted[i] = encrypted[i].modPow(d,n);
char[] charArray = new char[decrypted.length] ;
for( i = 0 ; i < charArray.length ; i++ )
charArray[i] = (char) (decrypted[i].intValue());
System.out.println( new String(charArray)) ;
String signmessage=new String(charArray);
if(signmessage.equals(message1))
System.out.println("Signature verified");
else
System.out.println("Signature verification failed");
}}
I want that encrypted should get convert to char or string so i can store in datatbase
I hav tried to write at line no 81.
Pls help me