Skip to content

Cipher

Bacon's cipher

培根密码是一种简单的替换密码,密文字符只有a和b

  • 每个明文字符都会被替换为一个由a和b组成的长度为5的字符串

  • h -> aabbb

  • 密文字符也可以选择任意两个其它字符

  • 常规培根密码表 明文i和j、u和v所对应的密文是一样的

  • 扩展培根密码表 包括所有26个字母

Bacon Table

python
str = 'hellO everyone,Are YOU huNGrY? woUld you li To eAt BAcon'

res = ''

for i in str:
    if i.islower():
        res += 'A'
    elif i.isupper():
        res += 'B'
print(res)

table = ''.maketrans('AB','BA')
print(res.translate(table))

Caesar cipher

凯撒密码加密原理

  • 把明文中的所有字母按字母表顺序向后移动n位,数字和非字母字符保持不变。
  • 位移数n就是密钥。
  • 凯撒密码只有25种可能的密钥。

caesar encrypt

tools.py

python
# caesar转换 text-待转换的文本   n-移动位数(正数右移,负数左移) 
def caesar_transfer(text, n):
    res = ''
    for i in text:
        if i.isalpha():
            if i.isupper():
                res += chr(((ord(i) - 65) + n) % 26 + 65)
            elif i.islower():
                res += chr(((ord(i) - 97) + n) % 26 + 97)
        else:
            res += i
    # print(res)
    return res

caesar_demo.py

python
#!/usr/bin/python
from tools import caesar_transfer

# 'L oryh Sbwkrq3'
# 'synt{mur_VF_syn9_svtug1at}'
# 'oknqdbqmoq{kag_tmhq_xqmdzqp_omqemd_qzodkbfuaz}'
# 'NGBKLATCOZNIXEVZU'
# "gmbhjtdbftbs" 

keywords = ('flag','ctf','pass','fun', 'have','caesar','cyber','you','zmxh','mzwg','666c')

cipher = input("请输入密文:")
print('-'*15, '解密结果', '-'*15)

for i in range(1,26):
    res = caesar_transfer(cipher.strip(), i)
    for keyword in keywords:
        if keyword in res.lower():
            print('Maybe:', res)
            break

ROT13

ROT13是凯撒密码的一种变体,移位数固定为13。

  • ROT13实现的效果是将26个字母的前半部分与后半部分互换,ROT13的密文和明文互为逆反。

ROT13

Last updated:

Released under the MIT License.