简体   繁体   English

解密Java中的字节数组

[英]Decrypt byte array in Java

My question is about C# and Java.我的问题是关于 C# 和 Java。

I want to decrypt a byte array like the method d67 of C# from the following link: https://github.com/AresChat/cb0t/blob/master/cb0t/Misc/Hashlink.cs我想从以下链接解密一个字节数组,如 C# 的方法 d67: https://github.com/AresChat/cb0t/blob/master/cb0t/Misc/Hashlink.cs

I translated it to Java:我翻译成Java:

private static byte[] d67(byte[] data, int b)
    {
        byte[] buffer = new byte[data.length];
        System.arraycopy(data, 0, buffer, 0, data.length);

        for (int i = 0; i < data.length; i++)
        {
            buffer[i] = (byte)(data[i] ^ b >> 8 & 255);
            b = (b + data[i]) * 23219 + 36126 & 65535;
        }
        return buffer;
    }

But it gives me not the same result as the C# method.但它给我的结果与 C# 方法不同。

What did I translate wrong?我翻译错了什么?

The result I am getting with C#:我用 C# 得到的结果:

public static void Main() 
        {
           string str = "F5fPxdTq8eJeuqSVejGmq7aTh6BJZ8J0jgt92MDDjxTIWf+mWa8Ld+01L2bVIV6FXhCO";
      byte[] val2 = Convert.FromBase64String(str);
      val2 = d67(val2, 28435);
      Console.WriteLine("Converted byte value: {0}", BitConverter.ToString(val2));
        }
private static byte[] d67(byte[] data, int b)
        {
            byte[] buffer = new byte[data.Length];
            Array.Copy(data, buffer, data.Length);

            for (int i = 0; i < data.Length; i++)
            {
                buffer[i] = (byte)(data[i] ^ b >> 8 & 255);
                b = (b + data[i]) * 23219 + 36126 & 65535;
            }
            return buffer;
        }

are: 78-9C-63-60-C0-04-CE-1E-8E-21-40-EC-E7-E7-EA-C3-60-A6-76-C9-DC-F0-0A-48-D4-D7-31-D2-3F-C8-35-58-C1-C5-55-C1-D8-40-C7-C4-40-C7-D4-80-81-01-00-F3-11-09-F1是:78-9C-63-60-C0-04-CE-1E-8E-21-40-EC-E7-E7-EA-C3-60-A6-76-C9-DC-F0-0A-48- D4-D7-31-D2-3F-C8-35-58-C1-C5-55-C1-D8-40-C7-C4-40-C7-D4-80-81-01-00-F3-11- 09-F1

I am expecting the same result with Java:我期待与 Java 相同的结果:

public static void main(String[] args)
    {
        String encodedString = "arlnk://F5fPxdTq8eJeuqSVejGmq7aTh6BJZ8J0jgt92MDDjxTIWf+mWa8Ld+01L2bVIV6FXhCO";
        encodedString = encodedString.substring(8);
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] decodedByteArray = decoder.decode(encodedString);
        decodedByteArray = d67(decodedByteArray, 28435);
        String array = "";
        for (int f=0; f<decodedByteArray.length; f++) {
            if (f == decodedByteArray.length - 1) {
                array = array + String.format("%02X", decodedByteArray[f]);
            }
            else {
                array = array + String.format("%02X", decodedByteArray[f]) + "-";
            }
        }
        System.out.print(array);
    }

    private static byte[] d67(byte[] data, int b)
    {
        byte[] buffer = new byte[data.length];
        System.arraycopy(data, 0, buffer, 0, data.length);

        for (int i = 0; i < data.length; i++)
        {
            buffer[i] = (byte)(data[i] ^ b >> 8 & 255);
            b = (b + data[i]) * 23219 + 36126 & 65535;
        }
        return buffer;
    }

As @khelwood observation byte type is different in both languages.由于@khelwood 观察byte类型在两种语言中都不同。

You can convert signed to unsigned.您可以将有符号转换为无符号。

After converting the Signed byte to unsigned it produces the result same as in.Net将有符号字节转换为无符号字节后,它会产生与 in.Net 相同的结果

    private static byte[] d67(byte[] data, int b) {
        byte[] buffer = new byte[data.length];
        System.arraycopy(data, 0, buffer, 0, data.length);

        for (int i = 0; i < data.length; i++) {
            int unsignedData = unsignedToBytes(data[i]);

            buffer[i] = (byte) (unsignedData ^ b >> 8 & 255);

            b = (b + unsignedData) * 23219 + 36126 & 65535;
        }
        return buffer;
    }

    public static int unsignedToBytes(byte b) {
        return b & 0xFF;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM