Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Baconian Cipher
Next article icon

Baconian Cipher

Last Updated : 03 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Bacon's cipher or the Baconian cipher is a method of steganography (a method of hiding a secret message as opposed to just a cipher) devised by Francis Bacon in 1605. A message is concealed in the presentation of text, rather than its content. The Baconian cipher is a substitution cipher in which each letter is replaced by a sequence of 5 characters. In the original cipher, these were sequences of 'A's and 'B's e.g. the letter 'D' was replaced by 'aaabb', the letter 'O' was replaced by 'abbab' etc. 

Each letter is assigned to a string of five binary digits. These could be the letters 'A' and 'B', the numbers 0 and 1 or whatever else you may desire. 

There are 2 kinds of Baconian ciphers -

  1. The 24 letter cipher: In which 2 pairs of letters (I, J) & (U, V) have same ciphertexts.
LetterCodeBinary
Aaaaaa00000
Baaaab00001
Caaaba00010
Daaabb00011
Eaabaa00100
Faabab00101
Gaabba00110
Haabbb00111
I, Jabaaa01000
Kabaab01001
Lababa01010
Mababb01011
LetterCodeBinary
Nabbaa01100
Oabbab01101
Pabbba01110
Qabbbb01111
Rbaaaa10000
Sbaaab10001
Tbaaba10010
U, Vbaabb10011
Wbabaa10100
Xbabab10101
Ybabba10110
Zbabbb10111
  1. The 26 letter cipher: In which all letters have unique ciphertexts.
LetterCodeBinary
Aaaaaa00000
Baaaab00001
Caaaba00010
Daaabb00011
Eaabaa00100
Faabab00101
Gaabba00110
Haabbb00111
Iabaaa01000
Jabaab01001
Kababa01010
Lababb01011
Mabbaa01100
LetterCodeBinary
Nabbab01101
Oabbba01110
Pabbbb01111
Qbaaaa10000
Rbaaab10001
Sbaaba10010
Tbaabb10011
Ubabaa10100
Vbabab10101
Wbabba10110
Xbabbb10111
Ybbaaa11000
Zbbaab11001

Encryption

We will extract a single character from the string and if its not a space then we will replace it with its corresponding ciphertext according to the cipher we are using else we will add a space and repeat it until we reach the end of the string. For example 'A' is replaced with 'aaaaa' 

Decryption

We will extract every set of 5 characters from the encrypted string and check if the first character in that set of 5 characters is a space. If not we will lookup its corresponding plaintext letter from the cipher, replace it and increment the index of character by 5 (to get the set of next 5 characters) else if its a space we add a space and repeat a process by incrementing the current index of character by 1

Approach

In Python, we can map key-value pairs using a data structure called a dictionary. We are going to use just one dictionary in which we will map the plaintext-ciphertext pairs as key-value pairs. For encryption we will simply lookup the corresponding ciphertext by accessing the value using the corresponding plaintext character as key. In decryption we will extract every 5 set of ciphertext characters and retrieve their keys from the dictionary using them as the corresponding value. For an accurate decryption we will use the 26 letter cipher. If you are not coding in python then you can come up with your own approach. 

Implementation:

Python
# Python program to implement Baconian cipher  '''This script uses a dictionary instead of 'chr()' & 'ord()' function'''  ''' Dictionary to map plaintext with ciphertext (key:value) => (plaintext:ciphertext) This script uses the 26 letter baconian cipher in which I, J & U, V have distinct patterns ''' lookup = {'A': 'aaaaa', 'B': 'aaaab', 'C': 'aaaba', 'D': 'aaabb', 'E': 'aabaa',           'F': 'aabab', 'G': 'aabba', 'H': 'aabbb', 'I': 'abaaa', 'J': 'abaab',           'K': 'ababa', 'L': 'ababb', 'M': 'abbaa', 'N': 'abbab', 'O': 'abbba',           'P': 'abbbb', 'Q': 'baaaa', 'R': 'baaab', 'S': 'baaba', 'T': 'baabb',           'U': 'babaa', 'V': 'babab', 'W': 'babba', 'X': 'babbb', 'Y': 'bbaaa', 'Z': 'bbaab'}  # Function to encrypt the string according to the cipher provided   def encrypt(message):     cipher = ''     for letter in message:         # checks for space         if(letter != ' '):             # adds the ciphertext corresponding to the             # plaintext from the dictionary             cipher += lookup[letter]         else:             # adds space             cipher += ' '      return cipher  # Function to decrypt the string # according to the cipher provided   def decrypt(message):     decipher = ''     i = 0      # emulating a do-while loop     while True:         # condition to run decryption till         # the last set of ciphertext         if(i < len(message)-4):             # extracting a set of ciphertext             # from the message             substr = message[i:i + 5]             # checking for space as the first             # character of the substring             if(substr[0] != ' '):                 '''                 This statement gets us the key(plaintext) using the values(ciphertext)                 Just the reverse of what we were doing in encrypt function                 '''                 decipher += list(lookup.keys()                                  )[list(lookup.values()).index(substr)]                 i += 5  # to get the next set of ciphertext              else:                 # adds space                 decipher += ' '                 i += 1  # index next to the space         else:             break  # emulating a do-while loop      return decipher   def main():     message = "Geeks for Geeks"     result = encrypt(message.upper())     print(result)      message = "AABAAABBABABAABABBBABBAAA"     result = decrypt(message.lower())     print(result)   # Executes the main function if __name__ == '__main__':     main() 

Output
aabbaaabaaaabaaabababaaba aabababbbabaaab aabbaaabaaaabaaabababaaba ENJOY

Complexity Analysis:  The Baconian cipher is a simple substitution cipher that replaces each letter of the plaintext with a corresponding five-character code. The cipher has a fixed dictionary of 26 codes for each letter of the alphabet, and can handle both upper and lowercase letters, as well as spaces. The encrypt() function has a time complexity of O(n) and the decrypt() function has a time complexity of O(n^2), where n is the length of the input string. Both functions have a space complexity of O(n), where n is the length of the input string.

Analysis: This cipher offers very little communication security, as it is a substitution cipher. As such all the methods used to cryptanalyse substitution ciphers can be used to break Baconian ciphers. The main advantage of the cipher is that it allows hiding the fact that a secret message has been sent at all. 

 


Next Article
Baconian Cipher

P

Palash Nigam
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

    Feistel Cipher
    Feistel Cipher model is a structure or a design used to develop many block ciphers such as DES. Feistel cipher may have invertible, non-invertible and self invertible components in its design. Same encryption as well as decryption algorithm is used. A separate key is used for each round. However sam
    3 min read
    Cryptophasia Code in Python
    Cryptophasia is a language that is created and spoken between twins before they learn their own mother tongue. Since no one knows that language, it still remains a secret. As humans never left a stone unturned, Scientists have found that this language is created, just by mispronouncing the existing
    3 min read
    Substitution Cipher
    Hiding some data is known as encryption. When plain text is encrypted it becomes unreadable and is known as ciphertext. In a Substitution cipher, any character of plain text from the given fixed set of characters is substituted by some other character from the same set depending on a key. For exampl
    6 min read
    Morse Code Translator In Python
    Morse code is a method of transmitting text information as a series of on-off tones, lights, or clicks that can be directly understood by a skilled listener or observer without special equipment. It is named for Samuel F. B. Morse, an inventor of the telegraph. Algorithm The algorithm is very simple
    5 min read
    XOR Encryption by Shifting Plaintext
    Here is a cipher algorithm, based on hexadecimal strings that is implemented by XORing the given plaintext, N number of times where N is its length. But, the catch is that every next XOR operation is done after shifting the consecutive plain text entry to the right. A sample operation is shown below
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences