Search

Affichage des articles dont le libellé est shift. Afficher tous les articles
Affichage des articles dont le libellé est shift. 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

mercredi 15 février 2017

Shifts

Context


Right or left bit shifts are operators in algorithmic, with a general syntax >> or <<.
These operators append B bits at the beginning or end of A, and therefore truncate the last or first B bits of A.
Example: 47 << 2 = 188, as 47 is 00101111, and 10111100 = 188 in base 10.
One useful application is the multiplication / division by powers of 2. Actually, X<<Y = X*2^Y, and X>>Y = X/2^Y.
Let's implement operations <<1 and >>1, namely multiplication / division by 2.

Initial state

  • Memory: X 0 0
  • Cursor: first cell
  • Input: any

Process - left shift

  • While first cell is not null
    • Remove 1 to first cell
    • Add 2 to second cell
    • Loop invariant: first cell + (second cell) / 2 = X
    • When first cell is null: second cell equals 2X

Code - try it

[->++<]

Final state - left shift

  • Memory: 0 2X
  • Cursor: first cell
  • Input: unchanged
  • Output: unchanged

Process - right shift

  • Division by 2 can be implemented in a different way than regular division:
  • While first cell if not null
    • Remove 1 to first cell
    • If possible, remove 1 again to first cell, increase third cell and stay on second
    • Move right (i.e. on second cell if first is now null, third otherwise, and third is not null in this case)
    • Move left if current is not null (i.e. fallback on second cell in all case)
    • Loop invariant: first cell + 2*third cell = X or X-1
    • When first cell is null: third is half X, rounded to floor

Code - try it

[-[->>+<]>[<]<]

 Final state - right shift

  • Memory: 0 0 X/2
  • Cursor: first cell
  • Input: unchanged
  • Output: unchanged