const  XorKey:array[0..7] of Byte=($B2,$09,$AA,$55,$93,$6D,$84,$47); //字符串加密用function Enc(Str:String):String;//字符加密函數  這是用的一個異或加密   
var  
 i,j:Integer;   
begin  
 Result:='';   
 j:=0;   
 for i:=1 to Length(Str) do  
   begin  
     Result:=Result+IntToHex(Byte(Str[i]) xor XorKey[j],2);   
     j:=(j+1) mod 8;   
   end;   
end;   
  
function Dec(Str:String):String;//字符解密函數   
var  
 i,j:Integer;   
begin  
 Result:='';   
 j:=0;   
 for i:=1 to Length(Str) div 2 do  
   begin  
     Result:=Result+Char(StrToInt('$'+Copy(Str,i*2-1,2)) xor XorKey[j]);   
     j:=(j+1) mod 8;   
   end;   
end; 

解决方案 »

  1.   

    Private XorKey(7) As Byte
    Private Sub Command1_Click()
        Dim s As String
        s = "hello"
        s = Encode(s)
        s = Decode(s)
        Debug.Print s
    End SubPrivate Sub Form_Load()
        XorKey(0) = &HB2
        XorKey(1) = &H9
        XorKey(2) = &HAA
        XorKey(3) = &H55
        XorKey(4) = &H93
        XorKey(5) = &H6D
        XorKey(6) = &H84
        XorKey(7) = &H47
    End Sub
    Function Encode(ByVal sStr As String) As String
        Dim i As Long, j As Long
        Dim s As String
        For i = 1 To Len(sStr)
            s = s & Hex(AscB(Mid(sStr, i, 1)) Xor XorKey(j))
            j = (j + 1) Mod 8
        Next i
        Encode = s
    End Function
    Function Decode(ByVal sStr As String) As String
        Dim i As Long, j As Long
        Dim s As String, c As String
        For i = 1 To Len(sStr) \ 2
            c = Mid(sStr, i * 2 - 1, 2)
            s = s & Chr(CByte("&H" & c) Xor XorKey(j))
            j = (j + 1) Mod 8
        Next i
        Decode = s
    End Function