gpt4 book ai didi

Java UTF-16LE 奇怪的错误困扰了我的教授

转载 作者:行者123 更新时间:2023-12-02 00:23:01 26 4
gpt4 key购买 nike

如上所述,我在 Java 中遇到了 UTF-16LE 问题。当我运行代码时,它通常会正确加密、解密并打印消息。有时它会丢失两个加密字符,有时它会打印出非英语字符,例如希腊语等。

这是一个类,它确实难倒了我的教授,任何有关该错误的帮助将不胜感激。

/*This program was written by me for my professors class.

该程序的目的是使用 XOR 加密来加密消息,以同样的方式解密它,并为这些方法生成密码本。*/

import java.security.*;

public class Crypto
{
public static void main(String[] args) throws Exception
{
Crypto c = new Crypto();

byte[]codebook = null;
String message = "";

System.out.println("Generating codebook");
codebook = c.makeCodebook(14);
System.out.println();

System.out.println("Now Encrypting");
System.out.println("This is the contents of the Encrypted Message: ");
message = c.crypt("1234567", codebook);
System.out.println();

System.out.println("Now Decrypting");
System.out.println("This is the contents of the Decrypted Message");
message = c.crypt(message, codebook);

System.out.println();
System.out.println("Your message is: ");
System.out.println(message);

}//publis static void main(String [] args)

//Encrypts or decrypts message against the codebook assuming the appropriate length
public String crypt (String message, byte [] codebook) throws Exception
{
//Take the message and put it into an array of bytes.
//This array of bytes is what will be XORed against the codebook
byte[] cryptedMessage = message.getBytes("UTF-16LE");
byte[] result = new byte[14];
message = "";
System.out.println(message.length());
System.out.println(cryptedMessage.length);
System.out.println(result.length);
//Let's see the contents of encryptedMessage
for(int i = 0; i< cryptedMessage.length; i++)
{
System.out.print(cryptedMessage[i]+" ");
}//for(int i = 0; i< encryptedMessage.length; i++)
System.out.println();

//XOR codebook and encryptedMessage
System.out.println("This is the message using XOR:");
for(int i = 0; i<result.length; i++)
{
//since XOR has return type of an int, we cast it to a byte
result[i] = (byte)(((byte)(codebook[i])) ^ ((byte)(cryptedMessage[i])));
System.out.print(result[i]+" ");
}//while(result[i]!=0)
//output
System.out.println();
//output

System.out.println(message.length());
System.out.println(cryptedMessage.length);
System.out.println(result.length);
return new String(result, "UTF-16LE");
}//public String crypt (String message, byte [] codebook) throws Exception

//Creates truly random numbers and makes a byte array using those truly random numbers
public byte [] makeCodebook (int length) throws Exception
{
SecureRandom SecureRandom = new SecureRandom();//instance of SecureRandom named random
byte[] codebook = null;

codebook = new byte[length];
SecureRandom.nextBytes(codebook);//generate bytes using the byte[]codebook

//output
System.out.println("This is the contents of the codebook: ");
for(int i = 0; i < codebook.length;i++)
{
System.out.print(codebook[i]+" ");
}//for(int i = 0; i < codebook[i];i++)
//output
System.out.println();
return codebook;
}//public byte [] MakeCodebook (int length) throws Exception

}//Public class Crypto

最佳答案

问题可能是因为对随机数据进行异或操作有时会产生一个输出,该输出在解释为 UTF-16LE 字节序列时无法表达有效的字符集。

不要尝试将密文解释为 UTF-16LE 字符串,而是考虑在生成密文字节后对其进行 Base64 编码并返回生成的 Base64 编码字符串。然后在解密时,对输入字符串进行 Base64 解码以获取密文字节,执行 XOR 以获得明文字节,然后通过将明文字节解释为 UTF-16LE 序列,从明文字节创建明文字符串。

<小时/>

已更新以回复下面的评论,而不必担心空间不足。

正如我所说,您可以对密文字节进行 Base64 编码。这在 Java 中当然是可行的。 commons-codec 库有方法可以做到这一点,搜索一下就能找到其他方法。如果您不允许使用外部库,请使用您自己的方法将任意字节转换为保证是某些内容的有效编码的字节。

例如,您可以将密文中的每个字节拆分为高 4 位和低 4 位。因此,每个字节都会产生一对值,每个值的范围为 0-15。然后,您可以通过将 'A' 的 ASCII 代码添加到这些数字中,从每个字节生成一个 2 字节序列。这不是很有效,但它可以完成工作。

关于Java UTF-16LE 奇怪的错误困扰了我的教授,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10587061/

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