gpt4 book ai didi

Java xor 字节数组不返回预期结果

转载 作者:行者123 更新时间:2023-11-29 06:34:20 25 4
gpt4 key购买 nike

这是作业的一部分,我正在尝试从使用相同 OTP 加密的多个密文中解密一条消息。

我的问题是,当我尝试对密文进行异或时,我收到了意外的结果。

String ciphertext1 = "315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e";
String ciphertext2 = "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904";

我将它们转换为字节数组

public static byte[] hexStringToByteArray(String s) {
HexBinaryAdapter adapter = new HexBinaryAdapter();
byte[] bytes = adapter.unmarshal(s);
return bytes;
}

public static byte[] xor(byte[] a, byte[] b) {
byte[] result = null;
if (a.length > b.length) {
for (int i = 0; i < b.length; i++) {
result = new byte[b.length];
result[i] = (byte) (((int) a[i]) ^ ((int) b[i]));
}
} else {
for (int i = 0; i < a.length; i++) {
result = new byte[a.length];
result[i] = (byte) (((int) a[i]) ^ ((int) b[i]));
}
}
return result;
}

public static void main(String[] args){
byte[] bytes1 = hexStringToByteArray(ciphertext1);
byte[] bytes2 = hexStringToByteArray(ciphertext2);
byte[] cipher1 = xor(bytes1, bytes2);
System.out.println(javax.xml.bind.DatatypeConverter.printHexBinary(bytes1));
System.out.println(javax.xml.bind.DatatypeConverter.printHexBinary(bytes2));
System.out.println(javax.xml.bind.DatatypeConverter.printHexBinary(cipher1));
}

输出:

315C4EEAA8B5F8AAF9174145BF43E1784B8FA00DC71D885A804E5EE9FA40B16349C146FB778CDF2D3AFF021DFFF5B403B510D0D0455468AEB98622B137DAE857553CCD8883A7BC37520E06E515D22C954EBA5025B8CC57EE59418CE7DC6BC41556BDB36BBCA3E8774301FBCAA3B83B220809560987815F65286764703DE0F3D524400A19B159610B11EF3E
32510BA9BABEBBBEFD001547A810E67149CAEE11D945CD7FC81A05E9F85AAC650E9052BA6A8CD8257BF14D13E6F0A803B54FDE9E77472DBFF89D71B57BDDEF121336CB85CCB8F3315F4B52E301D16E9F52F904
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000054

我的问题是异或运算的结果,输出的最后一行。当我手动异或前 2 个字节时,我得到;

315C = 0011000101011100
3251 = 0011001001010001

xor = 0000001100001101

所以我得到的结果是错误的。我在这里做错了什么?

谢谢!

最佳答案

每次迭代 for 循环时,您都在重新创建字节数组:

for (int i = 0; i < b.length; i++) {
result = new byte[b.length]; // <---- wipes out previous values
result[i] = (byte) (((int) a[i]) ^ ((int) b[i]));
}

将它移到 for 循环之外。

以下是经过简化、更正的版本。请注意,如果您使用 Math.min,则不需要两个循环:

public static byte[] xor(byte[] a, byte[] b) {
byte[] result = new byte[Math.min(a.length, b.length)];

for (int i = 0; i < result.length; i++) {
result[i] = (byte) (((int) a[i]) ^ ((int) b[i]));
}

return result;
}

关于Java xor 字节数组不返回预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24487006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com