Lately I have been reading some books about Cryptology and there I stumbled upon Caesars Cipher.
Caesars Cipher is a very simple form of Cryptation. Its method is to replace each plaintext letter with one a fixed number of places down the alphabet. The fixed number is the Key.
Ex. with a key 3 – the letter A will become D. B will become E etc.
So the word SECRET will be come VHFUHW.
This form for cryptology can be represented by the following formular:
c = p + k mod n
where c = cipher, p = plaintext, k = key, n = number of letters in the alphabet
Letters (p) will be represented as numbers, where a = 0, b = 1 etc.
If the cipher is greater than the number of letters in the alphabet, we have to start from the begin of the alphabet – this is solved by mod.
Ex. the letter K & letter Z:
K is the 10’te letter in the alphabet. We use the key 3 and there is 26 letters in the used alphabet.
c = p + k mod n = 10 + 3 mod 26 = 13, which gives us the letter N.
Z is the 26’te letter in the alphabet.
c = 26 + 3 mod 26 = 29 mod 26 = 3, which gives us the letter C.
Now that the theory is on place – we can look on some functions for Navision.
To Encrypt you can use this function:
Key := 3; NoLetters := 26; IF TextToCrypt = '' THEN EXIT; // Remove spaces ClearText := UPPERCASE(DELCHR(TextToCrypt,'=',' ')); // Encrypt letter by letter FOR i := 1 TO STRLEN(ClearText) DO BEGIN // Char to Int - where A = 0 ClearChar := ClearText[i] - 65; //Encrypt char - +65 to get a letter CryptChar := ((ClearChar + Key) MOD NoLetters) + 65; // Add char to crypt text CryptText += FORMAT(CryptChar); END; TextToCrypt := UPPERCASE(TextToCrypt);
Ok now you can encrypt – but what about decryption. Decryption is actually the same as encryption – all you have to do, is to use a different key 🙂
So which key to use? The key to encrypt was 3 and the number of letters was 26. To decrypt you have to deduct 3 letters. This gives us the decrypt key equals 23 (number of letters – key = 23).
Key := 3; NoLetters := 26; DecryptKey := NoLetters - Key; IF CryptText = '' THEN EXIT; //DeCrypt letter by letter FOR i := 1 TO STRLEN(CryptText) DO BEGIN //Char to Int - where A = 0 CryptChar := CryptText[i] - 65; //Encrypt char - +65 to get a letter ClearChar := ((CryptChar + DecryptKey) MOD NoLetters) + 65; // Add char to crypt text DeCryptText += FORMAT(ClearChar); END;
If you compare the two functions, you will see that the encryption and decryptions functions are pretty much the same – they only use different keys.
Now you can encrypt and decrypt with Caesars Cipher in Navision 🙂