Python rot13加密

http://zh.wikipedia.org/wiki/ROT13

 

 1 def rot13(s, OffSet=13):

 2      def encodeCh(ch):

 3          f=lambda x: chr((ord(ch)-x+OffSet) % 26 + x)

 4          return f(97) if ch.islower() else (f(65) if ch.isupper() else ch)

 5      return ''.join(encodeCh(c) for c in s)

 6  

 7  s='Hello!'

 8  print rot13(s)              # Uryyb!

 9  print rot13(rot13(s))       # Hello!

10  print rot13(s,26)           # Hello!

 

1 def rot13_2(s,offSet=13):

2     d={chr(i+c) : chr((i+offSet) % 26 + c) for i in range(26) for c in (65,97)}

3     return ''.join([d.get(c, c) for c in s])

 

 

你可能感兴趣的:(python)