首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >凯撒密码python加密

凯撒密码python加密
EN

Stack Overflow用户
提问于 2014-04-24 11:34:52
回答 1查看 717关注 0票数 1

如何修改,以便在加密时返回到"z“后返回"a”?

代码语言:javascript
复制
# ceaser cipher encoder
# by SaLiXa MoUdInHo aka Okhotnik

def ccipher():
    print "This is a program to compute the ceaser cipher encoding algorithm"
    # get the input from the user
    text = raw_input("Enter the text you want to encode: ")
    jmp = input("Input the numerical value of the key: ")
    # set up an accumulator
    lstmsg = ""
    for j in text:
        lstmsg = lstmsg + chr(ord(j) +jmp)
    # output the encrypted message
    print "[" + lstmsg + "]"
EN

回答 1

Stack Overflow用户

发布于 2014-11-17 21:41:41

Python源代码:

注:也适用于负移位数。 注意:如果反向移位,那么我们将进行编码-解码信息。 注:保留空间

代码语言:javascript
复制
small_chars = [chr(item) for item in range(ord('a'), ord('z')+1)]
upper_chars = [item.upper() for item in small_chars]

def encode_chr(chr_item, is_upper_case):
    '''
    Cipher each chr_item.
    '''
    # setting orig and end order.
    if is_upper_case:
        orig_ord = ord('A')
        end_ord  = ord('Z')
    else:
        orig_ord = ord('a')
        end_ord  = ord('z')

    # calculating shift    
    temp_ord = ord(chr_item)+shift
    # calculating offset order with modulo.
    # char is after end_ord, calculating offset
    num_of_chars = 26
    offset_ord = (temp_ord - end_ord - 1)%num_of_chars
    return chr(orig_ord + offset_ord)

# enable while loop to repeat until status not 'y'
status = 'y'
while status == 'y':
    # enter word to cipher.
    word = raw_input("Word: ")
    # enter char shift
    shift = input("Shift: ")
    print

    # create cipher list variable
    cipher = list()
    # loop trough each char in word
    for chr_item in word:
        # encode just letters.
        # replace non-alfa with underscore: "_"
        if chr_item in upper_chars or chr_item in small_chars:
            # set is_uppser_case to True for upper case chars.
            is_upper_case = (chr_item in upper_chars) and True
            # cipher char.
            temp_chr = encode_chr(chr_item, is_upper_case)
            # append ciphered char to list
            cipher.append(temp_chr)
        elif chr_item is ' ':
            cipher.append(chr_item)
        else:
            cipher.append('_')

    # print word
    print word
    # print ciphered word
    print ''.join(cipher)

    # repeat again for another word?
    status = raw_input("Repeat? [y|n]: ")
    print

测试用例:

代码语言:javascript
复制
>>> 
Word: aAzZ!@
Shift: 1

aAzZ!@
bBaA__
Repeat? [y|n]: y

Word: aAzZ@!
Shift: -1

aAzZ@!
zZyY__
Repeat? [y|n]: y

Word: aAzZ@$
Shift: 27

aAzZ@$
bBaA__
Repeat? [y|n]: y

Word: aAzZ%^
Shift: -27

aAzZ%^
zZyY__
Repeat? [y|n]: n

>>> 

输出:

注意:如果反向移位,那么我们将进行编码-解码信息。

代码语言:javascript
复制
>>>
Word: "Mary Had a Little    Lamb"
Shift: 1

"Mary Had a Little    Lamb"
_Nbsz Ibe b Mjuumf    Mbnc_
Repeat? [y|n]: y

Word: _Nbsz Ibe b Mjuumf    Mbnc_
Shift: -1

_Nbsz Ibe b Mjuumf    Mbnc_
_Mary Had a Little    Lamb_
Repeat? [y|n]: n

>>>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23267750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档