# Bifid Cipher

## Introduction §

Bifid is a cipher which combines the Polybius square with transposition, and uses fractionation to achieve diffusion. It was invented by Felix Delastelle. Delastelle was a Frenchman who invented several ciphers including the bifid, trifid, and four-square ciphers. The first presentation of the bifid appeared in the French Revue du GĂ©nie civil in 1895 under the name of cryptographie nouvelle.

It has never been used by a military or government organisation, only ever by amateur cryptographers. Be wary of the Wikipedia page on bifid, it is almost entirely incorrect.

## The Algorithm §

Keys for the Bifid cipher consist of a 25 letter 'key square'. e.g.

```   1 2 3 4 5
1| p h q g m
2| e a y l n
3| o f d x k
4| r c v s z
5| w b u t i
```

Note that there is no 'j' in the key-square, it is merged with the letter 'i'. The example below will encipher 'defend the east wall of the castle' using the key shown above.

When enciphering a plaintext, each letter is replaced by the numbers on the left hand side and top of the keysquare. These are then written on top of one another as shown in step 1 (below). E.g. 'd' is in row 3, column 3 of the key square so 3 is written in the top row and 3 is written in the second row. This is done for all plaintext letters. Step 2: The numbers are then grouped into blocks of a certain size (this is called the period, and forms part of the key). In this example the period is 5. The groups are then read off left to right (this is the fractionating step that makes bifid slightly more difficult to crack than a simple substitution cipher). Step 3 shows the new sequence of numbers after reading the groups left to right, first the top row of the group followed by the bottom row. The entire string is then re-enciphered using the original keysquare (shown in step 4) e.g. 'row 3, col 2' is 'f' in the original keysquare.

An example encryption using the above key:

```plaintext:   defend the east wall of the castle

step 1: row  323223 512 2245 5222 33 512 424522
col  312153 421 1244 1244 12 421 224441

step 2:      32322 35122 24552 22335 12424 522
31215 34211 24412 44124 21224 441

step 3:      3232231215 3512234211 2455224412 2233544124 1242421224 522441

step 4:      f f y h m  k h y c p  l i a s h  a d t r l  h c c h l  b l r```

Thus 'defendtheeastwallofthecastle' becomes 'ffyhmkhycpliashadtrlhcchlblr' using the key square shown above and a period of 5 during the enciphering step.

## Javascript Example §

The keysquare in this example has been written on a single line. To convert to the square shape, simply write the first 5 characters on the first line, the second 5 characters on the second line etc. Plaintext

keysquare =

Period =

Ciphertext

## Other Implementations §

To encipher your own messages in python, you can use the pycipher module. To install it, use pip install pycipher. To encipher messages with the Bifid cipher (or another cipher, see here for documentation):

```>>>from pycipher import Bifid
>>>Bifid('phqgmeaylnofdxkrcvszwbuti',5).encipher('defend the east wall of the castle')
'FFYHMKHYCPLIASHADTRLHCCHLBLR'
>>>Bifid('phqgmeaylnofdxkrcvszwbuti',5).decipher('FFYHMKHYCPLIASHADTRLHCCHLBLR')
'DEFENDTHEEASTWALLOFTHECASTLE'
```

## Cryptanalysis §

For C code that breaks bifid ciphers using simulated annealing see here.

Cryptanalysis of bifid by hand is actually fairly difficult. The 'fractionating' nature of the cipher i.e. each letter is substituted by 2 characters, then these characters are jumbled (which will pull them apart) makes the cipher much stronger than substitution ciphers or transposition ciphers on their own.

With computers, however, bifid is not all that secure. A good description of cracking the bifid cipher is provided in lecture 17 of the LANIKI Crypto Course Lessons Lecture 17, there is also a paper Automated Ciphertext-Only Cryptanalysis of the Bifid Cipher by Machiavelo and Reis.

Bifid can be quickly broken using a simulated annealing algorithm for finding the key square. Have a look at Cryptanalysis of the Bifid cipher.

## References §

comments powered by Disqus

## Further reading

We recommend these books if you're interested in finding out more.

Cryptanalysis: A Study of Ciphers and Their Solution ASIN/ISBN: 978-0486200972 Buy from Amazon.com
Elementary Cryptanalysis: A Mathematical Approach ASIN/ISBN: 978-0883856475 Buy from Amazon.com
The Code Book: The Science of Secrecy from Ancient Egypt to Quantum Cryptography ASIN/ISBN: 978-1857028799 Simon Singh's 'The Code Book' is an excellent introduction to ciphers and codes Buy from Amazon.com
The Codebreakers - The Story of Secret Writing ASIN/ISBN: 0-684-83130-9 Buy from Amazon.com
GQQ RPIGD GSCUWDE RGJO WDO WT IWTO WA CROEO EOJOD SGPEOE: SRGDSO, DGCPTO, SWIBPQEUWD, RGFUC, TOGEWD, BGEEUWD GDY YOEUTO