Python 栅栏凯撒

时间:2022-09-16 16:03:44
def fence_Crypto(msg,priority="row"):
    '''
    usage:
        fence_Crypto(msg[, priority])->msg to encrypt or decrypt with fence crypto
    args:
        msg:
            a plaintext which will be crypted or a ciphertext which will be decrypted
        priority:
            row priority or column priority,default is the former
    return:
        result:
            if msg is plaintext,it contains all of possible ciphertext.
            if msg is ciphertext,it contains all of possible plaintext
    '''
    msg_len = len(msg)
    row_num = 1
    result = []
    if priority == "row":
        while row_num<=msg_len:
            temp = [msg[block_num::row_num] for block_num in xrange(row_num)]
            result.append("".join(temp))
            row_num += 1
        return result
    elif priority == "columns":
        pass
    else:
        print "parameter error!please help(fence_Crypto)"
def caesar_Crypto(msg):
    '''
    usage:
        caesar_Crypto(msg) ---> to encrypt or decrypt the msg with caesar crypto
    '''
    lowercase = 'abcdefghijklmnopqrstuvwxyz'
    uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    result = []
    offset = 1
    while offset<=26:
        temp = []
        for char in msg:
            if char in lowercase:
                temp.append(chr(97 + (ord(char) - 97 + offset) % 26))
            elif char in uppercase:
                temp.append(chr(65 + (ord(char) - 65 + offset) % 26))
            else:
                temp.append(char)
        string = "".join(temp)
        print "[*]offset %d---------"%offset,string
        result.append(string)
        offset += 1
    #return result