怎样才能更好的加密一段字符串?MD5、RSA、DES?
好吧,这些都不懂。
参考了一下度娘,所以想法是:
对于加密:
· 从待加密字符串中取出一个字符
· 取字符的低字节和Key1进行异或运算
· 取字符的高字节
· 将运算后的数据合成新的字符
对于解密:
· 从待解密字符串中取出一个字符
· 取字符的低字节和Key1进行异或运算
· 取字符的高字节
· 将运算后的数据合成新的字符
有了方法,可以完成下面的函数:
本加密方法只用于英文,中文也可以加密,但无法解密...
加密 (Encryption):
Private Function Encrypt(ByVal StrSource As String) As String '加密 Dim BLowData As Byte Dim BHigData As Byte Dim SHigData As Byte Dim i As Long Dim k As Integer Dim StrEncrypt As String Dim StrChar As String Dim KeyTemp As String Dim Key1 As Byte For k = 1 To 30 KeyTemp = KeyTemp & CStr(Int(Rnd * (9) + 1)) Next Key1 = CByte(Mid(KeyTemp, 11, 1) & Mid(KeyTemp, 27, 1)) For i = 1 To Len(StrSource) StrChar = Mid(StrSource, i, 1) '从待加密字符串中取出一个字符 BLowData = AscB(MidB(StrChar, 1, 1)) Xor Key1 '取字符的低字节和Key1进行异或运算 SHigData = AscB(MidB(StrChar, 2, 1)) '取字符的高字节 StrEncrypt = StrEncrypt & ChrB(BLowData) & ChrB(BHigData) '将运算后的数据合成新的字符 Next i Encrypt = KeyTemp & StrEncrypt End Function
解密(Decryption):
Private Function Decrypt(ByVal StrSource As String) As String '解密 Dim BLowData As Byte Dim BHigData As Byte Dim i As Long Dim k As Integer Dim StrDecrypt As String Dim StrChar As String Dim KeyTemp As String Dim Key1 As Byte KeyTemp = Mid(StrSource, 1, 30) Key1 = CByte(Mid(KeyTemp, 11, 1) & Mid(KeyTemp, 27, 1)) For i = 31 To Len(StrSource) StrChar = Mid(StrSource, i, 1) '从待解密字符串中取出一个字符 BLowData = AscB(MidB(StrChar, 1, 1)) Xor Key1 '取字符的低字节和Key1进行异或运算 BHigData = AscB(MidB(StrChar, 2, 1)) '取字符的高字节 StrDecrypt = StrDecrypt & ChrB(BLowData) & ChrB(BHigData) '将运算后的数据合成新的字符 Next i Decrypt = StrDecrypt End Function
Private Sub copyl_Click() Clipboard.Clear Clipboard.SetText (jiemit.Text) End Sub
Option Explicit 'Copyright (c) 2014,烟台大学计算机学院 'All right reserved. '作者:邵帅 '文件:工程1.vsp '完成时间:2015年2月26日 '版本号:v1.0.1 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function ReleaseCapture Lib "user32" () As Long Private Const WM_SYSCOMMAND = &H112 Private Const SC_MOVE = &HF010& Private Const HTCAPTIO = 2 Private Function Encrypt(ByVal StrSource As String) As String '加密 Dim BLowData As Byte Dim BHigData As Byte Dim SHigData As Byte Dim i As Long Dim k As Integer Dim StrEncrypt As String Dim StrChar As String Dim KeyTemp As String Dim Key1 As Byte For k = 1 To 30 KeyTemp = KeyTemp & CStr(Int(Rnd * (9) + 1)) Next Key1 = CByte(Mid(KeyTemp, 11, 1) & Mid(KeyTemp, 27, 1)) For i = 1 To Len(StrSource) StrChar = Mid(StrSource, i, 1) '从待加密字符串中取出一个字符 BLowData = AscB(MidB(StrChar, 1, 1)) Xor Key1 '取字符的低字节和Key1进行异或运算 SHigData = AscB(MidB(StrChar, 2, 1)) '取字符的高字节 StrEncrypt = StrEncrypt & ChrB(BLowData) & ChrB(BHigData) '将运算后的数据合成新的字符 Next i Encrypt = KeyTemp & StrEncrypt End Function Private Function Decrypt(ByVal StrSource As String) As String '解密 Dim BLowData As Byte Dim BHigData As Byte Dim i As Long Dim k As Integer Dim StrDecrypt As String Dim StrChar As String Dim KeyTemp As String Dim Key1 As Byte KeyTemp = Mid(StrSource, 1, 30) Key1 = CByte(Mid(KeyTemp, 11, 1) & Mid(KeyTemp, 27, 1)) For i = 31 To Len(StrSource) StrChar = Mid(StrSource, i, 1) '从待解密字符串中取出一个字符 BLowData = AscB(MidB(StrChar, 1, 1)) Xor Key1 '取字符的低字节和Key1进行异或运算 BHigData = AscB(MidB(StrChar, 2, 1)) '取字符的高字节 StrDecrypt = StrDecrypt & ChrB(BLowData) & ChrB(BHigData) '将运算后的数据合成新的字符 Next i Decrypt = StrDecrypt End Function Private Sub clearl_Click() jiamit.Text = "" jiemit.Text = "" End Sub Private Sub close_Click() End End Sub Private Sub copyl_Click() Clipboard.Clear Clipboard.SetText (jiemit.Text) End Sub Private Sub Image1_Click() End Sub Private Sub jiamil_Click() jiemit.Text = Encrypt(jiamit.Text) End Sub Private Sub jiemil_Click() jiemit.Text = Decrypt(jiemit.Text) End Sub Private Sub labFormTitle_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) ReleaseCapture 'WM_SYS向窗体发送一个移动窗体命令 Call SendMessage(Me.hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTIO, 0) 'SC_MOVE+ HTCAPTIO表示单击左键移动窗体 End Sub Private Sub mini_Click() If Me.WindowState = 0 Then Me.WindowState = 1 End If End Sub
好吧,顺便写了一个UI,纯英文界面,高大上有木有,还能学习英文,因为写了这段代码,我知道了加密和解密的英文写法。
、
满满的字符,瞬间猜不透了有木有。
下载:
@ Mayuko