带密钥和消息的加密器 - VB.Net

时间:2013-11-21 21:36:09

标签: vb.net loops encryption for-loop

好的,所以我试图让我的程序接受你的密钥,并遍历密钥的每个字符,然后找到每个字符代码的ascii代码,然后循环遍历消息的每个字符,找到每个ascii代码,并将密钥代码添加到消息代码中,对密钥中的每个字符执行此操作,对消息中的每个字符执行此操作。我遇到了一个小问题,它在添加第一个字母后立即更改了消息,我无法弄清楚如何修复它,任何帮助都会很棒!

基本上,我想要的是关键字符的ascii代码并将它们添加到消息字符的ascii代码中,然后将最终代码转换回消息文本中的新字符。使用这个:

tbxMessage.Text = (AscW(Mid(tbxMessage.Text, xForMess, 1)) + AscW(vTemp))

这是我到目前为止所做的一切:

Public Class Form1

Function fctEncryptDecrypt(pMess As String, pKey As String) As String
    If Len(tbxMessage.Text) > 0 Then
        Dim xForKey As Integer
        Dim xForMess As Integer
        Dim intKey As Integer
        Dim intMessage As Integer
        Dim strAsciiKeyChar As String
        Dim intAsciiKeyChar As Integer
        Dim strAsciiMesChar As String
        Dim intAsciiMesChar As Integer
        Dim vTemp As String
        Dim vTempMess As String

        intKey = Len(tbxKey.Text)
        intMessage = Len(tbxMessage.Text)
        For xForKey = 1 To intKey
            strAsciiKeyChar = Mid(tbxKey.Text, xForKey, 1)
            intAsciiKeyChar = AscW(strAsciiKeyChar)
            vTemp = intAsciiKeyChar
            For xForMess = 1 To intMessage
                strAsciiMesChar = Mid(tbxMessage.Text, xForMess, 1)
                intAsciiMesChar = AscW(strAsciiMesChar)
                vTempMess = vTemp + intAsciiMesChar
                tbxMessage.Text = (AscW(Mid(tbxMessage.Text, xForMess, 1)) + AscW(vTemp))
            Next xForMess
        Next xForKey
        Label1.Text = vTemp

    Else
        MessageBox.Show("No Message Found")
    End If
End Function

Private Sub btnEncrypt_Click(sender As System.Object, e As System.EventArgs) Handles btnEncrypt.Click
    fctEncryptDecrypt(tbxMessage.Text, tbxKey.Text)
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

tbxMessage.Text = (AscW(Mid(tbxMessage.Text, xForMess, 1)) + AscW(vTemp))
内部For循环中的

是将文本框的值设置为数字。

我希望使用一个收集

的临时字符串变量
Chr((intAsciiKeyChar + intAsciiMesChar) mod 256)

我还希望密钥一次只应用一个字母。类似的东西:

Dim i as Integer
Dim s as String
Dim sKey as String
Dim sMesg as String
Dim intCharacter as Integer
s = ""
For i = 1 to len(tbxMessage.Text)
    sKey = Mid(tbxKey.Text, (i mod Len(tbxKey)) + 1, 1)
    sMesg = Mid(tbxMessage.Text, i, 1)
    intCharacter = (WAsc(sKey) + WAsc(sMesg)) mod 256
    s = s & Chr(intCharacter)
Next
tbxMessage.Text = s