I have written a program in java for RSA algorithm to encrypt a string and decrypt on receiver side. RSA public key and n value are announced to receiver by sender. Then encryption process begins. The RSA program in sender side returns BigInteger which is the encrypted string and I converted it to byte array.And sent to the receiver through datagram socket.On the receiver side the byte array is converted into BigInteger and decryption is done.This is the logic behind the program and it worked well.

I just changed the program such that, instead of using datagram, i wrote the encrypted byte array in sender to a file as bytes.On the receiver side I read it from the file as bytes and done the decryption.Now the problem is decryption failed while attempting like this.

Please help me to solve this problem.Thanks in advance.

Can't do much without seeing the code....

//This is the sender program.RSA program works well here and it had been written as a seperate class.ds is an object to a class which is for datagram communications.

public static void main(String args[])throws Exception
	{
	datasend ds=new datasend(5000,"rlaknar-desktop");
	RSA rsa=new RSA(200);
	BigInteger privateKey=rsa.getprivatekey();	
 	BigInteger publicKey=rsa.getpublickey();
	byte[] pub=publicKey.toByteArray();
	int lenpub=pub.length;
	byte[] lengpub=intToByteArray(lenpub);
	ds.SendPacket1(lengpub);//Send length of byte array
	ds.SendPacket1(pub);//Send packet of public key
	System.out.println("publicKey  "+publicKey);
	BigInteger modulus=rsa.getmodulus();
	byte[] mod=modulus.toByteArray();
	int lenmod=mod.length;
	byte[] leng=intToByteArray(lenmod);
	ds.SendPacket1(leng);//Send length of byte array
	ds.SendPacket1(mod);//Send packet of modulus
	System.out.println("modulus  "+modulus);
	String hash=SHA.sha();
	System.out.println(hash);
	String s3="",s4,s5;
	int l=hash.length();
	String s1=hash.substring(0,l/2);
	String s2=hash.substring(l/2);
//	StringBuffer sb=new StringBuffer(l);
	byte[] by1=rsa.RSAencryption1(s1,privateKey,modulus);
	int length1=by1.length;
	byte[] length11=intToByteArray(length1);
	ds.SendPacket1(length11);//
	byte[] by2=rsa.RSAencryption2(s2,privateKey,modulus);
	int length2=by2.length;
	byte[] length21=intToByteArray(length2);
	ds.SendPacket1(length21);//
FileOutputStream outb1=new FileOutputStream("verification1.pgp");
	FileOutputStream outb2=new FileOutputStream("verification2.pgp");
	FileOutputStream out=new FileOutputStream("verification.pgp");
	outb1.write(by1,0,length1);
	outb2.write(by2,0,length2);
	outb1.close();
	outb2.close();
FileInputStream inby1=new FileInputStream("verification1.pgp");
FileInputStream inby2=new FileInputStream("verification2.pgp");
inby1.read(by1,0,length1);
inby2.read(by2,0,length2);
	String dec1=rsa.RSAdecryption1(by1,publicKey,modulus);
	System.out.println("Dec1 "+dec1);
	String dec2=rsa.RSAdecryption2(by2,publicKey,modulus);
	System.out.println("Dec2 "+dec2);
	StringBuffer sb=new StringBuffer((length1+length2)*2);
	String dec=sb.append(dec1).append(dec2).toString();
	System.out.println("Dec "+dec);
	byte[] msg=new byte[length1+length2];
	for(int p=0;p<length1;p++)
	msg[p]=by1[p];
	int q=0;
	for(int p=length1;p<length1+length2;p++){
	msg[p]=by2[q];
	q++;
	}
out.write(msg,0,msg.length);
	out.close();
	}


}//class






//Receiver program
public static void main(String args[])throws Exception
{
datareceive dr=new datareceive(5000);
RSA1 rsa=new RSA1();
byte[] lengthpub=dr.ReceivePacket();
int lenpub=byteArrayToInt(lengthpub);
byte[] pub=dr.ReceivePacket1(lenpub);
BigInteger publicKey=new BigInteger(pub);
System.out.println("publicKey  "+publicKey);
byte[] lengthmod=dr.ReceivePacket();
int lenmod=byteArrayToInt(lengthmod);
byte[] mod=dr.ReceivePacket1(lenmod);
BigInteger modulus=new BigInteger(mod);
System.out.println("modulus  "+modulus);
byte[] length11=dr.ReceivePacket();
int length1=byteArrayToInt(length11);
byte[] length21=dr.ReceivePacket();
int length2=byteArrayToInt(length21);
int lengthtotal=length1+length2;
byte[] by1=new byte[length1];
byte[] by2=new byte[length2];
FileInputStream inby1=new FileInputStream("verification1.pgp");
FileInputStream inby2=new FileInputStream("verification2.pgp");
inby1.read(by1,0,length1);
inby2.read(by2,0,length2);
String dec1=rsa.RSAdecryption1(by1,publicKey,modulus);
System.out.println("Dec1 "+dec1);
String dec2=rsa.RSAdecryption2(by2,publicKey,modulus);
System.out.println("Dec2 "+dec2);
StringBuffer sb=new StringBuffer(lengthtotal*2);
String dec=sb.append(dec1).append(dec2).toString();
System.out.println("Dec "+dec);
}


}//class





//Ouput shown on sender side
data send started in port 5000
Data send openedjava.net.DatagramSocket@83cc67
Data send openedjava.net.DatagramSocket@e09713
publicKey  65537
Data send openedjava.net.DatagramSocket@de6f34
Data send openedjava.net.DatagramSocket@156ee8e
modulus  596947940769961701206482460397562027930416486106774347835627
637d57588b05292deefbf0131abb0c54ed22b8e2
Data send openedjava.net.DatagramSocket@10d448
Data send openedjava.net.DatagramSocket@e0e1c6
Dec1 637d57588b05292deefb
Dec2 f0131abb0c54ed22b8e2
Dec 637d57588b05292deefbf0131abb0c54ed22b8e2

 //Output shown on receiver side
publicKey  65537
modulus  596947940769961701206482460397562027930416486106774347835627
Dec1 �:#Hi>�$-gױ@�dI�/'�b�
Dec2 G@��j3JuY��"V���
Dec �:#Hi>�$-gױ@�dI�/'�b�G@��j3JuY��"V���

I am sure the decryption worked well when I attempted to send the byte array as datagram packets.Then, the output of the receiver was
Dec 637d57588b05292deefbf0131abb0c54ed22b8e2
for the same input.
Please help me.Thanks.

Post your code inside code tags.

[code=java] // code here

[/code]

//This is the sender program.RSA program works well here and it had been written as a seperate class.ds is an object to a class which is for datagram communications.

public static void main(String args[])throws Exception
	{
	datasend ds=new datasend(5000,"rlaknar-desktop");
	RSA rsa=new RSA(200);
	BigInteger privateKey=rsa.getprivatekey();	
 	BigInteger publicKey=rsa.getpublickey();
	byte[] pub=publicKey.toByteArray();
	int lenpub=pub.length;
	byte[] lengpub=intToByteArray(lenpub);
	ds.SendPacket1(lengpub);//Send length of byte array
	ds.SendPacket1(pub);//Send packet of public key
	System.out.println("publicKey  "+publicKey);
	BigInteger modulus=rsa.getmodulus();
	byte[] mod=modulus.toByteArray();
	int lenmod=mod.length;
	byte[] leng=intToByteArray(lenmod);
	ds.SendPacket1(leng);//Send length of byte array
	ds.SendPacket1(mod);//Send packet of modulus
	System.out.println("modulus  "+modulus);
	String hash=SHA.sha();
	System.out.println(hash);
	String s3="",s4,s5;
	int l=hash.length();
	String s1=hash.substring(0,l/2);
	String s2=hash.substring(l/2);
//	StringBuffer sb=new StringBuffer(l);
	byte[] by1=rsa.RSAencryption1(s1,privateKey,modulus);
	int length1=by1.length;
	byte[] length11=intToByteArray(length1);
	ds.SendPacket1(length11);//
	byte[] by2=rsa.RSAencryption2(s2,privateKey,modulus);
	int length2=by2.length;
	byte[] length21=intToByteArray(length2);
	ds.SendPacket1(length21);//
FileOutputStream outb1=new FileOutputStream("verification1.pgp");
	FileOutputStream outb2=new FileOutputStream("verification2.pgp");
	FileOutputStream out=new FileOutputStream("verification.pgp");
	outb1.write(by1,0,length1);
	outb2.write(by2,0,length2);
	outb1.close();
	outb2.close();
FileInputStream inby1=new FileInputStream("verification1.pgp");
FileInputStream inby2=new FileInputStream("verification2.pgp");
inby1.read(by1,0,length1);
inby2.read(by2,0,length2);
	String dec1=rsa.RSAdecryption1(by1,publicKey,modulus);
	System.out.println("Dec1 "+dec1);
	String dec2=rsa.RSAdecryption2(by2,publicKey,modulus);
	System.out.println("Dec2 "+dec2);
	StringBuffer sb=new StringBuffer((length1+length2)*2);
	String dec=sb.append(dec1).append(dec2).toString();
	System.out.println("Dec "+dec);
	byte[] msg=new byte[length1+length2];
	for(int p=0;p<length1;p++)
	msg[p]=by1[p];
	int q=0;
	for(int p=length1;p<length1+length2;p++){
	msg[p]=by2[q];
	q++;
	}
out.write(msg,0,msg.length);
	out.close();
	}


}//class

//Receiver program
public static void main(String args[])throws Exception
{
datareceive dr=new datareceive(5000);
RSA1 rsa=new RSA1();
byte[] lengthpub=dr.ReceivePacket();
int lenpub=byteArrayToInt(lengthpub);
byte[] pub=dr.ReceivePacket1(lenpub);
BigInteger publicKey=new BigInteger(pub);
System.out.println("publicKey  "+publicKey);
byte[] lengthmod=dr.ReceivePacket();
int lenmod=byteArrayToInt(lengthmod);
byte[] mod=dr.ReceivePacket1(lenmod);
BigInteger modulus=new BigInteger(mod);
System.out.println("modulus  "+modulus);
byte[] length11=dr.ReceivePacket();
int length1=byteArrayToInt(length11);
byte[] length21=dr.ReceivePacket();
int length2=byteArrayToInt(length21);
int lengthtotal=length1+length2;
byte[] by1=new byte[length1];
byte[] by2=new byte[length2];
FileInputStream inby1=new FileInputStream("verification1.pgp");
FileInputStream inby2=new FileInputStream("verification2.pgp");
inby1.read(by1,0,length1);
inby2.read(by2,0,length2);
String dec1=rsa.RSAdecryption1(by1,publicKey,modulus);
System.out.println("Dec1 "+dec1);
String dec2=rsa.RSAdecryption2(by2,publicKey,modulus);
System.out.println("Dec2 "+dec2);
StringBuffer sb=new StringBuffer(lengthtotal*2);
String dec=sb.append(dec1).append(dec2).toString();
System.out.println("Dec "+dec);
}
}//class

//Ouput shown on sender side
data send started in port 5000
Data send openedjava.net.DatagramSocket@83cc67
Data send openedjava.net.DatagramSocket@e09713
publicKey 65537
Data send openedjava.net.DatagramSocket@de6f34
Data send openedjava.net.DatagramSocket@156ee8e
modulus 596947940769961701206482460397562027930416486106774347835627
637d57588b05292deefbf0131abb0c54ed22b8e2
Data send openedjava.net.DatagramSocket@10d448
Data send openedjava.net.DatagramSocket@e0e1c6
Dec1 637d57588b05292deefb
Dec2 f0131abb0c54ed22b8e2
Dec 637d57588b05292deefbf0131abb0c54ed22b8e2

//Output shown on receiver side
publicKey 65537
modulus 596947940769961701206482460397562027930416486106774347835627
Dec1 �:#Hi>�$-gױ@�dI�/'�b�
Dec2 G@��j3JuY��"V���
Dec �:#Hi>�$-gױ@�dI�/'�b�G@��j3JuY��"V���


I am sure the decryption worked well when I attempted to send the byte array as datagram packets.Then, the output of the receiver was
Dec 637d57588b05292deefbf0131abb0c54ed22b8e2
for the same input.
Please help me.Thanks.

Is this format right?

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.