Easy Cipher Decryption

Easy Cipher Decryption

 
Author: Joyce_BY, all rights reserved.
Contact by email: [email protected]


caesar cipher / shift cipher / Caesar’s cipher / Caesar’s code / Caesar shift

It is a type of substitution cipher, each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on.

Decryption code is as followed:

	# !/usr/bin/python
	# python 3.7.0
	# encode -- UTF-8 --
	# authorized by Joyce_BY, all rights reserved.

	# caesar cipher
	c = input("input the ciphertext to decrypt: ")
	cipher  = c.split(' ')
	for i in range(26): 
	    plain = []
	    for item in cipher: 
	        m = []
	        for ch in item: 
	            m.append(chr((ord(ch)+i) % 26))
	        plain.append(''.join(m))  
	    print(' '.join(plain))

vigenere cipher

Find details in my another blog: Hacking Vigenère Cipher

hill cipher

Find details in my another blog: Hill Cipher and a Variant

bacon’s cipher

To encode a message, each letter of the plaintext is replaced by a group of five of the letters ‘A’ or ‘B’. This replacement is a binary encoding and is done according to the alphabet of the Baconian cipher.
There are 2 scheme, shown below:

Scheme1

Letter Code Binary
A aaaaa 00000
B aaaab 00001
C aaaba 00010
D aaabb 00011
E aabaa 00100
F aabab 00101
G aabba 00110
H aabbb 00111
I,J abaaa 01000
K abaab 01001
L ababa 01010
M ababb 01011
N abbaa 01100
O abbab 01101
P abbba 01110
Q abbbb 01111
R baaaa 10000
S baaab 10001
T baaba 10010
U,V baabb 10011
W babaa 10100
X babab 10101
Y babba 10110
Z babbb 10111

Scheme2

Letter Code Binary
A aaaaa 00000
B aaaab 00001
C aaaba 00010
D aaabb 00011
E aabaa 00100
F aabab 00101
G aabba 00110
H aabbb 00111
I abaaa 01000
J abaab 01001
K ababa 01010
L ababb 01011
M abbaa 01100
N abbab 01101
O abbba 01110
P abbbb 01111
Q baaaa 10000
R baaab 10001
S baaba 10010
T baabb 10011
U babaa 10100
V babab 10101
W babba 10110
X babbb 10111
Y bbaaa 11000
Z bbaab 11001

code is shown below:

# !/usr/bin/python
# python 3.7.0
# environment: windows 10
# encode -- UTF-8 --
# authorized by Joyce_BY, all rights reserved.
# contact by email: [email protected]

# bacon cipher decryption
# notice that 'A' to 'a', 'a' to 'A' !!

# Scheme 1
dic = { 'aaaaa':'a','aaaab':'b','aaaba':'c','aaabb':'d','aabaa':'e','aabab':'f','aabba':'g','aabbb':'h','abaaa':'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','baabb':'v','babaa':'w','babab':'x', 'babba':'y','babbb':'z'}

# Scheme2
dic = { 'aaaaa':'a','aaaab':'b','aaaba':'c','aaabb':'d','aabaa':'e','aabab':'f','aabba':'g','aabbb':'h','abaaa':'i','abaab':'j',
        'ababa':'k','ababb':'l','abbaa':'m','abbab':'n','abbba':'o','abbbb':'p','baaaa':'q','baaab':'r','baaba':'s','baabb':'t',
        'babaa':'u','babab':'v','babba':'w','babbb':'x','bbaaa':'y','bbaab':'z'}

def patter_change(sstr,a,b):
    ch_str = []
    for ch in sstr:
        if ch == a: 
            ch_str.append('a')
        else: 
            ch_str.append('b')
    s = ''.join(ch_str)
    return s

def decode(sstr):
    d1 = []
    d2 = []
    for i in range(len(sstr) // 5): 
        substr = sstr[:5]
        sstr = sstr[5:]
        d1.append(dic1[substr])
        d2.append(dic2[substr])
    return ''.join(d1), ''.join(d2)
    
if __name__ == '__main__':
    string = input('input the string to decode: ')
    c = string.split(' ')
    m1 = []
    m2 = []
    for item in c: 
        temp = patter_change(item,'c','d')
        t1,t2 = decode(temp)
        m1.append(t1)
        m2.append(t2)
    print("By scheme1: ", (' '.join(m1)).upper())
    print("By scheme2: ", (' '.join(m2)).upper())

当铺密码

当铺密码是一种将中文和数字进行转化的密码, 当前汉字有多少笔画出头,就转化成数字几。
例如:
王夫 井工 夫口 由中人 井中 夫夫 由中大:67 84 70 123 82 77 125
==> CTF{RM}

Morse Code

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.

# !/usr/bin/python
# python 3.7.0
# environment: windows 10
# encode -- UTF-8 --
# authorized by Joyce_BY, all rights reserved.
# contact by email: [email protected]

# morse code decryption

a = input("Input your ciphertext: ")
s = a.split(' ')
# International standard dictionary
dic = { '.-': 'A',
        '-...': 'B',
        '-.-.': 'C',
        '-..':'D',
        '.':'E',
        '..-.':'F',
        '--.': 'G',
        '....': 'H',
        '..': 'I',
        '.---':'J',
        '-.-': 'K',
        '.-..': 'L',
        '--': 'M',
        '-.': 'N',
        '---': 'O',
        '.--.': 'P',
        '--.-': 'Q',
        '.-.': 'R',
        '...': 'S',
        '-': 'T',
        '..-': 'U',
        '...-': 'V',
        '.--': 'W',
        '-..-': 'X',
        '-.--': 'Y',
        '--..': 'Z',
        '.----': '1',
        '..---': '2',
        '...--': '3',
        '....-': '4',
        '.....': '5',
        '-....': '6',
        '--...': '7',
        '---..': '8',
        '----.': '9',
        '-----': '0',
        '..--..': '?',
        '-..-.': '/',
        '-.--.-': '()',
        '-....-': '-',
        '.-.-.-': '.',
        '..--.-': ' ',
        '/': ' '
    }
decode = []
for item in s:
    decode.append(dic[item])
print((''.join(decode)).lower())

猜你喜欢

转载自blog.csdn.net/qq_35649764/article/details/82800623
今日推荐