In [15]:
#输入:一个txt文件(假设全是字母的英文词,每个单词之间用单个空格隔开,假设单词最长为10个字母)
#1.建立一个英文文档($)


#加密:得到每个单词的长度n,随机生成一个9位的数字,将n-1与这个9位的数字连接,形成一个10位的数字,作为密匙key。($)
#2.先获取一个9位key,然后获取每个单词的长度,得到n-1为长度位加密,拼接10位key
#9位key并不相同

#依照key中各个数字对单词中每一个对应位置的字母进行向后移位
#3.后移字母,获取字母的ASCII加上对应的加密值,求得新的字母,再拼接成原来的单词
#(例:如过key中某数字为2,对应该位置的字母为a,加密则应移位成c,如果超过z,则回到A处继续移位),#注意有循环移位的过程!!!

#对长度不到10的单词,移位后,将移位后的单词利用随机字母补全到10个,最终形成以10个字母为一个单词,
#4.补全,取随机字母,拼接单词


#并以单个空格分割的加密文本,存入文件。
#5.十个一分

#解密:给定该文本文件并给定key(10位数字),恢复原来的文本。
#9位key加上每个单词对应的长度-1,10个一输出,空格为间隔符

#(提示,利用ord()及chr()函数,ord(x)是取得字符x的ASCII码,chr(n)是取得整数n(代表ASCII码)对应的字符。
 #例:ord(a)的值为97,chr(97)的值为'a',因字母a的ASCII码值为97。)
import random

def encode(filename,code_filename,encode_filename):
    keys = []
    new_words = []
    with open(filename) as fh:
        text = fh.read()
    words = text.split()
    for word in words:
        new_word =''
        nine_key = random.randint(100000000,1000000000)
        length = len(word)
        last_key = length -1

        key = nine_key * 10 + last_key
        keys.append(key)
        i = 9
        for ch in word:
            shift = key // (10**i)
            key = key % (10**i)
            i -= 1
            old_num = ord(ch)
            new_num = old_num + shift
            if old_num <= 90 and new_num >90:  #从小写跳到大写的部分
                temp = new_num - 90
                new_num = 97 + temp - 1
            elif 97 <= old_num and old_num <=122 and new_num > 122:
                temp = new_num - 122    #从大写调回小写的部分
                new_num = 65 + temp - 1

            #A-Z (65-90)  a-z(97-122)
            new_ch = chr(new_num) 
            new_word =new_word + new_ch
        if length < 10:
            for i in range(10-length):
                new_word = new_word + chr(random.randint(97,122))
        new_words.append(new_word)

    filehandle1 = open(code_filename,'w')
    for k in keys:
        filehandle1.write(str(k) + ' ')
    filehandle1.close()

    filehandle2 = open(encode_filename,'w')
    for encode_word in new_words:
        filehandle2.write(encode_word + ' ')
    filehandle2.close()
    
    

def decode(encode_filename,code_filename,decode_filename):
    #keys = []
    keys = []
    cut_words = []
    new_words = []
    lengths = []
    with open(encode_filename) as fh:
        text = fh.read()
    words = text.split()
    with open(code_filename) as code_filehandle:
        code_text = code_filehandle.read()
    codes = code_text.split()
    for code in codes:
        key = int(code)
        keys.append(key)
        length = key %10 + 1
        #print(length)
        lengths.append(length)   
    i = 0     
    for word in words:
        cut_word =''
        cut_word = word[0:lengths[i]:]
        i += 1
        cut_words.append(cut_word)   
    j = 0
    for word in cut_words:
        new_word = ''
        m = 9
        k = int(keys[j])
        j += 1
        for ch in word:  
            #问题是还要获得偏移量
            shift = k // (10**m)
            k = k % (10**m)
            m -= 1
            old_num = ord(ch)
            new_num = old_num - shift
            if old_num <= 90 and new_num <65:  #从小写跳到大写的部分
                temp = 65 - new_num
                new_num = 122 - temp + 1
            elif 97 <= old_num and old_num <=122 and new_num < 97:
                temp = 97 - new_num    #从大写调回小写的部分
                new_num = 90 - temp + 1

            #A-Z (65-90)  a-z(97-122)
            new_ch = chr(new_num) 
            new_word =new_word + new_ch
        new_words.append(new_word)
        
    filehandle1 = open(decode_filename,'w')
    for newword in new_words:
        filehandle1.write(newword + ' ')
    filehandle1.close()

    
filename = r'D:\python\document.txt'
code_filename = r'D:\python\code.txt'
encode_filename = r'D:\python\encode.txt'
decode_filename = r'D:\python\decode.txt'
encode(filename,code_filename,encode_filename)
decode(encode_filename,code_filename,decode_filename)

In [ ]: