- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如上所述,我在 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/
当我向我的安装程序添加依赖项时,我总是留下文件的硬编码路径。 根据 this blog ,解决此问题的唯一方法是使用 ISProjectFolder 变量手动更改 ISL 文件中的硬编码路径。这没有任
我想开发像蓝牙 LE 外围设备这样的应用程序,它停止在与蓝牙 LE 中央设备连接时做广告,并限制与多个蓝牙 LE 中央设备连接的蓝牙 LE 外围设备。 一个蓝牙 LE 外围设备一次只能与一个蓝牙 LE
示例代码: # Step 1 $start = get-date for($i=1; $i -le 1000000; $i++){} $end = get-date ($end-$start).Tot
在Basic customisation ,python 文档说明了比较方法: [no swapped-argument versions of these methods]; rather, __l
蓝牙双模设备是否可以在与 BT LE 设备配对的同时被经典蓝牙发现?如果设备不能同时运行这两种模式也没关系,但我真的应该在这些模式之间切换芯片吗?我只是在 BT 4 Core 规范中找不到答案 最佳答
我正在尝试使用 fscanf 从输入文件中读取 3 个 double 值,但在尝试这样做时遇到了段错误。我的代码如下: FILE * infile = fopen(argv[1], "r");
我的域指向另一个网站,我想为其安装 LE 证书,这可能吗,或者我的域应该链接到服务器? 最佳答案 letsencrypt 不允许安装非 NS 指向,因为它检查反向验证,所以它会失败 关于wordpre
在我的 Android(API 版本 21 及更高版本)Bluetooth LE 应用程序中,该应用程序首先使用 BluetoothLeScanner 和 ScanCallback 对象扫描外围设备。
我正在使用 Xamarin 和这个 BLE 插件构建一个 iOS 应用程序: https://github.com/aritchie/bluetoothle 我只是通过 BLE 广播一个 UUID,它
我可以在点击时同时扫描蓝牙和蓝牙 LE 设备吗? 最佳答案 答案是否定的。 原文来自: http://developer.android.com/guide/topics/connectivity/b
关于 Dennis Mathews answer,您如何在 iOS 上指定经典蓝牙而不是 LE?有没有和Core Bluetooth Framework?不同的api 最佳答案 Joels 有一种在
我有一个使用 BLE 的应用程序。在某些情况下,例如当安装在 iPhone 6 上时,应用程序正在运行并且不请求使用 BLE 的许可。 在其他情况下,比如我的 iPad Air,应用程序开始运行,并且
我尝试使用 Bluetooth LE 检测设备,按照 Xamarin 库中的说明执行此操作,但设备发现 0 包括 android 中的必要声明。将扫描时间增加到 30 秒,但没有任何效果。支持蓝牙,版
配对“普通”蓝牙设备和配对蓝牙 LE 设备之间是否存在任何(大)技术差异? 我找到了很多关于蓝牙 LE 配对的信息,但没有正常的?例如 Info 1 . 那么普通蓝牙的这个信息也正确吗? 最佳答案 如
我尝试使用下面的代码将发现的设备的UUID存储到Array,然后在该阵列中选择UUID进行连接,但无法运行。 - (void)centralManager:(CBCentralManager *)ce
我正在研究一个通过蓝牙 LE 发送数据的 Android 应用程序。这里有两种方法让我感到困惑。有人可以解释一下这两种方法及其相互关系吗: private String localmessage =
我在使用 BluetoothLeScanner 的 startScan 方法时遇到了问题,找到了一个 BLE 设备,但是当我关闭 BLE 设备时,我的手机仍然显示此设备已打开!! 我尝试过使用: pr
我使用 StartLeScan 已经有一段时间了,扫描后返回的设备的名称从未被填充。我永远无法弄清楚为什么,我只是找回了 mac 地址。今天我从 OS BT 设置页面运行扫描,它只显示 MAC 地址,
在实现大量使用 Service 的 Android 应用程序方面,我不是很有经验,而且我在 SO 或 github 上找不到任何好的建议或示例来帮助我(或者只是谷歌搜索)所以我决定问我自己的问题。 当
开发用于蓝牙技术的移动应用程序能否用于蓝牙 LE 技术? 或者开发人员是否需要修改代码来处理蓝牙 LE(智能)信号、输入、输出等? 编辑 问这个问题的时候我想知道 如果我写一段代码,我可以在 BLE
我是一名优秀的程序员,十分优秀!