任何人都可以想到解决这个问题的方法

时间:2015-05-22 12:10:59

标签: vb.net console

我已经制作了这个代码,用于使用两个关键字加密/解密一个单词,我的加密工作,但我的解密不起作用。如果有人告诉我如何改变它,我真的很感激 这是两个。

 Sub encryption()
    Dim plain As String
    Dim keyword As String
    Dim keyword2 As String     ' declares these as variables(text)
    Dim encoded_message() As String


    Console.ForegroundColor = ConsoleColor.Red

    Console.Write("Enter your plaintext: ")
    plain = Console.ReadLine.ToUpper()      'stores input into plain variable and converts it into upper case

    Console.Write("Enter your keyword: ")
    keyword = Console.ReadLine.ToUpper()     'stores input into keyword variable and converts it into upper case

    Console.Write("Enter your second keyword: ")
    keyword2 = Console.ReadLine.ToUpper()     'stores input into keyword2 variable and converts it into upper case

    While plain.Length > keyword.Length
        keyword = keyword + keyword      'keyword cipher needs to make the keyword same length as the plaintext
    End While

    While plain.Length > keyword2.Length
        keyword2 = keyword2 + keyword2
    End While


    keyword = Asc(keyword)        'finds out ascii value of keyword

    keyword2 = Asc(keyword2)    'finds out ascii value of keyword2
    Console.ForegroundColor = ConsoleColor.Magenta

    Console.WriteLine("here is your encrypted message")

    ReDim Preserve encoded_message(0 To plain.Length - 1)
    For stepper As Integer = 0 To plain.Length - 1
        encoded_message(stepper) = Asc(plain(stepper)) + keyword + keyword2 - 96
        Console.Write(Chr(encoded_message(stepper)))

    Next
    Console.ReadLine()

End Sub

   Sub decrytpion()
    Dim plain As String
    Dim keyword As String
    Dim keyword2 As String
    Dim encoded_message() As Integer

    Console.ForegroundColor = ConsoleColor.Red

    Console.Write("Enter your plaintext: ")
    plain = Console.ReadLine.ToUpper()

    Console.Write("Enter your keyword: ")
    keyword = Console.ReadLine.ToUpper()

    Console.Write("Enter your second keyword: ")
    keyword2 = Console.ReadLine.ToUpper()

    While plain.Length > keyword.Length
        keyword = keyword + keyword
    End While

    While plain.Length > keyword2.Length
        keyword2 = keyword2 + keyword2
    End While

    keyword = Asc(keyword)        'finds out ascii value of keyword

    keyword2 = Asc(keyword2)    'finds out ascii value of keyword2

    Console.ForegroundColor = ConsoleColor.Magenta

    Console.WriteLine("here is your decrypted message")

    ReDim Preserve encoded_message(0 To plain.Length - 1)
    For stepper As Integer = 0 To plain.Length - 1
        encoded_message(stepper) = Asc(plain(stepper)) - keyword - keyword2 + 96
        Console.Write(Chr(encoded_message(stepper)))

    Next

    Console.ReadLine()

End Sub

1 个答案:

答案 0 :(得分:1)

在没有评论其稳健性的情况下,具体问题是您的编码可以生成小写字符作为输出,但是当您回读该数据以对其进行解码时,您强制它们全部为大写:plain = Console.ReadLine.ToUpper()完全改变他们的价值观。

删除ToUpper,它会按预期运行。