【问题标题】:How to loop a specific String Caesar Cipher vb10如何循环特定的字符串凯撒密码 vb10
【发布时间】:2016-01-22 06:57:25
【问题描述】:

我正在创建一个具有加密和解密功能的程序。问题是它应该只适用于代码上的静态字符串。当输入的纯文本是最后一个时,它应该循环。例如,当我输入 0 且密钥为 2 时,字符串为“ABCDE...Z1234567890”,密文应为 B。我有四个文本框用于密钥、输入、明文和密文。这是我的代码。

Public Class Form1
       Dim key As Integer
Const Intext As String = "abcdefghijklmnopqrstuvwxyz1234567890"
Private Sub btCipher_Click(sender As Object, e As EventArgs) Handles btCipher.Click

    Dim plain, s, r As String
    Dim i, j As Long
    key = Val(txtKey.Text)
    plain = txtInput.Text
    For i = 1 To Len(plain)
        r = Mid$(plain, i, 1)
        j = (InStr(1, Intext, r))
        If key > 36 Then
            s = s & Mid$(Intext, j + (key - 36), 1)
        Else
            s = s & Mid$(Intext, j + key, 1)
        End If

    Next i

    txtResult.Text = s
End Sub

Private Sub btDecipher_Click(sender As Object, e As EventArgs) Handles btDecipher.Click

    Dim plain, s, r As String
    Dim i, j As Long
    key = Val(txtKey.Text)
    plain = txtResult.Text
    For i = 1 To Len(plain)
        r = Mid$(plain, i, 1)
        j = (InStr(1, Intext, r))
        If key > 36 Then
            s = s & Mid$(Intext, j + (key - 36), 1)
        Else
            s = s & Mid$(Intext, j - key, 1)
        End If
    Next i

    txtText.Text = s
End Sub End Class

我的问题是它没有循环。也许有人可以帮忙。谢谢。

【问题讨论】:

  • key 变量是Intext 字符串中跳过的字符数吗?

标签: vb.net string loops encryption


【解决方案1】:

我不确定您的代码为什么不起作用,但请尝试以下代码:

Public Class Form1

    Const Intext As String = "abcdefghijklmnopqrstuvwxyz1234567890"

    Private Function Transcode(text As String, map As Dictionary(Of Char, Char)) As String
        Return New String(text.Select(Function(x) If(map.ContainsKey(x), map(x), x)).ToArray())
    End Function

    Private Sub btCipher_Click(sender As Object, e As EventArgs) Handles btCipher.Click
        Dim key = Val(txtKey.Text)
        Dim map = _
            Intext _
                .Zip(Intext.ToCharArray().Concat(Intext.ToCharArray()).Skip(key), Function(f, t) New With {f, t}) _
                .ToDictionary(Function(x) x.f, Function(x) x.t)
        txtResult.Text = Transcode(txtInput.Text, map)
    End Sub

    Private Sub btDecipher_Click(sender As Object, e As EventArgs) Handles btDecipher.Click
        Dim key = Val(txtKey.Text)
        Dim map = _
            Intext _
                .Zip(Intext.ToCharArray().Concat(Intext.ToCharArray()).Skip(key), Function(f, t) New With {f, t}) _
                .ToDictionary(Function(x) x.t, Function(x) x.f)
        txtText.Text = Transcode(txtResult.Text, map)
    End Sub

End Class

我已经测试了算法,它运行良好。

【讨论】:

  • 代码在循环中工作,但解密按钮不起作用?我有四个文本框,我在你的代码上看到了三个,我该如何破译?也许我们可以在解密结果中添加文本框。谢谢你。 :)
  • @WarrenRazon - 我直接从您的问题中获取了文本框。您可能需要检查是否需要更改我的答案中的任何一个以使其正常工作。您还会注意到.ToDictionary(Function(x) x.f, Function(x) x.t) 调用在两种方法上是相反的——一种是Function(x) x.f, Function(x) x.t,另一种是Function(x) x.t, Function(x) x.f。这允许解密器倒退。所以我认为您只需要正确设置文本框即可。
  • 好的,对不起。我错过了一个名字,它现在可以工作了。但是如果我必须在顶部添加一个新的文本框并作为字符串的引用呢? Const Intext As Double = txtString.Text 是我的代码不起作用,我该怎么办?
  • 我也用过这个,但它不能 Const Intext As String = "" + txtString.Text+ "" 它说 " 引用非共享成员需要一个对象引用”
  • @WarrenRazon - 是的,Const Intext As String = "" + txtString.Text+ "" 不能声明为 Const 是所有类的共享值,您正试图根据实例字段为其分配一个字符串。试试Private 而不是Const
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多