求實作簡單的字母加減移位,怎么效率最高?數字好辦, 字母怎么加減呢?不知道有沒有現成的這樣的函式?:
目的:對字串中的字符進行簡單的移位加密解密。
加密就是簡單地將字串中的b換成a,c換成b,d換成c, 1換成0, 2換成1.........
即全部減1(字母為字母表順序退1位).然后一些特殊字符特殊處理(a換成-,-換成$, 0換成Z,:換成.)
規則(字母數字一一對應關系:)
a-bcdefghijklmnopqrstuvw.xyz:1234567890
加密后一一對應:
-$abcdefghijklmnopqrstuv9wxy.012345678Z
比如
86baidu.com
加密后需要是
75a-hct9bnl
解密則反過來。不知道有沒有現成的這樣加解密的函式?
非常感謝!
uj5u.com熱心網友回復:
據我了解沒有現成的。讀取字串->識別第一個字符->編碼處理(可加可級訓替換成對應某字符,看你實際需求)->處理第二個字符直到最后一個。uj5u.com熱心網友回復:
可以簡單加減實作。但字母數字以外的符號,并非按你設想改變,而是按 ASCII 碼順序增減。Const key As Byte = 1
Private Function Encode(ByVal strSrc As String) As String
Dim i As Long, tmp As String, b As Byte
For i = 1 To Len(strSrc)
b = Asc(Mid(strSrc, i, 1))
If (b < 32) Or (b > 127) Then Exit Function
tmp = tmp & Chr((Asc(Mid(strSrc, i, 1)) + 127 - key) Mod 127)
Next i
Encode = tmp
End Function
Private Function Decode(ByVal strSrc As String) As String
Dim i As Long, tmp As String, b As Byte
For i = 1 To Len(strSrc)
b = Asc(Mid(strSrc, i, 1))
If b < 32 Or b > 127 Then Exit Function
tmp = tmp & Chr((Asc(Mid(strSrc, i, 1)) + 127 + key) Mod 127)
Next i
Decode = tmp
End Function
Private Sub Text1_Change()
Text2.Text = Encode(Text1.Text)
Text3.Text = Decode(Text1.Text)
End Sub
uj5u.com熱心網友回復:
當然,通過查表法也可以精確實作你設計的置換:Const key As Byte = 1
Const strList As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:$-abcdefghijklmnopqrstuvwxyz"
Private Function Encode(ByVal strSrc As String) As String
Dim i As Long, tmp As String, b As Byte
For i = 1 To Len(strSrc)
b = InStr(strList, Mid(strSrc, i, 1))
If b = 0 Then Exit Function
b = (b + Len(strList) - key) Mod Len(strList)
If b = 0 Then b = Len(strList)
tmp = tmp & Mid(strList, b, 1)
Next i
Encode = tmp
End Function
Private Function Decode(ByVal strSrc As String) As String
Dim i As Long, tmp As String, b As Byte
For i = 1 To Len(strSrc)
b = InStr(strList, Mid(strSrc, i, 1))
If b = 0 Then Exit Function
b = (b + Len(strList) + key) Mod Len(strList)
If b = 0 Then b = Len(strList)
tmp = tmp & Mid(strList, b, 1)
Next i
Decode = tmp
End Function
Private Sub Text1_Change()
Text2.Text = Encode(Text1.Text)
Text3.Text = Decode(Text1.Text)
End Sub
查表只能置換表中的字符。
但查表有一個潛在的優點,那就是 ,如果表中是經過亂序的字符,破解的難度就大很多。相當于使用了密碼表。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/91026.html
標籤:VB基礎類
上一篇:1.用VBA宏回圈創建word檔案后保存,如何默認創建,不顯示重繪?2.如何打開包含宏的ppt就執行相應的宏,求詳細步驟?
