Implement ancient ciphers in python

Typical ancient cipher

Caesar cipher (shift cipher)

Cryptographic implementation

Caesar cipher encrypts by shifting the alphabet by a certain number of characters. If the number of shifts is n, it is called ROT N. For example, when it is 3, it is ROT3.

caesar_cipher.py


#encryption
def encrypt(plain_text, shift_num):
	cipher = ""    #Cryptogram
	for char in plain_text:
		if(char.isupper()):    #When capitalized
		    cipher += chr((ord(char) + shift_num - 65) % 26 + 65)
		else:                  #In lowercase
			cipher += chr((ord(char) + shift_num - 97) % 26 + 97)
	return cipher

#Plaintext
plain  = "draemon"

#Number of shifts,key
s = 3

print("Plain Text : " + plain) 
print("shift_num : " + str(s))
print("cipher : " + encrypt(plain, s))

When you run

% python caesar_cipher.py 
Plain Text : draemon
shift_num : 3
cipher : gudhprq

I was able to encrypt it.

Decode with brute force attack

Since English has only 26 characters, there are at most 26 ways to shift it.

caesar_decipher.py




#Decode with brute force attack
def decrypt(cipher_text):  
	for shift_num in range(26):
		answer = ""  #Key shift_Result when decoding with num
		for char in cipher_text:
			if(ord(char) - shift_num < 65):        #When it is capitalized and smaller than A when shifted
				answer += chr(ord(char) - shift_num + 26)
			elif(ord(char) - shift_num < 97):      #When it is lowercase and less than a when shifted
				answer += chr(ord(char) - shift_num + 26)

			else:                                   #In lowercase
				answer += chr(ord(char) - shift_num)

		print("key" + str(shift_num) + "Decrypt with →" + answer)


#Cryptogram
cipher  = "gudhprq"

#Decoding
decrypt(cipher) 

When you do this

% python caesar_decipher.py
Decrypt with key 0 → gudhprq
Decrypt with key 1 → ftcgoqp
Decrypt with key 2 → esbfnpo
Decrypt with key 3 → draemon
Decrypt with key 4 → cqzdlnm
Decrypt with key 5 → bpyckml
Decrypt with key 6 → aoxbjlk
Decrypt with key 7 → znwaikj
Decrypt with key 8 → ymvzhji
Decrypt with key 9 → xluygih
Decrypt with key 10 → wktxfhg
Decrypt with key 11 → vjswegf
Decrypt with key 12 → uirvdfe
Decrypt with key 13 → thquced
Decrypt with key 14 → sgptbdc
Decrypt with key 15 → rfosacb
Decrypt with key 16 → qenrzba
Decrypt with key 17 → pdmqyaz
Decrypt with key 18 → oclpxzy
Decrypt with key 19 → nbkowyx
Decrypt with key 20 → majnvxw
Decrypt with key 21 → lzimuwv
Decrypt with key 22 → kyhltvu
Decrypt with key 23 → jxgksut
Decrypt with key 24 → iwfjrts
Decrypt with key 25 → hveiqsr

The most meaningful of these is when decrypted with key 3.

Single substitution cipher

The Caesar cipher is encrypted by shifting the alphabet, while the single transliteration cipher is encrypted by replacing the alphabet with a different alphabet. For example, when the plaintext is draemon, it becomes jixbors when encrypted according to the following correspondence table.

1 2 3 4 5 6 7
a d e m n o r
x j b o s r i

Cryptographic implementation

This time, I used the text file jobs_quote.txt which summarizes the English quotes of Steve Jobs Quotations as plain text, and used a single translation Encrypt with encryption. However, for simplicity, consider only the alphabet (both uppercase and lowercase).

simple_substitution.py


import random
import re

#Function to encrypt
def simple_sub_cipher():

    #Generate an ASCII list of alphabets
	alphabets = [65 + i for i in range(26)] + [97 + i for i in range(26)]    
	new_alphabets = [65 + i for i in range(26)] + [97 + i for i in range(26)]

	random.shuffle(new_alphabets)      #Sort randomly
	

	path = 'jobs_quote.txt'         #text file path
	with open(path) as f:
		words = f.read()      #Read text file
		words = re.sub(r'[\W0-9_]', "", words)     #Erase everything except the alphabet

        #Replace with new alphabets in order from a
		for old, new in zip(alphabets, new_alphabets):
			words = words.replace(chr(old), chr(new))

		print(words)      #Displaying encrypted text


simple_sub_cipher()

When you run

% python simple*
QPAacTWfDfDATDQTjAcTWaPLWQCPDfaHQcQfTGAAacafTTAQTLTFaHAAacaAacTWQLOaGOCGODcATTAATDaGQWATCGEATTAkDnALDEaCGEfTjATTAQQaODcfTTAQcCcVaHODEaAAaPCGcfTTAWaHQaODfGcATTACjTjAfHDPafWaHffafkTjCACjPafWaHfQaODfjQAjaGQWFWjTWCGEGaATTAWaHOTGOaGODGAfTADaGATDATCGEjATTATfDfDTQQWCLnafATGAVaHOTGAOaGGDOAATDcaAjQaakCGE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CAATDLIHCOkQWTGcEDAaGfCATCLnfaOCGEWaHfaATDfCGGaOTACaGjQGGaOTACaGcCjACGEHCjTDjFDAfDDGTQDTcDfTGcTPaQQafDfcaWaHfTGAAajnDGcATDfDjAaPWaHfQCPDjDQQCGEjHETfDcfTADfafcaWaHfTGATOTTGODAaOTTGEDATDfafQcVaHfACLDCjQCLCADcjacaGAfTjADCAQCOCGEjaLDaGDDQjDjQCPDcaGAFDAfTnnDcFWcaELTfTCOTCjQCOCGEfCATATDfDjHQAjaPaATDfnDanQDjATCGkCGEcaGAQDAATDGaCjDaPaATDfjanCGCaGjcfafGaHAWaHfafGCGGDfOaCODfGcLajACLnafATGATTODATDOaHfTEDAaPaQQafWaHfTDTfATGcCGAHCACaGnTDWjaLDTafTQfDTcWkGaffTTAWaHAfHQWfTGAAaFDOaLDWODfWATCGEDQjDCjjDOaGcTfWbDTWTfcjACOkaPIHTQCAWEaLDnDanQDTfDGAHjDcAaTGDGOCfaGLDGAfTDfDDqODQQDGODCjDqnDOADcQLTjnfaHcaPfTTAfDcaGAcaTjQTLaPfTTAfDcaQcCcGAjDDCAATDGFHACAAHfGDcaHAATTAEDAACGEPCfDcPfaLfnnQDfTjATDFDjAATCGEATTAOaHQcTTODDODfTTnnDGDcAaLDnTDTDTOCGDjjaPFDCGEjHOODjjPHQfTjfDnQTODcFWATDQCETAGDjjaPFDCGETFDECGGDfTETCGQDjjjHfDTFaHADODfWATCGEQAPfDDcLDAaDGADfaGDaPATDLajAOfDTACODnDfCacjaPLWQCPDQPWaHQCODDTOTcTWTjCPCAfTjWaHfQTjAjaLDcTWWaHQQLajAODfATCGQWFDfCETAVaHOTGAjHjATjkOHjAaLDfjfTTAATDWfTGATGcATDGAfWAaECODATTAAaATDLbWATDACLDWaHEDACAFHCQAATDWQQfTGAjaLDATCGEGDf

I was able to encrypt it into an incomprehensible sentence.

Decoding by frequency analysis

Frequency analysis is to find out the frequency of letters.

For example, according to Wikipedia of Frequency Analysis, general The most commonly used is ʻe. The second comes ʻi, the third comes ʻa, the fourth comes t, and the fifth comesn`.

Here, let's analyze the frequency of jobs_quote.txt.

frequency_analyzer.py


import re

path = 'jobs_quote.txt'  #path setting
with open(path) as f:
	words = f.read()   #Read file
	words = re.sub(r'[\W0-9_]', "", words).lower()

	char_set = { alphabet for alphabet in words } #Separate one character at a time
	char_freq = { alphabet : words.count(alphabet) for alphabet in char_set }  #Count the number of letters

	sorted_char_freq = sorted(char_freq.items(), key = lambda x:x[1],reverse = True)    #Sort by frequency

	print(sorted_char_freq)#indicate

When you run

% python freq*             
[('t', 285), ('e', 269), ('o', 244), ('n', 172), ('a', 168), ('i', 167), ('s', 128), ('r', 122), ('h', 118), ('l', 92), ('u', 89), ('y', 85), ('d', 84), ('m', 66), ('w', 64), ('c', 60), ('g', 55), ('f', 47), ('p', 39), ('v', 34), ('b', 32), ('k', 23), ('q', 5), ('x', 3), ('j', 1)]

It became. Although it is not exactly the same as the frequency analysis result of Wikipedia earlier, the result is quite similar considering that it is a saying.

By the way, if you know t and e, you can easily guess the, they, he, get, etc. that are often used in English. You can decipher this if you do your best.

Other historical ciphers

ROT13

The number of shifts of the Caesar cipher is 13.

Let x be the plaintext

ROT_{13}(ROT_{13}(x)) = ROT_{0}(x)

There are 26 alphabets, so if you repeat ROT13 twice, you will return to the sentence below. When ravine is converted to ROT13, it is used for word games such as enivar.

>>> import codecs
>>> codecs.decode('abc', 'rot13')
'nop'

Vigenère cipher

It is called the following table Vigenère cipher with the alphabet shifted one by one. 20160508023749.png

For example, when the plaintext is'DRAEMON'and the key is'ABC', D is D in A row and D column, R is S in B row and R column, and A is C in C row and A column. It becomes. If the plaintext is longer than the key, use the key repeatedly. In other words, the next ʻEis E in row A and column E. If this is repeated, it will be encrypted asDSCENQN`. It is important not to know the key cycle (3 in this case).

Here, a = 0, b = 1, ..., z = 26, pi is the i-th character of the plaintext, Ki is the i-th character of the key, and Ci is the i-th character of the ciphertext.

C_i = (P_i + K_i)mod26

By the way, if decoding is expressed in the same way

P_i = (C_i - K_i)mod26

Will be.

There are two deciphering methods: ** Cassis Key Test ** and ** Key Deduction **.

The ** Cassis Key Test ** focuses on key repetition and cannot be used when it is very long (more than half the length of plaintext).

Uesugi Cipher

Like the Vigenère cipher, hiragana is placed in a 7-square table, numbers are assigned vertically and horizontally, and the numbers corresponding to the hiragana are used for encryption.

For example, shino is encrypted as75, 36, 46.

series01_encryption_history_03_02.gif

Recommended Posts

Implement ancient ciphers in python
Implement Enigma in python
Implement XENO in python
Implement sum in Python
Implement Traceroute in Python 3
Implement naive bayes in Python 3.3
Implement Redis Mutex in Python
Implement extension field in Python
Implement fast RPC in Python
Implement method chain in Python
Implement Dijkstra's Algorithm in python
Implement Slack chatbot in Python
Implement stacking learning in Python [Kaggle]
Implement R's power.prop.test function in python
Implement the Singleton pattern in Python
Quickly implement REST API in Python
Quadtree in Python --2
Python in optimization
CURL in python
I tried to implement PLSA in Python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Implement __eq__ etc. generically in Python class
I tried to implement permutation in Python
Meta-analysis in Python
Unittest in python
Implement FIR filters in Python and C
Collectively implement statistical hypothesis testing in Python
I tried to implement PLSA in Python 2
Epoch in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
I tried to implement ADALINE in Python
Constant in python
I tried to implement PPO in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python