Tuesday, July 28, 2009

C++ EXPERTS? Need some advice? SIMPLE ENCRYPTION?

Hi I have this code to send a .txt file between 2 computers, I want to know how can I add simple encryption to it? say maybe where it moves one letter across?? e.g. A turns into a B etc??





#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;





#define CNTRLZ 0x1A





char sourceFileName[] = "H:\\text.txt";


char commStreamName[] = "COM1";


FILE *commStream;


FILE *SouceFile;


char charToSend;


char ReadAccess[] = "r";


char WriteAccess[] = "w";


char errorMsg[] = "fail to open data file\n";


char fileopenflag[] = "r";


char EndOfFile[] = "EOF";





int main(void) {


FILE *fp;


FILE *dp;


int c;





_asm {





// Opens COM port





lea eax, WriteAccess // Sets the read flag for the COM port


push eax


lea eax, commStreamName


push eax


call fopen


add esp, 8





mov dp, eax


cmp eax, 0 // Checks if COM port is opened


jne carryon





lea eax, errorMsg


push eax // Error Handling


call printf


add esp, 4





mov eax, 1


jp done

C++ EXPERTS? Need some advice? SIMPLE ENCRYPTION?
Well for starters that isn't C++, it's C with some assembly in there. Actually, it's mostly assembly and it looks like x86 (or perhaps an x86 predecessor) assembly. What is this for? If it is for the x86, it must be for some old hardware as I don't know anyone who programs for that platform with assembly anymore. Also, I'm sorry, but I'm not going to try to figure out what is going on in the code because I find interpreting assembly to be rather tedious, so I'll leave that up to you.





Anyway, one simple type of encryption is to XOR a message string with an encryption string. Now when I talk about the message string or encryption string, I'm talking about a string of bits, as in 1's and 0's, and not characters. Remember, a character is generally defined by 1 byte, or 8 bits.





XOR is a basic logic operation that is defined as follows:


0 xor 0 = 0


0 xor 1 = 1


1 xor 0 = 1


1 xor 1 = 0





So first you want to come up with an encryption string to act as a shared key. This can be any length you want, but longer is usually better and a multiple of 8 would probably be the best. We'll call this length n.





Then perform a bit wise XOR between the encryption string and the first n bits of the message to produce an encrypted result. Repeat this with each n bit long section of the message until the entire message is encrypted.





To decrypt the message at the receiver, XOR the encrypted message with the encryption string just as you did to encrypt it and you will again have the original message.





Here's an example of how this works. Say your message is 10010111 and the encryption string is 10101001, so encrypting goes like this:





10010111 -%26gt; message


10101001 -%26gt; key


--------------- XOR


00111110 -%26gt; this is your encrypted message





And decryption goes like this:





00111110 -%26gt; encrypted message


10101001 -%26gt; key


--------------- XOR


10010111 -%26gt; back at your original message





Now, this is not a secure encryption method by any stretch of the imagination; someone who is determined could easily crack it given some time. It is simple, easy to implement, and will stop most people from seeing the text. To make it better, you should also implement a way to make the encryption string change over time, such as making it increment by a certain value after every block you encrypt/decrypt.





Also, the alignment of the message is key; if it gets shifted by a bit in either direction, it will not line up with the encryption string and you won't be able to decrypt it. Given that this is going to send the data over a serial connection or similar, you can't assume data will always reach the receiver exactly how it was sent. Because of this, you will also need to implement a method of ensuring the data arrived correctly. A proper packetization method with a checksum would work fine, and may already be implemented in your program.
Reply:Try a vernam encryption.





You take a phrase of whatever legnth you want, convert the characters into an integer (assuming you use only alphabetic characters), add them serially to each character of a text message, modulo the range (e.g. 26).





What comes out is a fully encrypted message based on your phrase. To decrypt the message you would subtract each each character of the phrase from the message over the same range. If you do it right you will be left with your original message.





e.g.





Phrase: dad (4-1-4)


Message: hello (8-5-12-12-15)


Encrypted: lfppp (12-6-16-16-16)





Vernam encryption is one of the most difficult encryptions to break.


No comments:

Post a Comment