如何转换(' w' f')返回' b&#39 ;?

时间:2015-02-27 02:35:54

标签: python caesar-cipher

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

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)

3 个答案:

答案 0 :(得分: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'

我还建议将magic number 26替换为len(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)

答案 1 :(得分:0)

你改变了问题。

根据旧问题:如何转移(' w' f')返回' b'?

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

    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
    

答案 2 :(得分: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$