Search

Affichage des articles dont le libellé est caesar. Afficher tous les articles
Affichage des articles dont le libellé est caesar. Afficher tous les articles

mardi 28 février 2017

Cipher: ASCII shift

Context

The Shift cipher (also known as Caesar cipher) is a very simple and basic encryption technique.
Basically, each letter from alphabet is substituted by the Nth letter after this one in the alphabet (wrapping on alphabet's end to the beginning). The value of N is the cipher key.
Here, let's have a similar algorithm based on ASCII codes. We can of course add a lot of checks to consider only letters, have different behaviors on upper cased / lower cased chars, wrap on alphabet, ... but we will keep it short and simple.
Our cipher will just read a key N (number in its decimal form), then a comma separator, and finally each char will be displayed with an offset of N.
The deciphering tool can be based on the same code, with only one instruction to be replaced (the offset is taken as a negative number)

Initial state

  • Memory: empty
  • Cursor: first cell
  • Input: N,text to encrypt / decrypt

Process

  • Read key
    • Read char
      • If it's a comma, stop reading key
      • Otherwise, consider char as next decimal digit of the current key
  • Cipher / decipher engine
    • Read char
    • Add / remove offset
    • Print char
    • Loop

Code - cipher - try it

read key and comma separator
>,[>++++[-<----------->]+<[---->++++++++[-<<[->+>>+<<<]>>>[-<<<+>>>]<]<[-<+>],>]>[->]<<]

read chars then shift and print
>,[<<[->+>+<<]>[-<+>]>.,]

 Code - decipher - try it

read key and comma separator
>,[>++++[-<----------->]+<[---->++++++++[-<<[->+>>+<<<]>>>[-<<<+>>>]<]<[-<+>],>]>[->]<<]

read chars then shift and print
>,[<<[->+>-<<]>[-<+>]>.,]

 Final state

  • Memory: key 0 0
  • Cursor: third cell
  • Input: empty
  • Output: ciphered text