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

I have this for int, it works well. I am trying to make it into BigInteger as well. I have everything done, but cant get it to work for decryption either.

public class EncryptSimpleRSA extends SimpleRSA {
  public EncryptSimpleRSA(int e, int p, int q) {
    super (e,p,q) ;

  }

  public String encrypt(String plainText) {
    StringBuilder cipherText = new StringBuilder();
    for (int i = 0; i < plainText.length();i++ ) { 
      long ch = plainText.charAt(i);
      long result = ch;
      for (int j = 0; j < (e-1);j++) {
        ch = (ch * result) % n;
      }
      cipherText.append(ch + " ");
    }
    return cipherText.toString();
  }
  public String decrypt(String cipherText) {
    StringBuilder plainText = new StringBuilder();
    String[] cipherArray = cipherText.split(" ");

    for (int i = 0; i < cipherArray.length;i++ ) {
      long ch = (long)Integer.parseInt(cipherArray[i]);
      long result = ch;
      for (int j = 0; j < (d-1);j++) {
        ch = (ch * result) % n;

      }
      char decrypted = (char)ch;
      plainText.append(decrypted);
    }
    return "Decrypted Message: " + plainText.toString();
  }
  public static void main(String[] args) {
    EncryptSimpleRSA string = new EncryptSimpleRSA(17,61,53);
    System.out.println("Encrypted message: " + string.encrypt("This message contains ]['s ????'s and other goodies!!!!"));
    System.out.println(string.decrypt(string.encrypt("This message contains ]['s ????'s and other goodies!!!!")));
    System.out.println(string.getPrivateKey());
    System.out.println(string.getPublicKey());
  }
}
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.