In [1]:
import re

In [2]:
def mapSletter():
    letter = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'
    return letter.split(',')

In [3]:
def mapBletter():
    letter = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'
    return letter.split(',')

In [4]:
letterSList = mapSletter()

In [5]:
letterBList = mapBletter()

Caesar

空白和底線會跳過 英文大小寫皆可加解密,若出現空白底線英文以外的字元會跳出提示


In [6]:
# Caesar加密
def CaesarEncryption(cipher,shifting):    
    encryption =''
    for t in cipher:
        # 遇到 空白和底線不加密
        if t ==' ' or t=='_' or t=='_':
            encryption += t
        else:
            # 小寫加密
            if re.match(r'[a-z]', t) != None:
                cipherNum = letterSList.index(t)
                cipherShiftingNum = (cipherNum+shifting)%26
                encryption += letterSList[cipherShiftingNum]
            # 大寫加密
            elif re.match(r'[A-Z]', t) != None:
                cipherNum = letterBList.index(t)
                cipherShiftingNum = (cipherNum+shifting)%26
                encryption += letterBList[cipherShiftingNum]
            # 超出可加密的範圍
            else:
                print(t+'\n'+'無法加密'+'\n'+'請輸入英文(大小寫皆可) or 空白 底線皆可加密')
                return None
    return encryption

In [7]:
# Caesar解密
def CaesarDecrypt(cipher,shifting):    
    decrypt=''
    for t in cipher:
        # 遇到 空白和底線不解密
        if t ==' ' or t=='_' or t=='_':
            decrypt += t
        else:
            # 小寫解密
            if re.match(r'[a-z]', t) != None:
                cipherNum = letterSList.index(t)
                cipherShiftingNum = (cipherNum-shifting)%26
                decrypt +=letterSList[cipherShiftingNum]
            # 大寫解密
            elif re.match(r'[A-Z]', t) != None:
                cipherNum = letterBList.index(t)
                cipherShiftingNum = (cipherNum-shifting)%26
                decrypt +=letterBList[cipherShiftingNum]
            # 超出可解密的範圍
            else:
                print(t+'\n'+'無法解密'+'\n'+'請輸入英文(大小寫皆可) or 空白 底線皆可解密')
                return None
    return decrypt

In [8]:
s = 'A a_nnersmakethmaN'

In [9]:
# 測試Caesar加密
CaesarEncryption(s,3)


Out[9]:
'D d_qqhuvpdnhwkpdQ'

In [10]:
# 測試Caesar解密
CaesarDecrypt(CaesarEncryption(s,3),3)


Out[10]:
'A a_nnersmakethmaN'

virginia

空白和底線會跳過 英文大小寫皆可加解密,若出現空白底線英文以外的字元會跳出提示


In [11]:
# 產生秘鑰
def virginiaKeyPro(cipher,key):
    keyTmp = key * ((len(cipher)/len(key))+1)
    return keyTmp[:len(cipher)]

In [12]:
# virginia加密
def virginiaEncryp(cipher,key):
    virginiaKey = virginiaKeyPro(cipher,key)
    encryp=''
    for a,b in zip(cipher,virginiaKey):
        if a ==' ' or a=='_' or a=='_':
            encryp+=a
        else:
            if re.match(r'[a-z]', a) != None:
                if re.match(r'[a-z]', b) != None:
                    keyIndex = letterSList.index(b)
                elif re.match(r'[A-Z]', b) != None:
                    keyIndex = letterBList.index(b)
                cipherIndex = letterSList.index(a)
                encrypLetter = letterSList[(keyIndex+cipherIndex)%26]
                encryp+=encrypLetter
            elif re.match(r'[A-Z]', a) != None:
                if re.match(r'[a-z]', b) != None:
                    keyIndex = letterSList.index(b)
                elif re.match(r'[A-Z]', b) != None:
                    keyIndex = letterBList.index(b)
                cipherIndex = letterBList.index(a)
                encrypLetter = letterBList[(keyIndex+cipherIndex)%26]
                encryp+=encrypLetter                
            else:
                print('無法加密'+'\n'+'請輸入英文(目前只提供小寫) or 空白 底線皆可加密')
                return None  
    return encryp

In [13]:
# virginia解密
def virginiaDecrypt(cipher,key):
    virginiaKey = virginiaKeyPro(cipher,key)
    decrypt = ''
    for a,b in zip(cipher,virginiaKey):
        if a ==' ' or a=='_' or a=='_':
            decrypt+=a
        else:
            if re.match(r'[a-z]', a) != None:
                if re.match(r'[a-z]', b) != None:
                    keyIndex = letterSList.index(b)
                elif re.match(r'[A-Z]', b) != None:
                    keyIndex = letterBList.index(b)
                cipherIndex = letterSList.index(a)
                # Ci = (Pi + Ki) mod26
                decryptLetter = letterSList[( cipherIndex-keyIndex+26 )%26]
                decrypt+=decryptLetter
            elif re.match(r'[A-Z]', a) != None:
                if re.match(r'[a-z]', b) != None:
                    keyIndex = letterSList.index(b)
                elif re.match(r'[A-Z]', b) != None:
                    keyIndex = letterBList.index(b)
                cipherIndex = letterBList.index(a)
                # Pi = (Ci - Ki) mod26 加26是為了把負數導正
                decryptLetter = letterBList[( cipherIndex-keyIndex+26 )%26]
                decrypt+=decryptLetter                
            else:
                print('無法解密'+'\n'+'請輸入英文(目前只提供小寫) or 空白 底線皆可解密')
                return None                
    return decrypt

In [14]:
Cypher = "attackatdA_"
key = "lemon"

In [15]:
# 測試virginia加密
virginiaEncryp(Cypher,key)


Out[15]:
'lxfopvefrN_'

In [16]:
# 測試virginia加密
virginiaDecrypt(virginiaEncryp(Cypher,key),key)


Out[16]:
'attackatdA_'

In [17]:
# reference 
# wiki --- http://zh.wikipedia.org/wiki/%E7%BB%B4%E5%90%89%E5%B0%BC%E4%BA%9A%E5%AF%86%E7%A0%81

In [18]:
# github https://github.com/wy36101299/Vigen-re-cipher