Encryption & Decryption in Turbo Pascal


I have developed two forms of cryptography, know I must be careful about what I say and what I give out over the Internet. Cryptography is an essential and very important part of security. The United States themselves ban the release of certain cryptographic methods even to leave the country. If you know a cryptographic method that someone uses, you can decrypt what ever that person has sort to secure.


As I said I myself have come up with two cryptographic methods, one of which I believe is practically unhackable, even by myself who actually knows the encryption algorithm!(No release of this algorithm is going to occur under any circumstance!!). The other however, although still relatively hard to crack (time wise), I found is similar to encryption methods that many people use around the world. It is pretty simple, but then again it is not an 'ABC' method.

The Method.

The method is relatively simple, as I said before. The method that I am going to teach you consists of a single 128 character key, 2 seeds, and a 6 byte file header (optional).

[Click Here To Download The Example Archive - CRYPT.ZIP]

Below I am going to illustrate the algorithm to encrypt in pseudo-code


 Open source file and check header (use BLOCKREAD and BLOCKWRITE)
IF header IS PRESENT do not allow encryption again
Create destination file
Write header information into new file

Seed Generation
The seed generation is mainly dependant on the "password" (key) the user specifies.

Initialise the two seeds(bytes) to zero
Take another byte and make it the length of the key
seed1 = seed1 + (
ORD( key[ LOOP ] ))* LOOP
seed2 = seed2 + (
ORD( key[ LOOP ] ))* length of key
DEC( length of key )

The reason why the length is put as the seed is so that the key generated can not be of a multiple value. There can only be one key not three or four.

END of Seed Generation

Write seeds into the file (This might seem mad but it allows the decryption program to check if the 'password' is correct)
Read a block from the source file to the buffer array
WHILE amount read in >0 DO
LOOP 1 TO (amount read in)
seed1 = seed1 - LOOP seed2 = seed2 + LOOP
IF odd(LOOP) THEN buffer[LOOP] = buffer[LOOP]-seed1
ELSE buffer[LOOP] = buffer[LOOP]+seed2

Now have all sorts of (NOT, AND, XOR etc etc) in the loop. This will make the encryption method more personal, be careful though, you don't want a "LOOP" where your personal part turns the code back into just what it was originally

Heres an example...
buffer[LOOP] = 123 XOR buffer[LOOP] AND 5
buffer[LOOP] =
NOT buffer[LOOP]

  Write buffer to destination file
  Read next load from the source file to the buffer
  (bytes read) = (bytes read) + (amount read in)
END WHILE statement


Closing The File.
Now we need to close all the files down! Remember, we do not want a hacker to be able to retrieve the original file from the disk by undeleting!!

Rewrite the source file
Clean the buffer by filling it with zeros
Fill the source files with the zeros
Close the source file and destination file
Erase the source file
Rename the destination file to the original source file name


Basically the source file is rewritten to zeros so that it can not be found again on the storage media, and then the destination file is renamed to the same of that of the source so no 'undelete' can be made.

Below I have briefly described the algorithm to decrypt.


This is very similar to the encoding algorithm

The seed generation is the same and the decoding algorithm is that of encoding apart from the AND, XOR, NOT parts you put in come before the ODD seed generations, and they must be in a negative format of the encoding format.

The seed generation part of the algorithm should be opposite. e.g. Where ADD, SUB and visa versa.

Closing algorithm is the same

The opening is slightly different, generate the seeds from the users inputted key and check them against the ones stored in the file.

N.B You can use the 6 bytes of header to see if the file is encrypted or not. (You don't want to decrypt a file that has not been encrypted)



How about using this method, and trying to make it more "unhackable"?!?

  • No Header : Can't tell the encryption type.
  • Multiple Keys : Can encrypt the file several times with different keys can take several life times to crack!!
  • Multiple Seeds : Will not do too much to the encryption strength.
  • Lots more (AND, XOR, NOT, etc.) : In the algorithm adding more 'scramblers' will make the code more scrambled.
  • Do MAD things : Have the name of the file as a key and change the file name to something else! etc etc etc ...

Well that is pretty much it.. Don't be put off by the coding above, it is simpler than it really looks, honest!!
You are now another step further to becoming a master programmer!!!

Pascal Programming
Wed, 29 Jul 2009 14:50