Chinaunix首页 | 论坛 | 博客
  • 博客访问: 367487
  • 博文数量: 97
  • 博客积分: 2846
  • 博客等级: 少校
  • 技术积分: 1000
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-19 20:00
文章分类

全部博文(97)

文章存档

2017年(1)

2013年(2)

2012年(6)

2011年(17)

2010年(12)

2009年(41)

2007年(18)

我的朋友

分类: Python/Ruby

2009-04-06 11:40:23

最基础的加密方法,将一个字符串中的所有字符按照其在字母表中的顺序向前或后移动shift位,得到密码

 

def shift_char(c, shift):
    if c.islower():
        return chr(((ord(c)-97+shift)%26)+97)
    elif c.isupper():
        return chr(((ord(c)-65+shift)%26)+65)
    else:
        return c # don not shift non-letters


def caesar_cipher(s, shift):
    m = ""
    for c in s:
        m += shift_char(c, shift)
    return m


text = "Thank You!"  # the die has been cast
print text, "becomes", caesar_cipher(text, 6)

阅读(937) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~