解読/暗号化 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$