-1

我收到了一个在 excel 文件中保存的密码。密码使用 Excel xorc 函数“加密”。

现在我正在尝试在 java 程序中构建相同的内容。

当我使用 xor ^ 函数时,我没有收到与 excel 相同的结果。

有谁知道我如何创建一个 java xor 函数,它提供与 excel xorc 相同的结果?

结果 Excel:
键:锁定
输入:650827-25
xorc 输出:xxx[[TT_YOZZ

=xorc("650827-25";"锁")

我的 Java 应用程序的结果:
键:锁定
输入:650827-25
xorc 输出:ZZSS^XNYY

XOR_Codierer

public class XOR_Codierer extends Encoder {

private char[] key;
private char[] password;

public XOR_Codierer(String k){
    key = k.toCharArray();
}

public String encode(String s){
    password = s.toCharArray();

    int pl=password.length;
    int kl=key.length;
    char[] newmsg=new char[pl];

   for (int i=0; i<pl; i++){
          newmsg[i]=(char)(password[i]^key[i%kl]);
    }       


return new String(newmsg);
}

public String decode(String s){
    return encode(s);
}
}

演示

public class Demo{

public static void demo(Encoder enc, String text) {
    String encoded = ((XOR_Codierer) enc).encode(text);
    System.out.println("codiert : " + encoded);
    String decoded = ((XOR_Codierer) enc).decode(encoded);
    System.out.println("decodiert: " + decoded);

    if (text.equals(decoded))
        System.out.println("Verschluesselung erfolgreich!");
    else
        System.out.println("PROGRAMMFEHLER!");
}

public static void main(String[] args){
String key = "lock";
String text = "650827-25";
Encoder enc = new XOR_Codierer(key);
demo(enc, text);
}
}
4

1 回答 1

1

Because XorC (assuming that I have found the correct function) appears to use a more complex algorithm than you have implemented .... http://www.vbaexpress.com/kb/getarticle.php?kb_id=951.

Function XorC(ByVal sData As String, ByVal sKey As String) As String 
    Dim l As Long, i As Long, byIn() As Byte, byOut() As Byte, byKey() As Byte 
    Dim bEncOrDec As Boolean 
     'confirm valid string and key input:'
    If Len(sData) = 0 Or Len(sKey) = 0 Then XorC = "Invalid argument(s) used": Exit Function 
     'check whether running encryption or decryption (flagged by presence of "xxx" at start of sData):'
    If Left$(sData, 3) = "xxx" Then 
        bEncOrDec = False 'decryption'
        sData = Mid$(sData, 4) 
    Else 
        bEncOrDec = True 'encryption'
    End If 
     'assign strings to byte arrays (unicode)'
    byIn = sData 
    byOut = sData 
    byKey = sKey 
    l = LBound(byKey) 
    For i = LBound(byIn) To UBound(byIn) - 1 Step 2 
        byOut(i) = ((byIn(i) + Not bEncOrDec) Xor byKey(l)) - bEncOrDec 'avoid Chr$(0) by using bEncOrDec flag'
        l = l + 2 
        If l > UBound(byKey) Then l = LBound(byKey) 'ensure stay within bounds of Key'
    Next i 
    XorC = byOut 
    If bEncOrDec Then XorC = "xxx" & XorC 'add "xxx" onto encrypted text'
End Function 
于 2013-10-21T11:16:44.637 回答