I remember when I was starting out with encryption the examples were few and far between and those that I did find were just source code that wasn't well commented. I seek to change that.
The truth is encryption is easy once you break into it. NOTE: All of these algorithms can be applied to any language but the examples will be in Visual Basic.
The most important part of developing an encryption algorithm is to write it down as you go so that decrypting and undoing what was done will be much easier. I write it down on a notepad in regular English so I don't have to interpret code when coming up with the decryption method.
The first thing you have to realize about text is that each letter has a number code to go with it called ASCII code. This is a very important part of encryption. When you change a letter or character to this ASCII code it becomes a 3-digit number that you can work with. When it is in this number state you can perform mathematical functions on it which is how you change the data to an unreadable form that is reversible.
This is an example of a algorithm for changing the characters to ASCII and performing mathematical functions on them:
First Character -> Encrypt Function -> ASCII Code
ASCII Code - 13 -> New ASCII Code -> Back to Character Format
Now the first character is something different
To get back the original:
First Character -> Decrypt Function -> ASCII Code
ASCII Code + 13 -> New ASCII Code -> Back to Character Format
Now the first character is back to the original
Sound simple? That's because it is. No encryption would be written like this if you had something important to hide because it could be easily cracked. But it is a good place to start. Here is the Visual Basic source code to the same algorithm:
text1.text = Encrypt(character)
Private function Encrypt(char as string)
dim newASCII as integer
dim newChar as string
newASCII = asc(char) 'Convert to ASCII
newASCII = newASCII - 13 'Subtract 13 from the ASCII
newChar = chr(newASCII) 'Convert the new ASCII into Character format
Encrypt = newChar 'Return the result to the line calling it
end function
There are three important things that need to be learned from this example . 1. Asc() is a function provided by Visual Basic and it is used to convert a single character into ASCII. 2. Chr() is another function provided by Visual Basic and it will convert ASCII code into character format. 3. The third thing to note is that wherever you can add randomness the more secure your algorithm will be. If you had made the number subtracted a random number and changed it for each character than your encryption would have been more secure but you have to store those numbers somewhere and that is what's called a key. Now you wouldn't want to make a random number for each character or your key would be huge.
Something else you should know is that you can convert to other formats too. These are Octohedral with Oct() and Hexadecimal with hex(). These functions can be useful in adding an extra level of security but they are for more advanced algorithms and pretty much beyond the scope of this article.
The next thing you need to be able to do is break up a string into one character chunks. I am not going to go through the encryption methods right now, because the important part is how to break up the string. Here is the English version:
String -> Get first Character -> Perform any math -> Replace original with updated character -> Go on to the next character and repeat until the end
This is something very important and I use it in every single one of my encryption algorithms. You have to be able to break up or parse the string if you have any hope of encrypting it. The Visual Basic translation would be:
Private function ParseandEncrypt(string as string)
dim count as integer 'Declare the variable for the loop
dim Char as string 'Declare the variable for the character
dim newString as string 'Declare the variable for the new string
dim newASCII as integer 'Declare the variable for the new ASCII code
for count = 1 to len(string) 'Loop from 1 to the length of the string
Char = mid(string,count,1) 'Start with the character at the 'count' position and go one unit
newASCII = asc(Char)+(rnd*30) 'Add the ASCII number to a random number 0-30
newString = newString & chr(newASCII) 'Convert to regular character format and add to the new string
next count 'Loop again
ParseandEncrypt = newString 'Return the new string
End Function
That's where keys come in. Keys are used to store the numbers used in the mathematical functions. You can store the keys anyway you choose but I prefer arrays as it offers they easiest way for creation and implementation. The easiest thing to do is to store the numbers in the array before doing the math. I am going to show you how to do it in VB it should be almost identical in every other language so here it is:
dim arrayKey(128) as integer
for x = 0 to 127
arrayKey(x) = (rnd*9)
next x
Private function ParseandEncrypt(string as string)
dim count as integer 'Declare the variable for the loop
dim Char as string 'Declare the variable for the character
dim newString as string 'Declare the variable for the new string
dim newASCII as integer 'Declare the variable for the new ASCII code
for y = 0 to 127
arrayKey(x) = (rnd*9)
next y
for count = 1 to len(string) 'Loop from 1 to the length of the string
Char = mid(string,count,1) 'Start with the character at the 'count' position and go one unit
newASCII = asc(Char)+arrayKey(x) 'Add the ASCII number to a random number 0-30
newString = newString & chr(newASCII) 'Convert to regular character format and add to the new string
x = x + 1
if x > 127 then x = 0
next count 'Loop again
ParseandEncrypt = newString 'Return the new string
End Function
Private function ParseandDecrypt(string as string, key as integer)
dim count as integer 'Declare the variable for the loop
dim Char as string 'Declare the variable for the character
dim newString as string 'Declare the variable for the new string
dim newASCII as integer 'Declare the variable for the new ASCII code
for count = 1 to len(string) 'Loop from 1 to the length of the string
Char = mid(string,count,1) 'Start with the character at the 'count' position and go one unit
newASCII = asc(Char)-key(x) 'Add the ASCII number to a random number 0-30
newString = newString & chr(newASCII) 'Convert to regular character format and add to the new string
x = x + 1
if x > 127 then x = 0
next count 'Loop again
ParseandDecrypt = newString 'Return the new string
End Function
text1.text = ParseandDecrypt(text1.text, arrayKey)
Feel free to email me if you have any questions or comments my email is [email="newrepsoft@aol.com"]NewRepSoft@aol.com[/email] and I look forward to recieving feedback. I will try to help any body that asks for help. Thank you and I hope you found this article useful.