原来这个是delphi的、我翻译了一下、我感觉还是不错的、唯一的问题是不支持中文、不过没关系、反正我从来我对中文字符串加密的、
加密函数
Private Function EncrypStr(ByVal Src As String, _ Optional ByVal Key As String = "ForDream") As String '//对字符串加密(Src:源 Key:密匙) Dim KeyLen As Long Dim KeyPos As Long Dim offset As Long Dim dest As String Dim SrcPos As Long Dim SrcAsc As Long Dim Range As Long Dim TmpAsc As Long KeyLen = Len(Key) KeyPos = 0 Range = 256 Randomize offset = Int(Rnd * Range) dest = Hex(offset) For SrcPos = 1 To Len(Src) TmpAsc = (Asc(Mid$(Src, SrcPos, 1)) + offset) If TmpAsc < 0 Then TmpAsc = TmpAsc + 65536 SrcAsc = TmpAsc Mod 255 If KeyPos < KeyLen Then KeyPos = KeyPos + 1 Else KeyPos = 1 End If TmpAsc = Asc(Mid$(Key, KeyPos, 1)) If TmpAsc < 0 Then TmpAsc = TmpAsc + 65536 SrcAsc = SrcAsc Xor TmpAsc dest = dest + Hex(SrcAsc) offset = SrcAsc Next EncrypStr = dest End Function
解密函数
Private Function UncrypStr(ByVal Src As String, ByVal Key As String) As String '//对字符串解密(Src:源 Key:密匙) Dim KeyLen As Long Dim KeyPos As Long Dim offset As Long Dim dest As String Dim SrcPos As Long Dim SrcAsc As Long Dim TmpSrcAsc As Long Const H16 = "&H" KeyLen = Len(Key) KeyPos = 0 offset = CInt(H16 & Mid$(Src, 1, 2)) SrcPos = 3 Do SrcAsc = CInt(H16 & Mid$(Src, SrcPos, 2)) If KeyPos < KeyLen Then KeyPos = KeyPos + 1 Else KeyPos = 1 End If TmpSrcAsc = SrcAsc Xor Asc(Mid$(Key, KeyPos, 1)) If TmpSrcAsc <= offset Then TmpSrcAsc = 255 + TmpSrcAsc - offset Else TmpSrcAsc = TmpSrcAsc - offset End If dest = dest + Chr(TmpSrcAsc) offset = SrcAsc SrcPos = SrcPos + 2 Loop Until SrcPos >= Len(Src) UncrypStr = dest End Function