我对VB比较陌生,我正在尝试开发一个简单的加密程序。我已将大部分代码钉在了但为了使我的加密程序更好,我想将文本分组为5个字母的块。但是,我执行此操作的功能不起作用。我怀疑我的IF或循环有问题。
Function ExtendedEncrypt(ByVal MessageText As String, ByVal txtOffset As Integer) As String
Dim StringLength As String
Dim CharToEncrypt As Char
Dim EncryptedText As String
Dim CharAscNumber As Integer
Dim CipherText As String
Dim Counter As Integer
StringLength = Len(MessageText)
CipherText = ""
For Counter = 1 To StringLength
CharToEncrypt = Mid(MessageText, Counter, 1)
If CharToEncrypt = " " Then
EncryptedText = ""
Else : CharAscNumber = Asc(CharToEncrypt)
CharAscNumber = CharAscNumber + txtOffset
If CharAscNumber > 126 Then
CharAscNumber = CharAscNumber - 94
End If
EncryptedText = Chr(CharAscNumber)
If Counter Mod 5 = 1 And Counter > 1 Then EncryptedText = EncryptedText & " "
End If
CipherText = CipherText & EncryptedText
txtEncrypted.Text = CipherText
Next
Return CipherText
End Function
答案 0 :(得分:1)
SpaceCounter
。摆脱与此相关的一切。只要你不碰柜台,你就没事了。只需执行以下操作:If Counter Mod 5 = 0 And Counter > 1 Then EncryptedText = EncryptedText & " "
txtMessage
。将对txtMessage.text
的所有引用更改为MessageText
。txtEncrypted.Text = CipherText
。最终推荐结果(您可以删除评论,即'texttext)
Function ExtendedEncrypt(ByVal MessageText As String, ByVal txtOffset As Integer) As String
'Removed a bunch of unnecessary variables
Dim CipherText As String
Dim CharAscNumber As Integer
Dim Counter As Integer
'Condensed this section down. Removed/replaced references to EncryptedText
For Counter = 1 To Len(MessageText)
CharAscNumber = Asc(Mid(MessageText, Counter, 1)) + txtOffset '+Counter (optional, remember to account for this in decryption)
If CharAscNumber > 126 Then CharAscNumber = CharAscNumber - 94 'Consider -126
CipherText = CipherText & Chr(CharAscNumber)
If Counter Mod 5 = 0 And Counter > 1 Then CipherText = CipherText & " "
Next
Return CipherText
End Function
使用此“我们去公园”,偏移23返回“n | 7~(7-(7-!| 7)x + $”。