Hey guys,

Just needing your help on this code.

#include <stdio.h>

int main() {
	FILE *fin,*fout;
	char c;
 
	fin=fopen("input.img","rb");
	fout=fopen("output.img","wb");

	while ((c=fgetc(fin))!= EOF) {
		fputc(c, fout);
	}
	return 0;
	fclose(fin);
	fclose(fout);
}

I'm trying to unscramble a scrambled image and this just outputs nothing. Would you guys be able to help me? Thanks.

Here's part of what I need to do. I don't know how to reverse exclusive OR :

The next random number in the sequence is found from the current number using the following linear congruential generator

n=(n*106+1283)%6075;

where n is the current number. The first number in the sequence is 1 and so the sequence starts:

1,1389,2717,3760,4968,5441,904.....

The image is stored in a file called mystery.img which is exactly 40000 bytes long. Each byte gives the brightness of a point in the image. You can view the file using the program display.exe on the H: drive.

Each byte in the image file has been EXCLUSIVE ORed with the bottom 8 bits of the corresponding number in the sequence so that the image is lost in the randomness. This operation is reversible.

Edited 6 Years Ago by tots08: n/a

One obvious problem is that the program closes the files AFTER the program has ended <so it's not closing the files, at all>.

Your IDE MAY close the files for you, but that's your IDE, not your program.

I don't see anything wrong with the rest of the program.

Exclusive OR is like a light switch - one way and the data should be encrypted, do it again, and the data should be unencrypted, IIRC.

Yep: Google Exclusive XOR

Edited 6 Years Ago by Adak: n/a

Thank you. But the image (40000bytes) still doesn't come up when I execute the program. Do you think I'd need to add a few more codes?

Edited 6 Years Ago by tots08: n/a

First thing is to check that your program will copy a file without making any changes to it.

Take a jpg or other file and let your program copy it, - just copy not encrypt or decrypt it.

Then go to the console window (terminal), and type FC filename1 filename2, using the correct filenames, of course.

If Windows tells you "No differences encountered", then you know the file has been copied correctly.

You moved return 0 down to the last line of the program?

The program you posted does NOT decrypt anything - it just copies a file. If you need to reverse an exclusive OR, etc., then sure, you'll need more code.

;)

Yeah. I need to unscramble the scrambled image. Would you be able to give me an XOR code? :)

I don't have any XOR code for this, atm, but it's not hard to do that part of it. I might need to tinker with the congruential generator part of it.

Do you have any info on the file header? Should that be XOR'd also, or should only the data portion of the file be XOR'ed? If the file header is to be left alone, then I need to know how long the header is, in bytes, or it's end of header marker.

I would need a copy of the encoded image file so I could check it out with a hex editor for details, as needed. If you want help with that, load the image to Swoopshare, and PM me with the URL to the encrypted image file. Or just post the url in this thread.

The loop logic is pretty simple:

n=1
while(there is another byte to be read from the encrypted image file) {
  get that char from the file
  char = char XOR n  //use the ^ symbol for XOR
  write the char to the output file
  use the number generator equation to get the next n
}

You can use getc() and putc(), or fread() and fwrite(). Since you know the exact size of the file in bytes, you could use a for loop, but a while loop is much more flexible and robust.

And when you open the new output file with the display.exe program posted (link posted above), there's the image - buck teeth and all. ;)

* When * will cartoon characters demand good orthodontic care?? ;)

Edited 6 Years Ago by Adak: n/a

Quick question ... When you open a file why dont you check if the file has actually been opened or not ?

That was just a program outline, not to worry. The actual code does these checks.

I recommend this exercise for you Abhimanipal - it's quite interesting, imo.

I have the same problem in this program and I will be think full if you show me the answer for it ,,,it's drive me crazy

You have the mystery.img file in the same directory as your program? You're using the display.exe to view the output file, after you've decoded it?

Post your code, and I'll tell you what's up with it. I'm not going to post the solution code, until you've put some real work into this assignment.

Don't be lazy, this is a good assignment, I promise.

Hello ,,,,
yap man I get the same one ....
that my code
i use for instate of while ..it's more easy and fun but it doesn't work with me ...
for ( i=0; i<40000; i++ )
{
c = fgetc ( fin );
s = ab & n;
c = n ^ c;
n = ( n * 106 + 1283 ) % 6075;
printf ("\n%d",c);
fputc ( c, fout );

Did you initialize n to one? It's not in your code.

Also missing is the closing curly brace at the end of the for loop.

The for() loop CAN work, but it has to be done right. Also, the while() loop is much more flexible.

And PLEASE use [code] tags around your code, when you post it.

that my programs ,,,,
#include <stdio.h>
int main()
{
FILE *fin,*fout;

fin=fopen("mystery.img","r");
fout=fopen("output.img","w");
unsigned char c; // individual bytes//
int i; // for purposes//
int n;
int s;
int ab = 0x00ff;

n = 1;
for ( i=0; i<40000; i++ )
{
c = fgetc ( fin );
s = ab & n;
c = n ^ c;
n = ( n * 106 + 1283 ) % 6075;

printf ("\n%d",c);
fputc ( c, fout );
}
return 0;
}

I don't see any reason for the ab or s variables. You AND the char from the mystery file, with the n that is generated (or assigned as 1 on the first loop), and that's it.

You can see that s especially, is never used in your loop. n remains unchanged by

s= ab & n;

Now, two questions:

1) What happened to using code tags?
2) When will cartoon characters receive orthodontic care for a bad overbite? ;)

thnks man I have done it and send it ,,,,
yahhhhh

Edited 6 Years Ago by devel2000: n/a

Haha! It's funny how we have the same assignment Devel! You must be in my comsci class. :)

This article has been dead for over six months. Start a new discussion instead.