python 维吉尼亚

时间:2023-03-09 08:03:13
python 维吉尼亚
加密key='COMPUTER'
plaintext='BLOCKCIPHERDESIGNPRINCIPLE'
ascii='abcdefghijklmnopqrstuvwxyz'.upper()
keylen=len(key)
ptlen=len(plaintext)
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