0
所以,即时制作一个简单的加密程序,这是该算法:加密程序串错误
- 例:炭:一个;键:10000
- 从控制数减去ASCII数字一零零零年至1097年= 9903
- 转换结果为十六进制,然后进入一个字符串9903 = 26AF
- 拆分六角导致成的两个数字26基团,AF
- 转换分裂的十六进制值为ASCII 26 = 38(&)AF = 175(¯)你的水库ult会是:&
问题是,有时(代码如下),根据加密密钥,加密结果出来所有磨损和解密功能将无法正常工作。我做了测试,我知道问题在于加密功能,但我不知道在哪里。下面的代码:
Option Strict On
Imports System.Numerics
Public Class MainF
'tick for random keys
Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
Private Function AsciiToChars(ByVal nums As IEnumerable(Of Integer)) As Char()
'converts ASCII value to char
Return (From c In nums Select Chr(c)).ToArray
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'set random key
KeyE.Text = CStr(GetRandomNumber(10000, 99999))
KeyD.Text = KeyE.Text
End Sub
Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
'generate a randomic number, for the random key
Return objRandom.Next(Low, High + 1)
End Function
Private Sub GenKey_Click(sender As Object, e As EventArgs) Handles GenKey.Click
'renew key
KeyE.Text = CStr(GetRandomNumber(10000, 99999))
Dim tmptxt As String
tmptxt = En.Text
En.Text = ""
En.Text = tmptxt
KeyD.Text = KeyE.Text
tmptxt = String.Empty
End Sub
Sub En_TextChanged(sender As Object, e As EventArgs) Handles En.TextChanged
'encrypt
If (String.IsNullOrEmpty(En.Text)) Then
Enres.Text = ""
Else
Dim key As Integer = CInt(KeyE.Text)
Dim chars() As Char = En.Text.ToCharArray
Dim ints(chars.Length) As Integer
Dim hex(chars.Length) As String
Dim fex As String = ""
Dim Mlist As New List(Of String)
For Loop0 As Integer = 0 To chars.Length - 1
ints(Loop0) = key - Asc(chars(Loop0))
hex(Loop0) = Conversion.Hex(ints(Loop0))
fex &= hex(Loop0)
Next
If fex.Length Mod 2 <> 0 Then 'Mod returns the remainder of a division calculation. It will be 0 if the number is even.
fex = "0" & fex 'This will change "6AF" to "06AF"
End If
For x As Integer = 0 To fex.Length - 1 Step 2
Mlist.Add(fex.Substring(x, 2))
Next
Dim fdec(CInt((fex.Length - 2)/2)) As Integer
Dim fstr As String
For y As Integer = 0 To fdec.Length - 1
fdec(y) = CInt(Val("&H" & Mlist(y)))
Next
fstr = AsciiToChars(fdec)
Enres.Text = fstr
End If
End Sub
Private Sub De_TextChanged(sender As Object, e As EventArgs) Handles De.TextChanged
'decrypt
If (String.IsNullOrEmpty(De.Text)) Then
DeRes.Text = ""
Else
Dim final As String = ""
Dim key As Integer
key = CInt(KeyD.Text)
Dim FSTR As String = De.Text
Dim chars() As Char = FSTR.ToCharArray
Dim hexsub(chars.Length) As String
Dim ints(chars.Length) As String
Dim finalhex As String
For loop1 As Integer = 0 To chars.Length - 1
ints(loop1) = CStr(Asc(chars(loop1)))
hexsub(loop1) = Hex(ints(loop1))
Next
finalhex = Join(hexsub, String.Empty)
If finalhex.Length Mod 4 = 0 Then
Dim newlist As New List(Of String)
For x As Integer = 0 To finalhex.Length - 1 Step 4
newlist.Add(finalhex.Substring(x, 4))
Next
Dim sourceNUM(newlist.Count) As Int32
Dim finalascii(newlist.Count) As Int32
Dim finalchar(newlist.Count) As Char
key = CInt(KeyD.Text)
For b As Int32 = 0 To newlist.Count - 1
sourceNUM(b) = Convert.ToInt32(newlist(b), 16)
finalascii(b) = key - sourceNUM(b)
If finalascii(b) >= 32 And finalascii(b) <= 255 Then
finalchar(b) = Chr(finalascii(b))
final &= finalchar(b)
Else : final = "Invalid Input"
End If
Next
DeRes.Text = final
Else
DeRes.Text = ""
End If
End If
End Sub
Private Sub KeyE_TextChanged(sender As Object, e As KeyEventArgs) Handles KeyE.KeyDown
'future idea, enter key will run encription sub
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
End If
End Sub
End Class
这不是加密;它只是某种编码。您可以尝试使用Base64或ROT13等标准。 – ntoskrnl
仅仅因为它并不复杂并不意味着它没有加密。 ['加密是对信息(或信息)进行编码的过程,窃听者或黑客无法阅读,但授权方可以。](http://en.wikipedia.org/wiki/Encryption) – tinstaafl
算法只是一个测试,我打算以后开发它。 –