Hello, i want to ask, messing words in array is a good way to encrypt data? or can someone very easy to brake it

here is an example

#include "stdafx.h"
#include <fstream>

using namespace std;

// Decleration for functions
void EncryptFile(char c[], int key1, int key2);
void DecryptFile(string filename, int key1, int key2);

char NewMessage[80];

int _tmain(int argc, _TCHAR* argv[])
{
	// Array to store message
	char Message[80];

	// Prompt user type something
	printf("Write a short story about you, less than 80 words.\n");
	
	// Get wrods
	gets(Message);

	// Encrypt & Decrypt message
	EncryptFile(Message, 1, 1);
	DecryptFile("passwords.key", 1,1);

	return 0;
}

void EncryptFile(char c[], int key1, int key2)
{
	// Write Encrypted message to file
	ofstream myf;
	myf.open("passwords.key");

	for(int i=0; c[i]; i++) {
		c[i] =  (c[i] + key1) * key2;
		myf << c[i];
		printf("%c", c[i]);
	}

	printf("\n");
	myf.flush();
	myf.close();
}

void DecryptFile(string filename, int key1, int key2)
{
   // Open Message from file
   ifstream  myf;
   myf.open(filename);

   while(!myf.eof())
		myf >> NewMessage;

   for (int i=0; NewMessage[i]; i++) {
	   NewMessage[i] = (NewMessage[i] - key1) / key2;
	   printf("%c", NewMessage[i]);
   }
   printf("\n");
   
}

this part can be better, using a strong mathematic function, but for now i have this

NewMessage[i] = (NewMessage[i] - key1) / key2;

Using this

// Encrypt & Decrypt message
EncryptFile(Message, 1, 1);
DecryptFile("passwords.key", 1,1);

The encrypted message of abc is bcd because we add 1*1 = 1 in asii code ;p so it goes to next ;p

Recommended Answers

All 3 Replies

Forget about whether it's strong encryption or not. I don't see how it can work at all due to two factors...

1) rollover.
2) integer division.
2) Non-prime numbers

1 and 1 are trivial, so these won't come up, but what about a character like '@', which is 64 in ASCII, and my multiply key is 2, so I multiply 64 times 2 and get 128, which is 0x80, but since a char is only 8 bits, it's actually -128, so then I divide by 2 and get -64, not 64. Plus you aren't taking into account non-printable characters and white space, so the >> operator isn't going to work. What if something encrypts into a tab? I think when you start trying keys other than 1, you're going to have a lot of problems with this method.

ASCII Table supprot 256 characters, my queston is...

Adding to each code + 1000example
A=64? if we add to 64 +1000 will go 1064, there is no any ASCII code 1064

Yea ok, ofc this is not method to encrypt data, but i say "messing data"-"hiding data"

The ASCII table supports 0 to 127, not all of which are printable. The point is that once you get multiplication "keys" other than 1, the whole system will fail. You have two keys, the multiplication key and the adding key. Any letter will go beyond 127 and into negative numbers with a multiplication key greater than one, which means it won't "decrypt" correctly. Try your program with some other keys and see if it works. The method CAN work, but you need to store the characters as integers, not characters, to prevent this overflow problem.

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.