Created
November 1, 2018 14:03
-
-
Save qinmenghua/7538e1888764587c914f2413ec299935 to your computer and use it in GitHub Desktop.
TudouCode Demo in Python 3. 与佛论禅 Python 3 版 Demo(源码参见 https://github.com/lersh/TudouCode/ )。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !/usr/bin/env python3 | |
# | |
# nianfo.py - TudouCode Demo in Python 3 | |
# Credit: https://github.com/lersh/TudouCode/ | |
# | |
# Note: | |
# | |
# This is the first version('佛曰') of TudouCode('与佛论禅'). | |
# The second version('如是我闻') needs compression and I can't debug. | |
# | |
# Dependency: pycrypto | |
from Crypto.Cipher import AES | |
from random import choice | |
KEY = b'XDXDtudou@KeyFansClub^_^Encode!!' | |
IV = b'Potato@Key@_@=_=' | |
TUDOU = [ | |
'滅', '苦', '婆', '娑', '耶', '陀', '跋', '多', '漫', '都', '殿', '悉', '夜', '爍', '帝', '吉', | |
'利', '阿', '無', '南', '那', '怛', '喝', '羯', '勝', '摩', '伽', '謹', '波', '者', '穆', '僧', | |
'室', '藝', '尼', '瑟', '地', '彌', '菩', '提', '蘇', '醯', '盧', '呼', '舍', '佛', '參', '沙', | |
'伊', '隸', '麼', '遮', '闍', '度', '蒙', '孕', '薩', '夷', '迦', '他', '姪', '豆', '特', '逝', | |
'朋', '輸', '楞', '栗', '寫', '數', '曳', '諦', '羅', '曰', '咒', '即', '密', '若', '般', '故', | |
'不', '實', '真', '訶', '切', '一', '除', '能', '等', '是', '上', '明', '大', '神', '知', '三', | |
'藐', '耨', '得', '依', '諸', '世', '槃', '涅', '竟', '究', '想', '夢', '倒', '顛', '離', '遠', | |
'怖', '恐', '有', '礙', '心', '所', '以', '亦', '智', '道', '。', '集', '盡', '死', '老', '至'] | |
BYTEMARK = ['冥', '奢', '梵', '呐', '俱', '哆', '怯', '諳', '罰', '侄', '缽', '皤'] | |
def Encrypt(plaintext): | |
# 1. Encode Plaintext in UTF-16 Little Endian | |
data = plaintext.encode('utf-16le') | |
# 2. Add Paddings (PKCS7) | |
pads = (- len(data)) % 16 | |
data = data + bytes(pads * [pads]) | |
# 3. Use AES-256-CBC to Encrypt | |
cryptor = AES.new(KEY, AES.MODE_CBC, IV) | |
result = cryptor.encrypt(data) | |
# 4. Encode and Add Header | |
return '佛曰:' + ''.join([TUDOU[i] if i < 128 else choice(BYTEMARK) + TUDOU[i-128] for i in result]) | |
def Decrypt(ciphertext): | |
# 1. Remove Header and Decode | |
if ciphertext.startswith('佛曰:'): | |
ciphertext = ciphertext[3:] | |
data = b'' | |
i = 0 | |
while i < len(ciphertext): | |
if ciphertext[i] in BYTEMARK: | |
i = i + 1 | |
data = data + bytes([TUDOU.index(ciphertext[i]) + 128]) | |
else: | |
data = data + bytes([TUDOU.index(ciphertext[i])]) | |
i = i + 1 | |
# 2. Use AES-256-CBC to Decrypt | |
cryptor = AES.new(KEY, AES.MODE_CBC, IV) | |
result = cryptor.decrypt(data) | |
# 3. Remove Paddings (PKCS7) | |
flag = result[-1] | |
if flag < 16 and result[-flag] == flag: | |
result = result[:-flag] | |
# 4. Decode Plaintext with UTF-16 Little Endian | |
return result.decode('utf-16le') | |
else: | |
return '' | |
print(Encrypt('test测试测试测试')) | |
print(Decrypt('佛曰:梵依沙若罰怖諳羅怯大罰波除諳室若若奢伽涅怯究皤苦冥逝冥曳麼梵咒離除奢咒梵夷有怯神倒所滅俱孕盡等呐佛')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment