Introduction
This article shows a classical approach to lock a file so that it doesn't
reveal its original content in any software. For locking a file, a password is
required. To view the file again in its original form, it has to be unlocked
with the LOCK software with the same password which was used to lock the file.
Background
The basic approach used is the symmetric key cryptography. The file is
converted into blocks of 8 bytes and repeatedly XORed with the similar block of
8 bytes obtained by the password. Because the encryption used is symmetric key
cryptography, the same process is used for unlocking\decryption.
Using the code
Following is the code for main encryption routine. For more details, look at
source code for the comments.
int count = (1024 + keyBytes.Length-1)/keyBytes.Length;
for( int i=0 ; i<count ; i++)
Array.Copy( keyBytes , 0 , keybuf , i*keyBytes.Length , keyBytes.Length);
long lBytesRemaining = stream.Length;
while (lBytesRemaining>0)
{
long lPosition = stream.Position ;
int nBytesRequested = (int)System.Math.Min(bufSize , lBytesRemaining);
int nBytesRead = reader.Read (buffer, 0, nBytesRequested)
for (int i=0; i<nBytesRead; i++)
buffer[i] ^= keybuf[i];Stream.Seek (lPosition, SeekOrigin.Begin);
writer.Write (buffer, 0, nBytesRead);
lBytesRemaining -= nBytesRead;
}
Screenshots
Points of Interest
This
is my second article at code project. For the first article which about
Handling the Big Numbers, click here. I
donot have much experience of article writing. I have just tried to minimize
the shortcomings I have faced drafting the First article. Suggestion regarding
the clarity, confusion, code, technical writing are welcomed. Comments are also
welcomed. You can also mail me at funatlearn@ontheedge.co.in
with subject Lock Code Project Article regarding the same.
History
This is the first version of the program. I have some inputs for the next
version of the code. I am expecting some more inputs from the readers of the
code. After finalizing all the inputs I would write next version of the code.
If you feel any shortcoming of this code/article, please leave a message
about the same, so that these pitfalls may not appear in the subsequent
versions.