假设您的字母表的键长度相同,您可以执行一个 FOR 循环来检查字符串的每个字符(该字符如何与您的键相关)。
例如,您的消息“你好”。
这不是实际代码,只是为了演示您所要求的概念:
for each letter in message
position_in_alphabet = current_letter
cipher_letter = key(position_in_alphabet)
append cipher_letter to cipher_message
例如,“你好”是 5 个字符。所以这个循环会循环5次。字母表中的位置是(8th, 5th, 12th, 12th, 15th)。您插入与密钥相关的那些,您会得到“qlnnr”(或者密钥指示)。
把它们放在一起,它看起来像:
Dim _message As String = "hello"
Const _plain As String = "abcdefghijklmnopqrstuvwxyz"
Const _key As String = "kxgtlmpqbwcnderfahjusviyoz"
Dim charPos As Integer = 0
Dim Cipher As String = ""
For i = 0 To _message.Length - 1
charPos = _plain.IndexOf(_message(i))
Cipher = Cipher & _key(charPos)
Next i
反过来,查找密码相对于密钥的位置,然后将其放入明文字典中,如下所示:
Dim _cipher As String = "qlnnr"
Const _plain As String = "abcdefghijklmnopqrstuvwxyz"
Const _key As String = "kxgtlmpqbwcnderfahjusviyoz"
Dim charPos As Integer = 0
Dim Message As String = ""
For i = 0 To _message.Length - 1
charPos = _key.IndexOf(_cipher(i))
Message = Message & _plain(charPos)
Next i