0

使用这个程序取出空格、标点符号,并使字母小写...

def pre_process(s): #Enter: "Jim's secret password."

    s= s.replace("'","")
    s= s.replace('.','')
    s= s.lower()
    s= s.replace(" ","")
    return s

如何加密消息,以使每个字母的移位量等于字母表中的相应字母?例如m移动 5 次变为r,但w移动 5 次变为b。这是我当前的代码:

def shift(ch,k):    
    return chr(ord('a')+(ord(ch)-ord('a')+k) % 26)
4

3 回答 3

1
def shift(ch, k):
    return chr(ord('a') + ((ord(ch) - ord('a')) + 
                           (ord(k) - ord('a'))) % 26)

一种解释:

def shift(ch, k):
    #
    # k_delta
    # ────>
    #
    # ch_delta                 k_delta
    # ────────────────────────>────>
    # a....f.........m....r....w..zab
    # ──────────────>────>         ┊
    # ch_delta       k_delta       ┊
    #                              ┊
    #                             %26

    ch_delta = ord(ch) - ord('a')
    k_delta = ord(k) - ord('a')
    return chr(ord('a') + (ch_delta + k_delta) % 26)

除非k有所不同,否则您可以使用它str.translate来加速加密:

import string
message = 'mw'
key = 'f'
enc_table = string.maketrans(
    string.ascii_lowercase,
    ''.join(shift(c, key) for c in string.ascii_lowercase)
)
message.translate(enc_table) # -> 'rb'

我还建议用例如替换幻数 26len(string.ascii_lowercase)

可以使用相同的函数进行解密,但使用不同的密钥。他们之间的关系是这样的enc_delta + dec_delta = 0 modulo 26。由此得出结论dec_delta = -enc_delta % 26。所以:

dec_k = chr(ord('a') + ((ord(enc_k) - ord('a'))) % 26)
于 2015-02-27T05:23:21.387 回答
0

解密/加密 Python

代码:

def shift(ch, k):
    return chr(ord('a') + ((ord(ch) - ord('a')) + (ord(k) - ord('a'))) % 26)


def reshift(ch, k):
    tmp =  (ord(ch) - (ord(k) - ord('a')))
    if tmp<ord('a'):
        tmp = ord("a") +(26 - (ord("a") - tmp))
    return chr(tmp)

print "w and f:"
re = shift("w", "f")
print "Encryption:", re
re = reshift(re, "f")
print "Decrytion:", re

print "----------"
print "e and b"
re = shift("e", "b")
print "Encryption:", re
re = reshift(re, "b")
print "Decrytion:", re

print "----------"
print "z and b"
re = shift("z", "b")
print "Encryption:", re
re = reshift(re, "b")
print "Decrytion:", re

print "----------"
print "x and b"
re = shift("x", "b")
print "Encryption:", re
re = reshift(re, "b")
print "Decrytion:", re

输出:

vivek@vivek:~/Desktop/stackoverflow$ python 26.py 
w and f:
Encryption: b
Decrytion: w
----------
e and b
Encryption: f
Decrytion: e
----------
z and b
Encryption: a
Decrytion: z
----------
x and b
Encryption: y
Decrytion: x
vivek@vivek:~/Desktop/stackoverflow$ 
于 2015-02-27T19:09:08.300 回答
0

你换了个问题。

按照老问题:如何让 shift('w','f') 返回 'b'?

  1. 从 alpha 获取第二个参数的差异a,代码是diff = a2-start
  2. 通过添加差异从第一个参数中获取下一个值,代码是next = a1+diff
  3. 检查下一个值是否大于 alpha z
  4. 如果不大于则z新值是下一个值。
  5. 如果更大,则从 alpha 获取新值a
  6. 返回值字符。

代码:

def shift(ch,k):
    start = ord("a")
    end = ord("z")
    a1 = ord(ch)
    a2 = ord(k)

    diff = a2-start
    print "\ndiff:", diff
    next = a1+diff
    print "next value:", next
    if next>end:
        new = next-end-1
        print "new1:", new
        new = start + new  
    else:
        new = next

    print "new value:", new

    return chr(new)


rs = shift("w", "f")
print "w and f:", rs

rs = shift("e", "b")
print "e and b:", rs

rs = shift("z", "b")
print "z and b:", rs

rs = shift("x", "b")
print "x and b:", rs

输出:

vivek@vivek:~/Desktop/stackoverflow$ python 26.py 

diff: 5
next value: 124
new1: 1
new value: 98
w and f: b

diff: 1
next value: 102
new value: 102
e and b: f

diff: 1
next value: 123
new1: 0
new value: 97
z and b: a

diff: 1
next value: 121
new value: 121
x and b: y
vivek@vivek:~/D
于 2015-02-27T03:21:36.653 回答