Try to make a "cryptanalysis" cipher with Python

I tried to encrypt p101-103 of the second volume with Python. All encrypt HELLO.

1-1. Transposition cipher


Input
HELLO

s=input()
n=len(s)
l=[]
for i in range(n):
    l.append(str(bin(ord(s[i])))[2:])

m="".join(l)

odd=m[::2]
even=m[1::2]

num=len(even)

for j in range(num):
    print(even[j],end="")
    print(odd[j],end="")
print(odd[-1])


Output
01100010001010011001100011000110111

First convert to ASCII. This is OK with ord (). Unicode and ASCII have the same alphabet. Yeah.

Binary numbers are difficult to handle, so If you use str () to make a character string, you can retrieve the numbers in each digit. With str (bin (ord (s [i]))) [2:], take the leading 0b.

I wondered how to exchange the first number for the second number, I tried to process it that seems to be the A problem of AtCoder.

It is divided into odd-numbered and even-numbered and output alternately.

Since odd has one more character, only odd [-1] is output at the end. If the input is an even number of alphabets, I have to change it again, For the time being, this time.

1-2. Decoding the transposition type

Now, on the contrary, try returning 01100010001010011001100011000110111 to HELLO.


Input
01100010001010011001100011000110111

s=input()
even=s[:-2:2]
odd=s[1::2]

l=[]
n=len(even)
for i in range(n):
    l.append(odd[i])
    l.append(even[i])
l.append(s[-1])

m="".join(l)

num=len(l)//7

for j in range(num):
    print(chr(int(m[7*j:7*(j+1)],2)),end="")

It's basically the opposite of encryption, so it was still easy.

I knew that int ('number in binary notation', 2) was enough to convert a binary number back to a decimal number, so I could write it simply. Convert binary, octal, and hexadecimal numbers and strings to each other with Python

2-1. Substitution cipher

First, convert the DAVID to ASCII code. 10001001000001101011010010011000100 This is the key.

The rules of encryption are -If the plaintext and key elements are the same, the ciphertext will be 0. -If the plaintext and key elements are different, the ciphertext will be 1. It's an exclusive OR (XOR).


Input
HELLO DAVID

s=input()
key=input()

n=len(s)

#ASCII conversion
l_s=[]
l_k=[]

for i in range(n):
    l_s.append(str(bin(ord(s[i])))[2:])
    l_k.append(str(bin(ord(key[i])))[2:])
    
a_s="".join(l_s)
a_k="".join(l_k)

num=len(a_s)
cipher=[]

for j in range(num):
    if a_s[j]==a_k[j]:
        cipher.append("0")
    else:
        cipher.append("1")

print(*cipher,sep="")


Output
00011000000100001101000001010001011

2-2. Decoding the transposition cipher

Now, conversely, move 00011000000100001101000001010001011 back to HELLO using the key DAVID. If the ciphertext and key elements are the same, the plaintext will be 0, and if they are different, it will be 1. It's the same as when encrypting.


Input
00011000000100001101000001010001011 DAVID

s=input()
key=input()

n=len(key)

#ASCII converter key
l_k=[]

for i in range(n):
    l_k.append(str(bin(ord(key[i])))[2:])
    
k="".join(l_k)

num=len(s)
l_p=[]
for j in range(num):
    if s[j]==k[j]:
        l_p.append("0")
    else:
        l_p.append("1")

p="".join(l_p)

n_p=len(p)//7

for l in range(n_p):
    print(chr(int(p[7*l:7*(l+1)],2)),end="")


Output
HELLO

Recommended Posts

Try to make a "cryptanalysis" cipher with Python
Try to make a dihedral group with Python
Try to make a command standby tool with python
Try to draw a life curve with python
I want to make a game with Python
Make a fortune with Python
Try to make a capture software with as high accuracy as possible with python (2)
Try to make a Python module in C language
Let's make a GUI with python.
Try to operate Facebook with Python
Make a recommender system with python
WEB scraping with python and try to make a word cloud from reviews
Let's make a graph with python! !!
Experiment to make a self-catering PDF for Kindle with Python
Try to bring up a subwindow with PyQt5 and Python
Try to reproduce color film with Python
Try logging in to qiita with Python
Let's make a shiritori game with Python
[Python] How to make a class iterable
Try to make a kernel of Jupyter
Fractal to make and play with Python
Let's make a voice slowly with Python
Try HTML scraping with a Python library
Let's make a web framework with Python! (1)
Try drawing a map with python + cartopy 0.18.0
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
[3rd] I tried to make a certain authenticator-like tool with python
Try to create a python environment with Visual Studio Code & WSL
How to make a surveillance camera (Security Camera) with Opencv and Python
Try to make a web service-like guy with 3D markup language
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
Try to make capture software with as high accuracy as possible with python (1)
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
Try adding a wall to your IFC file with IfcOpenShell python
Try to make your own AWS-SDK with bash
[TCP / IP] After studying, try to make an HTTP client-like with Python
How to read a CSV file with Python 2/3
Try scraping with Python.
Python: I tried to make a flat / flat_map just right with a generator
Send a message to LINE with Python (LINE Notify)
[Cloudian # 3] Try to create a new object storage bucket with Python (boto3)
A memorandum to make WebDAV only with nginx
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
Try to solve the man-machine chart with Python
How to make a dictionary with a hierarchical structure.
Try to make foldl and foldr with Python: lambda. Also time measurement
Try to automatically generate Python documents with Sphinx
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
[Python] Make a simple maze game with Pyxel
Decide to assign a laboratory with Python (fiction)
Steps to create a Twitter bot with python
Let's replace UWSC with Python (5) Let's make a Robot
[Python] Try to make a sort program by yourself. (Selection sort, insertion sort, bubble sort)