如何使用变量在VB.NET中加密和解密数据 [英] How to encrypt and decrypt data in VB.NET by using variable

查看:91
本文介绍了如何使用变量在VB.NET中加密和解密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vb.net中创建一个项目加密和解密数据

如何使用此代码作为函数请帮助它只在一个文本框上工作

i想要发送一个字符串值然后返回加密值,当我发送加密值然后发送给我解密值。



我尝试过:



I create a project encrypt and decrypt data in vb.net
how to use this code as a function please help it is work only on a textbox
i want to send a string value then return encrypt value and when i send encrypt value then send me decrypt value.

What I have tried:

Private arLetterChars() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 "
    Private arEncryptedChars() As Char = "***********************************"










'// encrypt.
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button4.Click
       With TextBox1
           For Each myTextBoxChar As Char In .Text '// loop thru TextBox, one char. at a time.
               For i As Integer = 0 To arLetterChars.Length - 1 '// loop thru all letters in the Array.
                   '// if TextBox char ='s the char in your Array, replace the TextBox char with the same #'ed Array char of the Encrypted letters.
                   If myTextBoxChar = arLetterChars(i) Then .Text = .Text.Replace(myTextBoxChar, arEncryptedChars(i))
               Next
           Next
       End With
   End Sub







'// decrypt.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    With TextBox1
        For Each myTextBoxChar As Char In .Text '// loop thru TextBox, one char. at a time.
            For i As Integer = 0 To arEncryptedChars.Length - 1 '// loop thru all Encrypted char.s in the Array.
                '// if TextBox char ='s the char in your Array, replace the TextBox char with the same #'ed Array char of the Letters.
                If myTextBoxChar = arEncryptedChars(i) Then .Text = .Text.Replace(myTextBoxChar, arLetterChars(i))
            Next
        Next
    End With
End Sub

推荐答案

您是否意识到这不起作用?

有几个原因:

1)如果代码与您显示的完全相同,那么加密方法的输出将是所有'*'字符 - 并且您无法在任何情况下对其进行解密以获取原始输入。

2)如果你更换了将它们更改为'*'的字符,那么我们就不能破坏你的加密,然后1)消失 - 但它仍然无效。假设我在文本框中输入abc,你将'a'改为'c','b'改为'd','c'改为'e'?你将用'c'替换我的'a':

You do realize that won't work?
There are a couple of reasons:
1) If the code is exactly as you show, then the output of the encryption method will be all '*' characters - and you cannot under any circumstances decrypt that to get the original input.
2) If you have replaces the characters you change them to to '*' so we can't "break" your encryption, then 1) goes away - but it still won't work. Suppose I type "abc" in the text box, and you change 'a' to 'c', 'b' to 'd', and 'c' to 'e'? You will replace my 'a' with a 'c':
abc ==> cbc

我的'b'带'd'

my 'b' with a 'd'

cbc ==> cdc

和我的'c'带'e':

and my 'c' with an 'e':

cdc ==> ede



当你解密时,你不能说'e'应该是不同的角色......



此外,这不是一个好的加密 - 它是一个基本的替代密码,他们每个月都会打印杂志,所以很少有老太太可以在业余时间做这些......



加密不应该对字符起作用,它应该在字节上工作 - 它应该使用基于密钥的算法而不是固定的替换! :笑:


When you decrypt that, you can't tell that the 'e's should be different characters...

And besides, this isn't good encryption - it's a basic substitution cypher, and they print magazines full of those every month so little old ladies can do them in their spare time...

Encryption shouldn't work on characters, it should work on bytes - and it should use a key-based algorithm instead of a fixed substitution! :laugh:


参见 https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/walkthrough-encrypting-and-decrypting-strings [ ^ ]


这篇关于如何使用变量在VB.NET中加密和解密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆