在VB2010中无法将文本分组为5个字母的块

时间:2014-08-02 08:21:36

标签: vb.net vbscript

我对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

1 个答案:

答案 0 :(得分:1)

  • 不需要SpaceCounter。摆脱与此相关的一切。只要你不碰柜台,你就没事了。只需执行以下操作:If Counter Mod 5 = 0 And Counter > 1 Then EncryptedText = EncryptedText & " "
  • 接下来,您将函数传递给字符串'MessageText',但引用控件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 + $”。