Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Cipher3D

my XOR encryption/breaking-analyzer program..erg...

This topic is 5315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i''m trying to learn about cryptography, and to start off, i''m writing my own simple XOR encryption program, along with an analyzer that uses the indexes of coincidence to somehow find out the plaintext. the problem is, whenever i write to a file, it always appends something at the end that I did write to. so i''ll include my encryption algorithm, then my analyzer:



int encrypt()
{
	FILE* fin,*fout;

	cout << "Enter the filename of the plaintext: " << endl;

	char buffer[80];

	cin >> buffer;

	if ((fin = fopen(buffer,"rb")) == NULL)
		return (1);

	cout << "Enter the filename of the output ciphertext: " << endl;

	cin >> buffer;

	if ((fout = fopen(buffer,"wb")) == NULL)
		return (1);

	cout << "Enter the key: " << endl;

	cin >> buffer;
	char* key = buffer;

	int temporary;
	
	fscanf(fin,"%d",&temporary);
	fprintf(fout,"%d\n",temporary);

	while (!feof(fin) && temporary != 0)
	{
		temporary--;
		int c;

		if (!key)
		{
			key = buffer;
		} //ebnd


		c = fgetc(fin);

		c ^= *(key++);

		fputc(c,fout);
	}

	fclose(fout);
	fclose(fin);

	return (0);
}
and my analyzer
int analyze()
{
	
	//break a xor

	FILE* fcipher;

	cout << "Enter filename: ";

	char buffer[80];

	cin >> buffer;

	fcipher = fopen(buffer,"rb");

	if (!fcipher)
		return (1);


	//read in the ciphertext

	int ciphertextlen = 0;
	int c = 0;
	int d = 0;
	
	fscanf(fcipher,"%d",&ciphertextlen);


	while (!feof(fcipher))
	{
		c = fgetc(fcipher);
		buffer[d] = c;
		d++;
	} //end while



	fclose(fcipher);


	float coincidences[20];

	//analyze

	for (int keylen = 1;keylen<20;keylen++)
	{
		int coincidence_count = 0;

		for (int index=0;index<ciphertextlen;index++)
		{

			int j = buffer[index] ^ buffer[(index+keylen)%ciphertextlen];

			if (j == 0)
				coincidence_count++;
		

		}

		coincidences[keylen] = coincidence_count/(float)ciphertextlen;

	}

	cout << "Cipher text length: " << ciphertextlen << endl;

	cout << "Ciphertext: " << buffer << endl;
	cout << "Keylength          Coincidence Percentage" << endl;
	cout << "-----------------------------------------" << endl;
	
	for (int index=1;index<20;index++)
	{
		cout << index << "          " << coincidences[index] << endl;
	} //end for


	int coincidence_index = 999;

	for (index=1;index<20;index++)
	{
		if (coincidences[index] > 0.06f)
		{
			coincidence_index = index;
			break;
		}
	}


	
	for (index=0;index<ciphertextlen;index++)
	{
		buffer[index] ^= buffer[(index+coincidence_index)%ciphertextlen];
	} //end for


	cout << "Ciphertext with removed key: " << buffer << endl;



	return (0);
}
what do i do after the "ciphertext with removed key"? Any ideas? Thanks! Cipher3D

Share this post


Link to post
Share on other sites
Advertisement
ok then, new question:

if i write to a file, say:

fprintf(fp,"blah");

while will it append extra stuff at the end of the file?

for example,

while (!feof(fp))
{
stuff[c++] = fgetc(fp);
}

, and stuff will yield more than blah? It will yield, "blah" plus some strange ASCII characters, like heart shapes and y''s with two dots above them.

Share this post


Link to post
Share on other sites
You need to terminate the string with a 0. Both for cout and for fprintf().


--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!