python編寫的維吉尼亞密碼加解密程序

維吉尼亞密碼錶算法

wKioL1Z1WwDBEaHWAAGY7UKfZSE837.jpg

=============================================ide

#維吉尼亞密碼  加密加密

key='helloworld'3d

plaintext='whereisthekey'ip


#key='relations'ci

#plaintext='tobeornottobeth'get


ascii='abcdefghijklmnopqrstuvwxyz'it

keylen=len(key)io

ptlen=len(plaintext)class

ciphertext = ''

i = 0

while i < ptlen:

    j = i % keylen

    k = ascii.index(key[j])

    m = ascii.index(plaintext[i])

    ciphertext += ascii[(m+k)%26]

    i += 1


print ciphertext

===================================================================

#維吉尼亞加密算法 解密

key='helloworld'

ciphertext='dlpcsegkshrij'


#key='relations'

#ciphertext='ksmehzbblk'


ascii='abcdefghijklmnopqrstuvwxyz'

keylen=len(key)

ctlen=len(ciphertext)

plaintext = ''

i = 0

while i < ctlen:

    j = i % keylen

    k = ascii.index(key[j])

    m = ascii.index(ciphertext[i])

    if m < k:

        m += 26

    plaintext += ascii[m-k]

    i += 1


print plaintext

相關文章
相關標籤/搜索