关于VigenâRiver
vigenâre密码是一种经典的加密技术,可以追溯到16世纪。它是由法国外交官和加密摄影师布莱斯·德·维纳雷(Blaise deVigenâre)发明的,因此名称。它被称为“ chiffre ind - chiffrable,这意味着不可能的密码,直到19世纪英国Polymath Charles Charles Babbage在19世纪打破了它。
。怎么运行的
vigenâre密码是一个多型替代密码,它使用关键字来加密和解密消息。 Vigenâre密码背后的关键思想是使用多个Caesar密码
基于关键字的字母。假设我们有一条明文消息 consisting of letters: . We also have a keyword consisting of letters: , where .
To encrypt the message, we repeat the keyword until it matches the length of the plaintext. Let repeated until it matches the length of .
To encrypt each letter in the plaintext, we find the corresponding letter in the keyword where is the index of modulo . Then, we shift the letter by the index of in the alphabet. Let represent the encrypted message.
The mathematical representation of the encryption process is given by:
where is the -th letter of the encrypted message and .
To decrypt the message, we use a similar process. We repeat the keyword until it matches the length of the ciphertext. Let (repeated until it matches the length of ).
To decrypt each letter in the ciphertext, we find the corresponding letter in the keyword (where is the index of modulo ). Then, we shift the letter back by the index of in the alphabet. Let represent the decrypted message.
The mathematical representation of the decryption process is given by:
where is the -th letter of the decrypted message and .
Note that in these equations, we use modular arithmetic with since there are letters in the English alphabet. Additionally, we assume a simple mapping where is represented by , by , and so on.
例子
让我们使用“键”一词作为关键字,然后使用vigenârecipher加密纯粹的消息“ hello”。
首先,我们重复关键字“键”,以匹配明文“你好”的长度。因此,重复的关键字变为“ keyke”。
明文消息的相应数值表示“ hello”是:
Using the mathematical encryption formula
, we can calculate the encrypted values as follows:
Thus, the encrypted message is "RIJVS".
To decrypt the ciphertext "RIJVS" back to the original plaintext, we repeat the keyword "KEY" to match the length of the ciphertext. Thus, the repeated keyword becomes "KEYKE".
Using the mathematical decryption formula
, we can calculate the decrypted values as follows:
Thus, the decrypted message is "HELLO".
维登雷桌子
在Vigenere Cipher的情况下,加密/解密是一项繁琐的任务。 Vigenâre表,也称为VigenâreSquare或Tabula Recta,用于简化加密和解密过程。它由网格或矩阵组成,该网格或矩阵提供了使用Vigenâre密码加密和解密消息的系统方法。该表是通过将多个Caesar密码对齐在一起的,每个密码都由由关键字确定的不同移位值。表的行和列表示字母的字母,每个单元格包含使用Caesar Cipher组合行和列字母产生的字母。
代码
可以从这里下载加密解码脚本以及黑客脚本:
About Vigenère Cipher
The Vigenère cipher is a classic encryption technique that dates back to the 16th century. It was invented by a French diplomat and cryptographer named Blaise de Vigenère, hence the name. It was known as “le chiffre indéchiffrable,” which means “the indecipherable cipher,” and remained unbroken until British polymath Charles Babbage broke it in the 19th century.
How it works
The Vigenère cipher is a polyalphabetic substitution cipher that uses a keyword to encrypt and decrypt messages. The key idea behind the Vigenère cipher is to use multiple Caesar ciphers based on the letters of the keyword.
Let's say we have a plaintext message
To encrypt the message, we repeat the keyword until…
这是 encryption_decryption.py 的源代码:
def vigenere_cipher(text, keyword, mode):
result = ""
keyword_length = len(keyword)
keyword = keyword.upper()
key_index = 0
for i, char in enumerate(text):
if char.isalpha():
ascii_offset = ord('A') if char.isupper() else ord('a')
keyword_shift = ord(keyword[key_index % keyword_length]) - ord('A')
if mode == "decrypt":
keyword_shift = -keyword_shift # Reverse the shift for decryption
char = chr((ord(char) - ascii_offset + keyword_shift) % 26 + ascii_offset)
key_index+=1
result += char
return result
# Get mode
while True:
print('Do you want to (e)ncrypt or (d)ecrypt?')
response = input('> ').lower()
if response.startswith('e'):
action = 'encrypt'
break
elif response.startswith('d'):
action = 'decrypt'
break
print('Please enter the letter e or d.')
print("Enter the message.")
message = input('> ')
print("Enter the keyword.")
keyword = input('> ').upper()
# Perform encryption/decryption
result = vigenere_cipher(message, keyword, action)
# Display the result
print(f"Result: {result}")
vigenere_cipher函数根据提供的参数执行加密或解密操作。它初始化一个名为“结果”的空字符串来存储最终结果。它计算关键字的长度并将其转换为大写。
接下来,它通过文本输入中的每个字符迭代。如果字符是字母,它将确定它是大写还是小写,并相应地分配ASCII偏移。它根据关键字中的相应字母计算当前字符的移位值。如果将模式设置为“解密”,则逆转移动值。
然后,它通过减去ASCII偏移,添加关键字移位并将Modulo 26围绕字母包裹,将移位应用于当前字符。最后,它使用ASCII偏移将移位值转换回一个字符,并将其附加到结果字符串。 key_index会增加以确保循环使用关键字字符。
处理文本中的所有字符后,结果字符串包含加密或解密的消息,具体取决于模式。
代码提示用户输入加密或解密模式(加密或解密),并将响应存储在操作变量中。它一直要求输入直到提供有效模式为止。
然后,它要求用户输入消息和关键字,分别将它们存储在消息和关键字变量中。
使用提供的输入(消息,关键字和操作)调用Vigenere_cipher函数,结果存储在结果变量中。
行动程序
这是我们执行程序时获得的示例输出:
Do you want to (e)ncrypt or (d)ecrypt?
> e
Enter the message.
> Enemy is approaching! Send troops immediately!
Enter the keyword.
> Moonlight
Result: Qbszj qy hibfcnnpouz! Esbq ezuvie wazplohmqzm!
Do you want to (e)ncrypt or (d)ecrypt?
> d
Enter the message.
> Qbszj qy hibfcnnpouz! Esbq ezuvie wazplohmqzm!
Enter the keyword.
> Moonlight
Result: Enemy is approaching! Send troops immediately!
这里使用的密钥是月光。
结论
有几种方法可以破解Vigenâre密码,例如Kasiski检查,Friedman测试,频率分析,字典攻击等。但是,这些是高级方法,需要复杂的组合方法和统计数据。甚至讨论其中一个都需要单独的帖子。目前,我们将专注于加密和解密。我一定会在将来发布一个vigenâre黑客教程。
注意:vigenâre密码虽然复杂,但仍然太容易使用现代方法,不应用于严重的加密目的。