如何正确转换字符串而不是MutableString

2024-05-06 03:32:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把我的加密程序整合到聊天程序中。但是我不能用套接字传递可变字符串。那么,如何将可变字符串转换成正确的字符串呢?在

import sys
from UserString import MutableString

def GetMode():

while True:
    print '\n(E)ncrypt Or (D)ecrypt?'
    Mode = raw_input().lower()
    if Mode in 'e d encrypt decrypt'.split():
        return Mode
    else:
        print '\nEnter Proper Choice!'

def GetInput():
    while True:
        print '\n(T)ype Message Or (L)oad File?'
        Input = raw_input().lower()
        if Input in 't type'.split():
            Input = raw_input('>')
            return Input
        elif Input in 'l load'.split():
            MsgLoc = raw_input()
            MsgLoc = open(MsgLoc, 'r')
            try:
                Input = MsgLoc.read()
                MsgLoc.close()
                return Input
            except:
                print '\nCould Not Open' , MsgLoc
        else:
            print '\nEnter Proper Choice!'

def GetKey():
    while True:
        Key = 0
        print '\nPlease Enter A 20 Digit Number...\n** Do NOT use zeros!!!! EX-NAY ERO-ZAY! **'
        try:
            Key = int(input())
            Key = str(Key)
            if (len(Key) == 20):
                return Key
            else:
                print('\nPlease Enter A Valid Number!')
        except:
            print('\nPlease Enter A Valid Number!')

def Translate(Mode, Input, Key):
    if Mode[0] == 'e':
        print('\nEncrypting....')
        Encrypt(Input, Key)
    else:
        print('\nDecrypting....')
        Decrypt(Input, Key)

def Encrypt(Input, Key):
    Msg = MutableString()
    NonMutMsg = Input
    Msg += NonMutMsg
    MsgLen = len(Msg)
    CypherKey = Key
    a = 0
    b = 19

    #Loop For Proccessing Key
    for z in range(10):
        KeySkip = int(CypherKey[a])
        KeyIncrement = int(CypherKey[b])
        c = MsgLen/KeySkip
        d = -1

        #Loop To Skip Then Increment
        for y in range(c):
            d = d+KeySkip
            LtrNum = ord(NonMutMsg[d])
            LtrNum = LtrNum + KeyIncrement
            Msg[d] = chr(LtrNum)

Tags: key字符串ininputrawreturnifmode
1条回答
网友
1楼 · 发布于 2024-05-06 03:32:08

首先,正如MutableString documentation所述:

It should be noted that these classes are highly inefficient compared to real string or Unicode objects; this is especially the case for MutableString.

最重要的是:

The main intention of this class is to serve as an educational example for inheritance

换句话说,不要在真实的代码中使用这个类。在

要获取数据,请使用data参数:

pythonstring = mutablestring.data

相关问题 更多 >