16,554
社区成员
发帖
与我相关
我的任务
分享
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
'Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
'写ini
Friend Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
End Function
'读ini
Friend Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
Dim Str As String = ""
Str = LSet(Str, 256)
GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function
Friend Function MD5EncryptProc(ByVal strData As String) As String
Dim MD As New System.Security.Cryptography.MD5CryptoServiceProvider
Try
Return System.Text.Encoding.Default.GetString(MD.ComputeHash(System.Text.Encoding.Default.GetBytes(strData.Trim)))
Catch ex As Exception
Return ""
End Try
End Function
'文本加密
Friend Function EnText(ByVal StrText As String, ByVal strKey As String) As String
'Text = Text.ToLower
Try
Dim Des As New DESCryptoServiceProvider
Dim inputByteArray() As Byte
inputByteArray = Encoding.Default.GetBytes(StrText)
Des.Key = ASCIIEncoding.ASCII.GetBytes(Left(MD5EncryptProc(strKey), 8).PadRight(8))
Des.IV = ASCIIEncoding.ASCII.GetBytes(Left(MD5EncryptProc(strKey), 8).PadRight(8))
Dim MS As New System.IO.MemoryStream
Dim CS As New CryptoStream(MS, Des.CreateEncryptor, CryptoStreamMode.Write)
CS.Write(inputByteArray, 0, inputByteArray.Length)
CS.FlushFinalBlock()
Dim Ret As New StringBuilder
For Each b As Byte In MS.ToArray()
Ret.AppendFormat("{0:X2}", b)
Next
Return Ret.ToString()
Catch ex As Exception
Return ""
End Try
End Function
'文本解密
Friend Function DeText(ByVal strText As String, ByVal strKey As String) As String
Try
Dim Des As New DESCryptoServiceProvider
Dim intLen As Integer
intLen = strText.Length / 2 - 1
Dim inputByteArray(intLen) As Byte
Dim x, i As Integer
For x = 0 To intLen
i = Convert.ToInt32(strText.Substring(x * 2, 2), 16)
inputByteArray(x) = CType(i, Byte)
Next
Des.Key = ASCIIEncoding.ASCII.GetBytes(Left(MD5EncryptProc(strKey), 8).PadRight(8))
Des.IV = ASCIIEncoding.ASCII.GetBytes(Left(MD5EncryptProc(strKey), 8).PadRight(8))
Dim MS As New System.IO.MemoryStream
Dim CS As New CryptoStream(MS, Des.CreateDecryptor, CryptoStreamMode.Write)
CS.Write(inputByteArray, 0, inputByteArray.Length)
CS.FlushFinalBlock()
Return Encoding.Default.GetString(MS.ToArray)
Catch ex As Exception
Return ""
End Try
End Function
'写入
'假设2个变量记录名字和密码:name,pwd
Dim name As String = "name"
Dim pwd As String = "password"
'因为要一起加密存储,所以把名字和密码连接在一起,编程一个字符串,统一加密
Dim code As String = name + CType(0, String) + pwd
'把连接后的字符串转换成字节数组,以便加密
Dim bs As Byte() = System.Text.Encoding.GetEncoding(936).GetBytes(code)
'定义密文列表,用于存储密文,因为最后一个字节是校验,而vb.net我又忘记如何定义数组了,就先用List(Of T)来存储
Dim bsList As New List(Of Byte)
'遍历字符数组,自己随便定义一个规则进行加密,这里是区分能被2,3整除和不能被整除的3种,分别异或不同的值,进行
'简单的置换加密,因为异或的原理是两个数相关性的,异或的值能作为加解密用的固定密码,这里只使用异或,也就算一个
'对称加密方法,同样的方法再次执行就可以得到明文
For i As Integer = 0 To bs.Length - 1
If i Mod 2 = 0 Then
bs(i) = bs(i) Xor &HA1
ElseIf i Mod 3 = 0 Then
bs(i) = bs(i) Xor &H82
Else
bs(i) = bs(i) Xor &H67
End If
bsList.Add(bs(i))
Next
'因为加密后的密文已经无法识别,所以未来解密的时候,如果强行解密,就会遇到别人随机给数据进行算法测试,进行暴力
'破解,所以最后加一个字节的校验,以防数据错误或被修改。校验部分就是把前面生成的密文逐个异或。存储在最后一个字节
Dim checksum As Byte = 0
For Each b As Byte In bsList
checksum = checksum Xor b
Next
bsList.Add(checksum)
'写入文件,写入密文
System.IO.File.WriteAllBytes("c:\log.ini", bsList.ToArray())
'至此,保存部分就结束了
'*************************************************************'
'读取
'读取文件,得到密文
Dim bsRead As Byte() = System.IO.File.ReadAllBytes("c:\log.ini")
'首先将排除了最后一个自己的所有其他密文异或得到校验,再和最后一个字节对比,以确定密文数据的正确性
Dim bc As Byte = 0
For i As Integer = 0 To bsRead.Length - 2
bc = bc Xor bsRead(i)
Next
'这里进行校验检查
If bc <> bsRead(bsRead.Length - 1) Then
MessageBox.Show("checksum error")
Return
End If
'使用同加密一样的方法,对密文异或,置换出明文
Dim readList As New List(Of Byte)
For i As Integer = 0 To bsRead.Length - 2
If i Mod 2 = 0 Then
bsRead(i) = bsRead(i) Xor &HA1
ElseIf i Mod 3 = 0 Then
bsRead(i) = bsRead(i) Xor &H82
Else
bsRead(i) = bsRead(i) Xor &H67
End If
readList.Add(bsRead(i))
Next
'根据得到的明文字节数组创建连接后的字符串
Dim readstring As String = System.Text.Encoding.GetEncoding(936).GetString(readList.ToArray())
'使用连接符进行分割字符串,依次还原得到name,pwd
Dim fields As String() = readstring.Split(CType(0, String))
name = fields(0)
pwd = fields(1)
'结束
reg = Registry.CurrentUser.CreateSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree)
Registry.GetValue(reg.Name, UserNameBox.Text, "PassWord is Null")
'写入
Dim name As String = "name"
Dim pwd As String = "password"
Dim code As String = name + CType(0, String) + pwd
Dim bs As Byte() = System.Text.Encoding.GetEncoding(936).GetBytes(code)
Dim bsList As New List(Of Byte)
For i As Integer = 0 To bs.Length - 1
If i Mod 2 = 0 Then
bs(i) = bs(i) Xor &HA1
ElseIf i Mod 3 = 0 Then
bs(i) = bs(i) Xor &H82
Else
bs(i) = bs(i) Xor &H67
End If
bsList.Add(bs(i))
Next
Dim checksum As Byte = 0
For Each b As Byte In bsList
checksum = checksum Xor b
Next
bsList.Add(checksum)
System.IO.File.WriteAllBytes("c:\log.ini", bsList.ToArray())
'读取
Dim bsRead As Byte() = System.IO.File.ReadAllBytes("c:\log.ini")
Dim bc As Byte = 0
For i As Integer = 0 To bsRead.Length - 2
bc = bc Xor bsRead(i)
Next
If bc <> bsRead(bsRead.Length - 1) Then
MessageBox.Show("checksum error")
Return
End If
Dim readList As New List(Of Byte)
For i As Integer = 0 To bsRead.Length - 2
If i Mod 2 = 0 Then
bsRead(i) = bsRead(i) Xor &HA1
ElseIf i Mod 3 = 0 Then
bsRead(i) = bsRead(i) Xor &H82
Else
bsRead(i) = bsRead(i) Xor &H67
End If
readList.Add(bsRead(i))
Next
Dim readstring As String = System.Text.Encoding.GetEncoding(936).GetString(readList.ToArray())
Dim fields As String() = readstring.Split(CType(0, String))
name = fields(0)
pwd = fields(1)