VB6的一个字符加密、解密的函数

原来这个是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

转载于:https://www.cnblogs.com/ForDream/archive/2010/08/29/1924463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值