怎样才能更好的加密一段字符串?MD5、RSA、DES?
好吧,这些都不懂。
参考了一下度娘,所以想法是:
对于加密:
· 从待加密字符串中取出一个字符
· 取字符的低字节和Key1进行异或运算
· 取字符的高字节
· 将运算后的数据合成新的字符
对于解密:
· 从待解密字符串中取出一个字符
· 取字符的低字节和Key1进行异或运算
· 取字符的高字节
· 将运算后的数据合成新的字符
有了方法,可以完成下面的函数:
本加密方法只用于英文,中文也可以加密,但无法解密...
加密 (Encryption):
Private Function Encrypt(ByVal StrSource As String) As String '加密Dim BLowData As ByteDim BHigData As ByteDim SHigData As ByteDim i As LongDim k As IntegerDim StrEncrypt As StringDim StrChar As StringDim KeyTemp As StringDim 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 & StrEncryptEnd Function解密(Decryption):
Private Function Decrypt(ByVal StrSource As String) As String '解密Dim BLowData As ByteDim BHigData As ByteDim i As LongDim k As IntegerDim StrDecrypt As StringDim StrChar As StringDim KeyTemp As StringDim Key1 As ByteKeyTemp = 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 iDecrypt = StrDecryptEnd Function另外,考虑到加密字符的复杂性,建立一个command用来复制到剪贴板:
Private Sub copyl_Click()Clipboard.ClearClipboard.SetText (jiemit.Text)End Sub所以:
Option Explicit'Copyright (c) 2014,烟台大学计算机学院'All right reserved.'作者:邵帅'文件:工程1.vsp'完成时间:2015年2月26日'版本号:v1.0.1Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Declare Function ReleaseCapture Lib "user32" () As LongPrivate Const WM_SYSCOMMAND = &H112Private Const SC_MOVE = &HF010&Private Const HTCAPTIO = 2Private Function Encrypt(ByVal StrSource As String) As String '加密Dim BLowData As ByteDim BHigData As ByteDim SHigData As ByteDim i As LongDim k As IntegerDim StrEncrypt As StringDim StrChar As StringDim KeyTemp As StringDim 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 & StrEncryptEnd FunctionPrivate Function Decrypt(ByVal StrSource As String) As String '解密Dim BLowData As ByteDim BHigData As ByteDim i As LongDim k As IntegerDim StrDecrypt As StringDim StrChar As StringDim KeyTemp As StringDim Key1 As ByteKeyTemp = 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 iDecrypt = StrDecryptEnd FunctionPrivate Sub clearl_Click()jiamit.Text = ""jiemit.Text = ""End SubPrivate Sub close_Click()EndEnd SubPrivate Sub copyl_Click()Clipboard.ClearClipboard.SetText (jiemit.Text)End SubPrivate Sub Image1_Click()End SubPrivate Sub jiamil_Click()jiemit.Text = Encrypt(jiamit.Text)End SubPrivate Sub jiemil_Click()jiemit.Text = Decrypt(jiemit.Text)End SubPrivate 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 SubPrivate Sub mini_Click()If Me.WindowState = 0 Then Me.WindowState = 1End IfEnd Sub好吧,顺便写了一个UI,纯英文界面,高大上有木有,还能学习英文,因为写了这段代码,我知道了加密和解密的英文写法。
、
满满的字符,瞬间猜不透了有木有。
下载:
@ Mayuko