Hi all,
I have implemented some code for simple columnar encryption. The algorithm is encrypting properly as I want. But during decryption it's giving me some errors. Please go through and suggest me if any idea

public static void encrypt(RandomAccessFile f,RandomAccessFile d,int columns) throws Exception
	{
		d.seek(10);           // SEEK IS USED FOR INTERNAL PURPOSE
		for(int i=0;i<columns && flag;i++)
			for(int j=0;(j*columns+i)<f.length() && flag;j++)
			{
				f.seek(j*columns+i);
				p.setValue((int)d.getFilePointer());			
				d.write(f.read());
			}
	}

//DECRYPTION FUNCTION

	public static void decrypt(RandomAccessFile f,RandomAccessFile d,int columns) throws Exception
	{
		f.seek(10);
		int rows;
		long temp;
		int cache=(int)((f.length()-10)%columns);
		int buffer=(int)((f.length()-10)/columns);
		
		if(cache==0)
			rows=buffer;
		else
			rows=(buffer+1);

		for(int i=0;i<rows && flag;i++)
		{
			temp=i;
			for(int j=0;j<columns && flag;j++)
			{
				f.seek(temp+10);
				p.setValue((int)d.getFilePointer());
				d.write(f.read());
				temp+=buffer;

				if(j<cache)
					temp++;
			}
		}
	}

Please help me out!! thanks in advance!

sorry forgot to give example
Suppose we have data abcdef and user inputs no of colunns as 2 then our data would be
a b
c d
e f
And encrypted would be acebdf please help me out if you understand problem. F.seek(10) is used for internal purpose to save files extension

I'm not sure how your algorithm in your code work... You tends to manipulate file content instead of read the whole thing, work with it, then write out the whole thing. It would be much easier to deal with the whole string all at once because you will iterate through the string only once O(n) to get the encryption done. The way you are doing is odd...

I will give you an example!
ORIGINAL FILE CONTENT

String q,z;
q=z="";


q+=temp;
z+=temp;

System.out.println("b4 Encryption Q value\n"+q+"\n");
System.out.println("afr encryption Z value\n"+z+"\n");

String q,z;
q=z="";

q+=temp;
z+=temp;

System.out.println("b4 Decryption Q value\n"+q+"\n");
System.out.println("afr decryption value\n"+z+"\n");

ENCRYPTED FILE with 4 as KEY (4 columns)

.txt     Sn,
=

=pze
Seurlbnpnve+\;ymtinfnpnve+\;
iq
z;
t;+m
ymtin4ct a\qn
s..n(rct u"")tgzq"

t;+m
ymtin4ct a\qn
s..n(rct a\zn
Sn,
=
qe
=p
s..n( riQln+"
topt" rive+\;r ;="
qe
=p
s..n( riQln+"
topt" riZln+"
tgzq"
+m
t;
topt"Dyo u"")Seurladyoa\zniq
z;
+m
t;
topt"Eyo u"")Seurlaeyo u"")
r ;="
=pze
Seurlbepnve+\;ymtinfepnln+"

After decryption

String q,z;
q=z="";


q+=temp;
z+=temp;

System.out.println("b4 Encryption Q value\n"+q+"\n");
System.out.println("afr encryption Z value\n"+z+"\n");

String q,z;
q=z="";

q+=temp;
z+=temp;

System.out.println("b4 Decryption Q value\n"+q+"\n");
System.out.println("afr decryption value\n"+z+"\n");iÿ

Just check out decrypted content it's attaching some garbage value.

Edited 5 Years Ago by mandy011: n/a

I was trying it other way .. I read the bytes from file and concatenated to a string , after that i did convert that string to byte array and tried to write bytes by
f.write() as well as f.writeByte() but both the things are writting only INTEGER values. Please what to do help , it's very urgent!

I solved this problem by setting the decrypted file length to encrypted file length-10. It was attaching some garbage characters at the end after decryption. But problem is solved . Thanks :)
Mods can close this thread!

This question has already been answered. Start a new discussion instead.