I am practicing writing a simple (not practical) encryption algorithm. I do not know how to write a 'good' algorithm, but I have one that seems to work on simple text files. Anybody that knows what they're looking for could probably recover the plaintext in minutes... I understand that this is not really 'secure' by any means.
The encryption algorithm:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
while ( !iFile.eof() )
{
if ( k >= strlen( finalKey ) )
k = 0;
int byte = iFile.get();
if ( byte == 0 )
cout << byte << " ";
if ( iFile.fail() )
return;
int newByte = byte;
if ( newByte != 0 )
newByte = byte + finalKey[k];
oFile.put( newByte );
k++;
}
|
The decryption algorithm is the same, except the + is a minus (silly I know).
Note that finalKey[] is just a character array of the user has entered ('key' for example), and is not 'hashed' or 'salted'. (Though I'd be interested in learning how this works)
I have a problem when using this on .docx (microsoft word files), since some of the bytes are zeroes. The result is that the ciphertext displays the key in place of the 0 bytes... (0 + anything = anything), and in this case, the anything is my key.
I have tried to cirumvent this problem by not changing the byte if the original byte is 0, but this causes the file to be encrtypted/decrypted incorrectly.
What am I doing wrong? (Other than a terribly insecure/impractical algorithm).
Although if anyone knows about encryption/has any comments on how I could improve this algorithm, I certainly look forward to advice. I am trying to learn about encryption - any advice that could make this more secure is welcome.
Thanks for reading such a long post...