密码学学习(一) Python实现两个简单的加密算法

字符串倒序输出

顾名思义= =|

# !python3.3

def stringReverse(s):
    """
    @param s: string
    @return: reversed string
    @example:
    stringReverse('abc')
    output cba
    """
    return s[::-1]  #start:end:step


凯撒密码

把字母替换为之后第key个字母,如key=3的话则
a -> d
b -> e
c -> f
...
# !python3.3

def caesarCipher(s, key):
    """
    @param s: text needs encrypt/decrypt
    @param key: positive if encryption, negative if decryption
    @return: the encrypted or decrypted text   
    """
    import string
    a = string.ascii_lowercase
    b = string.ascii_uppercase
    key = key % 26      #key must be within 0 - 25
    ta = a[key:] + a[:key]
    tb = b[key:] + b[:key]
    table = s.maketrans(a+b, ta+tb)
    return s.translate(table)


Bouns

http://www.blisstonia.com/software/WebDecrypto/index.php

只要是替换密码(就是简单地把一个字母替换成另一个字母的加密方式),这个网站一定能给你一个美妙的答案 :)

http://www.ssynth.co.uk/~gay/anagram.html

http://wordsmith.org/anagram/

把字母重新组成单词,例如把 rehipc 重组成 cipher

你可能感兴趣的:(密码学,日记)