VB加密算法改成java

Public Function Encode(ByVal s As String) As String '加密
    On Error GoTo err
    If Len(s) = 0 Then Exit Function
    Dim Buff() As Byte
    Buff = StrConv(s, vbFromUnicode)
    Dim i As Long
    Dim j As Byte
    Dim k As Byte, m As Byte
    Dim mstr As String
    mstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
    Dim outs As String
    i = UBound(Buff) + 1
    outs = Space(2 * i)
    Dim Temps As String
    For i = 0 To UBound(Buff)
        Randomize Time
        j = CByte(5 * (Math.Rnd()) + 0) '
        Buff(i) = Buff(i) Xor j
        k = Buff(i) Mod Len(mstr)
        m = Buff(i) \ Len(mstr)
        m = m * 2 ^ 3 + j
        Temps = Mid(mstr, k + 1, 1) + Mid(mstr, m + 1, 1)
        Mid(outs, 2 * i + 1, 2) = Temps
    Next
    Encode = outs
    Exit Function
err:
End Function






Public Function Decode(ByVal s As String) As String '解密
    On Error GoTo err
    Dim i As Long
    Dim j As Byte
    Dim k As Byte
    Dim m As Byte
    Dim mstr As String
    mstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
    Dim T1 As String, T2 As String
    Dim Buff() As Byte
    Dim n As Long
    n = 0
    For i = 1 To Len(s) Step 2
        T1 = Mid(s, i, 1)
        T2 = Mid(s, i + 1, 1)
        k = InStr(1, mstr, T1) - 1
        m = InStr(1, mstr, T2) - 1
        j = m \ 2 ^ 3
        m = m - j * 2 ^ 3
        ReDim Preserve Buff(n)
        Buff(n) = j * Len(mstr) + k
        Buff(n) = Buff(n) Xor m
        n = n + 1
    Next
    Decode = StrConv(Buff, vbUnicode)
    Exit Function
err:
    Decode = ""

End Function


>>java转换





public class EDcode {
public static void main(String[] s) throws Exception{

   String ss=StringEncode("123456锦是方法");
   String ds= StringDecode(ss);
   System.out.println(ds);
  
}
static String  StringEncode(String s) throws Exception{
   byte j;
   byte k;
   byte m;
   String mstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
   byte[] Buff=s.getBytes("GBK");
   char[] outs=new char[s.getBytes("GBK").length*2];
   for(int i=0;i<Buff.length;i++){  
   j=(byte) (Math.random()*5+0.5);    
   //Buff[i] =(byte) ((Buff[i]&0xFF) ^ j); 
   k = (byte)(((Buff[i]&0xFF) ^ j) %mstr.length());
   m = (byte)(((Buff[i]&0xFF) / mstr.length())* 8 + j);
        outs[2 * i + 0]= mstr.charAt(k);
        outs[2 * i + 1]= mstr.charAt(m);
   }
   return String.valueOf(outs);
}

static String  StringDecode(String s) throws Exception{
   byte j;
   byte k;
   byte m;
   String mstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";  
   char t1,t2;
   int n=0;
   byte[] Buff = new byte[s.length()/2];
   for(int i=0;i<s.length();i+=2){
    t1 = s.charAt(i);//Mid(s, i, 1) 
       t2 =  s.charAt(i+1);
       k = (byte)mstr.indexOf(t1);
       m = (byte)mstr.indexOf(t2);
       
       j = (byte)(m/8);
       
       m = (byte)(m - j * 8);
     
     //  Buff[n]= (char)(j *mstr.length() + k);
       Buff[n] = (byte)((j *mstr.length() + k)^ m);
       n ++;
   }
   return new String(Buff);
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值