猫史档案馆


【Python作品分享】code【作品秀】

用户:上进的阿米巴scdr上进的阿米巴scdr查看:0 回复:3 评论:0 创建时间:2020-05-16T14:59:46


【作品展示】

center_image

 

【作品介绍】

加密器,用于愚人节时喵,也可以用于密信等玩法

 

【作品源代码】

import json
from os import sep

# 加密


def code(word, Type, password=None, spacer='  '):
    # 十六进制
    if Type == 'hex':
        with open(r'system\hex.json') as f:
            Dict = json.load(f)
    # 十进制
    elif Type == 'dec':
        with open(r'system\bas.json') as f:
            Dict = json.load(f)
    # 八进制
    elif Type == 'oct':
        with open(r'system\oct.json') as f:
            Dict = json.load(f)
    # 二进制
    elif Type == 'bin':
        with open(r'system\bin.json') as f:
            Dict = json.load(f)
    # 获得密码字典
    strs = list(Dict.keys())
    output = ''
    # 开始加密
    for s in word:
        if s in strs:
            output += str(Dict[s])
        else:
            output += s
        output += spacer
    # 加密信息
    end_tips = '\n\n\ntype={}\nspacer={}'.format(Type, spacer)
    if password != None:
        ps = password
        end_tips += '\npassword={}'.format(ps)
    output += end_tips
    return output

# 解密


def decode(word, password=None):
    # 拆开分析
    txt, inf = word.split('\n\n\n')
    inf = inf.split('\n')
    # 有密码
    if len(inf) == 3:
        pas = inf[2].split('=')[-1]
        # 密码不正确
        if password != pas:
            return '错误:密码不正确'
    # 获取加密类型
    Type = inf[0].split('=')[-1]
    # 十六进制
    if Type == 'hex':
        with open(r'system\hex.json') as f:
            Dict = json.load(f)
    # 十进制
    elif Type == 'dec':
        with open(r'system\bas.json') as f:
            Dict = json.load(f)
    # 八进制
    elif Type == 'oct':
        with open(r'system\oct.json') as f:
            Dict = json.load(f)
    # 二进制
    elif Type == 'bin':
        with open(r'system\bin.json') as f:
            Dict = json.load(f)
    # 获得密码字典
    _strs = list(Dict.values())
    vals = list(Dict.keys())
    strs = []
    for a in _strs:
        strs.append(str(a))
    reldict = dict(zip(strs, vals))
    output = ''
    # 开始解密
    spacer=inf[1].split('=')[-1]
    txt=txt.split(spacer)
    for text in txt:
        if text in strs:
            output+=reldict[text]
        else:
            output+=text
    return output


if __name__ == '__main__':
    print(code('codemao', 'bin',spacer='/',password='bcm'))
    print(code('lightling_monkey', 'oct', password='ldh'))
    print(decode(code('lightling_monkey', 'oct', password='ldh'), password='ldh'))

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
上进的阿米巴scdr上进的阿米巴scdr

这里的四个json文件是我已经写好的

点赞2


评论


上进的阿米巴scdr上进的阿米巴scdr

lightling_monkey是我翻译的,中文是雷电猴

点赞2


评论


lsk666666lsk666666

你让我成功想起了我自己写的Unicode编码器。也算是一种加密方式吧,因为谁会去背Unicode码表呢

点赞0


评论