[GO] Write a Caesar cipher program in Python

Introduction

When I started CTF, a Caesar cipher came out, so decryption processing was necessary. You can do it manually, but this is a program! It can also be a rehabilitation! So I wrote it immediately. My machine only had a Python development environment, so go with Python!

What is Caesar cipher?

In order to decrypt it, you need to know what algorithm the cipher was generated by. The Caesar cipher is generated by shifting characters by 3 characters. How simple!

Encryption and decryption program

First of all, encryption. Shifts plaintext character by character with the specified key (number). Since the case is different, this time we will use only lowercase letters. There are 25 English letters, so be careful to turn around. Encrypt the appropriate character string "wjrslairznb"!

def crypt(plainStr, shiftInt):
    resultStr = ""
    for char in plainStr:
        if ord(char) + shiftInt > ord('z'):
            resultStr = resultStr + chr(ord(char) + shiftInt - 26)
        else:
            resultStr = resultStr + chr(ord(char) + shiftInt)
    print('crypt:' + resultStr)

Execution result スクリーンショット 2020-11-20 14.11.02.png

Decryption was simply calculated in reverse ...

def decrypt(cryptoStr, shiftInt):
    resultStr = ""
    for char in cryptoStr:
        if ord(char) - shiftInt < ord('a'):
            resultStr = resultStr + chr(ord(char) - shiftInt + 26)
        else:
            resultStr = resultStr + chr(ord(char) - shiftInt)
    print('decrypt:' + resultStr)

Execution result スクリーンショット 2020-11-20 14.12.52.png It seems to be easier and simpler ...

Digression: Caesar cipher is too easy as a cipher, but can it be used?

Not available. Vulnerable! !! It is a code made in ancient Rome, so it must have been valid in the past. Cryptography has a history.

in conclusion

Actually, it's been a while since I've had a program, and even though I have a Python environment, I haven't touched it and it was a lot of fun. There are tons of programs like this out there if you google, but I like to write them myself first and then look at the genius code and match the answers later.

Recommended Posts

Write a Caesar cipher program in Python
I made a Caesar cryptographic program in Python.
Write a super simple molecular dynamics program in python
Write a binary search in Python
Caesar cipher (including kanji) in Python
Write A * (A-star) algorithm in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
When writing a program in Python
I made a payroll program in Python!
Write the test in a python docstring
Write a short property definition in Python
Write a simple greedy algorithm in Python
Write a simple Vim Plugin in Python 3
Write Python in MySQL
A program that removes duplicate statements in Python
A simple Pub / Sub program note in Python
Let's write a Python program and run it
Take a screenshot in Python
Write Pandoc filters in Python
I made a prime number generation program in Python
Create a function in Python
Receive dictionary data from a Python program in AppleScript
Write python in Rstudio (reticulate)
I want to write in Python! (2) Let's write a test
Try embedding Python in a C ++ program with pybind11
[Beginner] What happens if I write a program that runs in php in Python?
Make a bookmarklet in Python
Write a log-scale histogram on the x-axis in python
A program to write Lattice Hinge with Rhinoceros with Python
I made a prime number generation program in Python 2
Draw a heart in Python
A Python program in "A book that gently teaches difficult programming"
A general-purpose program that formats Linux command strings in python
I tried "a program that removes duplicate statements in Python"
Maybe in a python (original title: Maybe in Python)
How to write a Python class
Write a table-driven test in C
[python] Manage functions in a list
Hit a command in Python (Windows)
Write JSON Schema in Python DSL
Create a DI Container in Python
Write an HTTP / 2 server in Python
Draw a scatterplot matrix in python
Write AWS Lambda function in Python
ABC166 in Python A ~ C problem
Compatibility diagnosis program written in python
Write selenium test code in python
Solve ABC036 A ~ C in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Draw a CNN diagram in Python
Create a random string in Python
Write C unit tests in Python
Schedule a Zoom meeting in Python
Write a batch script with Python3.5 ~
Read a file in Python with a relative path from the program
A program that determines whether a number entered in Python is a prime number