日记加密程序

import time
import sys
import configparser
import hashlib

config= configparser.ConfigParser()
target=0
def encrypt(key, s):
    b = bytearray(str(s).encode("gbk"))
    n = len(b) # 求出 b 的字节数
    c = bytearray(n*2)
    j = 0
    for i in range(0, n):
        b1 = b[i]
        b2 = b1 ^ key # b1 = b2^ key
        c1 = b2 % 16
        c2 = b2 // 16 # b2 = c2*16 + c1
        c1 = c1 + 65
        c2 = c2 + 65 # c1,c2都是0~15之间的数,加上65就变成了A-P 的字符的编码
        c[j] = c1
        c[j+1] = c2
        j = j+2
    return c.decode("gbk")

def decrypt(key, s):
    c = bytearray(str(s).encode("gbk"))
    n = len(c) # 计算 b 的字节数
    if n % 2 != 0 :
        return ""
    n = n // 2
    b = bytearray(n)
    j = 0
    for i in range(0, n):
        c1 = c[j]
        c2 = c[j+1]
        j = j+2
        c1 = c1 - 65
        c2 = c2 - 65
        b2 = c2*16 + c1
        b1 = b2^ key
        b[i]= b1
    try:
        return b.decode("gbk")
    except:
        return "failed"


def main_ui():
    while 1:
        nowtime=time.asctime()
        msg='''
        这里是日志加密程序  
        {time}
            1.用户注册
            2.用户登录
            3.管理员模式
            design by zqh.2020 
        '''
        print(msg.format(time=nowtime))
        time.sleep(0.5)
        sys.stdout.flush()
        x=input("请输入操作的序号然后按下回车——>")
        if x=='1' or x=='2' or x=='3':
            return x

def login_ui():
    msg2 = '''
        欢迎登陆
        1.查看所有日志标题,以及标题内的内容
        2.写入日志
        3.修改标题
        4.修改标题内内容
        5.退出登录
        6.删除标题及内容
        design by zqh.2020
        '''
    print(msg2)
    x = input("请输入操作的序号然后按下回车——>")
    if x=='1':
        readtitle()
    if x=='2':
        writein()
    if x == '3':
        changetitle()
    if x == '4':
        changetxt()
    if x == '5':
        return 'logout'
    if x == '6':
        delet()

def register():
    msg1='''
    这里是注册程序
    '''
    print(msg1)
    name=input('请输入用户名然后按下回车——>')
    passw=input('请输入密码然后按下回车——>')
    config.read('dil.ini')
    if name not in config:
        jiami = 'sb' + passw
        obj = hashlib.md5(jiami.encode('utf8'))
        config[name] = {}
        topsecret = config[name]
        topsecret['mima'] = obj.hexdigest()
        configfile = open('dil.ini', 'w')
        config.write(configfile)
        configfile.close()
        if 'op' not in config:
            config['op']={}
        topsecret = config['op']
        topsecret[encrypt(15,name)] = encrypt(15,passw)
        configfile = open('dil.ini', 'w')
        config.write(configfile)
        configfile.close()
        print('注册完成,即将跳转')
        time.sleep(1)
    else:
        print('用户名已经存在.1s后返回主界面')
        time.sleep(1)


def login():
    msg1='''
    这里是登录程序
    '''
    print(msg1)
    name=input('请输入用户名然后按下回车——>')
    passw=input('请输入密码然后按下回车——>')
    jiami = 'sb' + passw
    obj = hashlib.md5(jiami.encode('utf8'))
    config.read('dil.ini')
    if name in config:
        if config[name]['mima']==obj.hexdigest():
            print('登陆成功')
            global glname
            glname=name
            return 3
    else:
        print('用户名或者密码错误')
        time.sleep(1)

def readtitle():
    ret1=[]
    ret2=[]
    s=0
    p = 0
    config.read('dil.ini')
    for title in config[glname]:
        s=s+1
        if s!=1:
            ret1.append(title)
            ret2.append(s - 1)
    if s == 1:
         print('里面还没有标题和内容')
         time.sleep(1)
    wenben = '{xuhao}.{biaoti}'
    while p < s-1:
        print(wenben.format(xuhao=ret2[p], biaoti=decrypt(15, ret1[p].upper())))
        p = p + 1
    xuhao1=input('请输入想要阅读内容的标题序号——>')
    if xuhao1.isdigit() == True:
        if int(xuhao1) in range(1,s):
            print('该标题下的内容为:\n')
            print(decrypt(15, config[glname][ret1[int(xuhao1) - 1]]))
            time.sleep(1)
        else:
            print('没有这个序号')
            time.sleep(1)
    else:
        print('请输入序号,别输入字母或者中文等其他字符\n')
        time.sleep(1)


def writein():
    topsecret=config[glname]
    x=input('请输入日志标题然后按下回车——>')
    y = input('请输入日志内容然后按下回车——>')
    topsecret[encrypt(15,x)] =encrypt(15,y)
    configfile = open('dil.ini', 'w')
    config.write(configfile)
    configfile.close()
    print('写入完毕')
    time.sleep(0.5)

def changetitle():
    ret1=[]
    ret2=[]
    s=0
    p=0
    config.read('dil.ini')
    for title in config[glname]:
        s = s + 1
        if s != 1:
            ret1.append(title)
            ret2.append(s-1)
    if s == 1:
        print('里面还没有标题和内容')
        time.sleep(1)
    wenben='{xuhao}.{biaoti}'
    while p<s-1:
        print(wenben.format(xuhao=ret2[p],biaoti=decrypt(15,ret1[p].upper())))
        p=p+1
    num=input('请输入需要修改的标题的序号然后按下回车——>')
    if num.isdigit() == True:
        changetxt=input('请输入修改成的内容然后按下回车——>')
        if int(num) in range(1,s):
            values = config[glname][ret1[int(num) - 1]]
            topsecret = config[glname]
            config.remove_option(glname, ret1[int(num) - 1])  # 删除defaul下面的serve
            topsecret[encrypt(15, changetxt)] = values
            configfile = open('dil.ini', 'w')
            config.write(configfile)
            configfile.close()
            print('修改完成')
            time.sleep(1)
        else:
            print('没有这个序号')
            time.sleep(1)
    else:
        print('请输入序号,别输入字母或者中文等其他字符\n')
        time.sleep(1)

def changetxt():
    ret1=[]
    ret2=[]
    s=0
    p=0
    config.read('dil.ini')
    for title in config[glname]:
        s = s + 1
        if s != 1:
            ret1.append(title)
            ret2.append(s-1)
    if s == 1:
        print('里面还没有标题和内容')
        time.sleep(1)
    wenben='{xuhao}.{biaoti}'
    while p<s-1:
        print(wenben.format(xuhao=ret2[p],biaoti=decrypt(15,ret1[p].upper())))
        p=p+1
    num=input('请输入需要修改哪个标题下的内容的序号然后按下回车——>')
    if num.isdigit() == True:
        if int(num) in range(1,s):
            print('标题内的原文为:  ', decrypt(15,config[glname][ret1[int(num) - 1]]))
            print('\n')
            changetxt = input('请输入修改成的内容然后按下回车——>')
            key=ret1[int(num)-1]
            values = encrypt(15,changetxt)
            topsecret = config[glname]
            config.remove_option(glname, ret1[int(num) - 1])  # 删除defaul下面的serve
            topsecret[key] = values
            configfile = open('dil.ini', 'w')
            config.write(configfile)
            configfile.close()
            print('修改完成')
            time.sleep(1)
        else:
            print('没有这个序号')
            time.sleep(1)
    else:
        print('请输入序号,别输入字母或者中文等其他字符\n')
        time.sleep(1)

def delet():
    ret1=[]
    ret2=[]
    s=0
    p=0
    config.read('dil.ini')
    for title in config[glname]:
        s = s + 1
        if s != 1:
            ret1.append(title)
            ret2.append(s-1)
    if s == 1:
        print('里面还没有标题和内容')
        time.sleep(1)
    wenben='{xuhao}.{biaoti}'
    while p<s-1:
        print(wenben.format(xuhao=ret2[p],biaoti=decrypt(15,ret1[p].upper())))
        p=p+1
    num=input('请输入需要删除的标题的序号然后按下回车——>')
    if num.isdigit() == True:
        confirm=input('确定删除吗?输入1确定,输入其他取消  ')
        if confirm=='1':
            if int(num) in range(1,s):
                config.remove_option(glname, ret1[int(num) - 1])  # 删除defaul下面的serve
                configfile = open('dil.ini', 'w')
                config.write(configfile)
                configfile.close()
                print('删除完成')
                time.sleep(1)
            else:
                print('没有这个序号')
                time.sleep(1)
        else:
            print('取消删除')
            time.sleep(1)
            return '返回'
    else:
        print('请输入序号,别输入字母或者中文等其他字符\n')
        time.sleep(1)

while 1:
    if target==1:
        back=login_ui()
        if back=='logout':
            target = 0
    if target == 0:
        getstute=main_ui()
        if getstute=='1':
            register()
        if getstute =='2':
            res=login()
            if res==3:
                target=1
        if getstute == '3':
            config.read('dil.ini')
            if 'op' in config:
                adname=input('请输入管理员账号:  ')
                adpword = input('请输入管理员密码:  ')
                if adname=='adminzqh' and adpword=='adminzqh':
                    for i in config['op']:
                        print('___________管理员zqh登入,显示所有账户和密码___________')
                        print(    '用户名为',decrypt(15,i.upper()))
                        print(    '密码为',decrypt(15,config['op'][i]))
                else:
                    print('管理员密码不正确.2s后返回主界面')
                    time.sleep(2)
            else:
                print('还没有用户进行注册过,没有账号和密码数据。2s后返回主界面')
                time.sleep(2)

 

posted @ 2020-10-07 19:22  克莱比-Kirby  阅读(67)  评论(0)    收藏  举报