:: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  111 随笔 :: 10 文章 :: 2680 评论 :: 0 Trackbacks

7-10. Encryption. Using your solution to the previous problem, and create a "rot13" translator. "rot13" is an old and fairly simplistic encryption routine whereby each letter of the alphabet is rotated 13 characters. Letters in the first half of the alphabet will be rotated to the equivalent letter in the second half and vice versa, retaining case. For example, a goes to n and X goes to K. Obviously, numbers and symbols are immune from translation.

(b) Add an application on top of your solution to prompt the user for strings to encrypt (and decrypt on reapplication of the algorithm), as in the following examples:

    % rot13.py
    Enter string to rot13: This is a short sentence.
    Your string to en/decrypt was: [This is a short
    The rot13 string is: [Guvf vf n fubeg fragrapr.].
    % rot13.py
    Enter string to rot13: Guvf vf n fubeg fragrapr.
    Your string to en/decrypt was: [Guvf vf n fubeg
    The rot13 string is: [This is a short sentence.].

 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0710.py 153 2010-06-21 04:19:15Z xylz $
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
10endic = None
11if not endic:
12    endic = {}
13    import string
14    for cc in (string.lowercase,string.uppercase):
15        for i,c in enumerate(cc):
16            if i<13: endic[c]=cc[i+13]
17            else: endic[c]=cc[i-13]
19def encrypt_decrypt(s):
20    ret=[]
21    for c in s:
22        ret.append(endic.get(c,c))
23    return "".join(ret)
25if __name__ == '__main__':
26    while True:
27        my_input = raw_input('Enter string to rot13: ')
28        if not my_input: break
29        print "Your string to en/decrypt was: [",encrypt_decrypt(my_input),"]."

©2009-2014 IMXYLZ |求贤若渴
posted on 2010-06-21 12:25 imxylz 阅读(17220) 评论(0)  编辑  收藏 所属分类: Python



©2009-2014 IMXYLZ