- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
简短版本:我使用 DataOutputStream 将填充有随机字节的 8 字节字节数组写入磁盘,然后在另一种方法中使用 DataInputStream 将其读回。数据看起来不太一样。我应该从哪里开始寻找问题?
长版:我有一段代码正在使用 javax.crypto 库进行基于密码的加密。我使用随机数生成器生成 8 字节随机盐,然后使用 1000 次迭代计数来生成 key 。
当我写入文件时,它的格式为:
[ 8-byte Salt ][int iteration count][encrypted payload]
当我读取文件的开头以恢复重建 key 的参数时,字节数组似乎与写入的内容不同。然而,迭代计数已成功恢复。
完整的代码在这里:
http://hg.kurt.im/479hw3/src/0c3c11f68f26/src/csc479_hw3/PBE.java
相关部分如下:
boolean encrypt(char[] password, String fileName){
Random randy = new Random();
byte[] salt = new byte[8];
randy.nextBytes(salt);
System.out.println("Salt: "+salt);
try{
File inFile = new File(fileName);
File outFile = new File(fileName+."encrypted);
DataOutputStream dOS = new DataOutputStream(new FileOutputStream(outFile));
dOS.write(salt);
dOS.flush();
dOS.writeInt(1000);
dOS.flush();
dOS.close();
/* Snip a bunch of stuff currently commented out related to encryption */
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
boolean decrypt(char[] password, string fileName, string outFileName){
byte[] salt = new byte[8];
try{
DataInputStream dIS = new DataInputStream(new FileInputStream(newFile(fileName)));
dIS.read(salt,0,salt.length);
int iterationCount = dIS.readInt();
System.out.println("Recovered salt: "+salt);//Different than above written salt
System.out.println("Recovered count: "+iterationCount);//Same as above
/*Snip some more commented out crypto */
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
启用加密代码后,我得到了一半解密的文件。没有它,我只会得到不一致的写入然后读取。
最佳答案
您只需使用其 toString()
实现即可打印出字节数组 - 它不显示数据,仅显示哈希码。
尝试使用Arrays.toString(salt)
代替:
System.out.println("Recovered salt: " + Arrays.toString(salt));
(写出来时也是如此。)
我怀疑你现在会发现你实际上一直在正确地读取盐值。
关于java - 写入结果不一致,然后使用字节数组和 DataOutputStream 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6244245/
代码在Java Tutorials显示了使用 DataOutputStream 类和 DataInputStream 类的示例。 代码片段如下所示: //.. out = new DataOutput
我怎样才能做类似ByteBuffer.putBoolean和ByteBuffer.getBoolean的事情? 与DataInputStream一样,有readBoolean,而DataOutputS
这个问题在这里已经有了答案: Java Socket why server can not reply client (4 个回答) 2年前关闭。 我有一个使用套接字与另一台计算机通信的 java 应
我在应用程序中使用此代码来发送一些字符串并抛出套接字。 public class OutgoingData { public static DataOutputStream dos = null; p
当我在没有 close 方法的情况下运行此代码时,服务器无法接收消息! 客户: Socket con = new Socket(InetAddress.getByName("local
DataOutputStream salida; salida = new DataOutputStream(socket.getOutputStream()); Error:(35, 24) ja
在下面的代码中。我正在将文件读入一个小缓冲区(len = CHUNK_SIZE),我只想将此缓冲区写入输出流。但即使我在每个 block 之后刷新,我也会遇到堆溢出异常。好吧,如果我想流式传输小文件,
我尝试使用 DataOutputStream 发送 POST 数据并获取响应数据。 我是这样编码的。 String urlParameters = "table=page&format=xml
所以我尝试将我的代码与 DataInputStream 和 BufferedReader 一起使用将字符串从客户端发送到服务器,并在整个互联网上查找资源来解决此问题。我正在使用一种从客户端读取字符串的
我正在开发服务器/客户端通信程序,但遇到了一个问题。当我尝试从客户端发送消息时,它无法正常工作。初始化服务器后,我连接客户端并且成功。当我尝试从客户端发送消息时,服务器不会收到它们。关闭客户端连接后,
DataOutputStream 类用于将原始数据类型写入二进制格式。我一直在使用DataOutputStream类的方法void writeChars(String s),使用程序将其写入文件中..
我正在尝试使用 Java 创建一个客户端/服务器程序,其中客户端将简单的字符串发送到服务器,并且连接到服务器的每个客户端都会获取此字符串。因此,我为服务器创建了 2 个服务器类,它们为每个客户端创建新
对于简单的 2D 游戏,我需要我的客户端程序始终将坐标发送到服务器。 因此,我创建了一个测试,看看是否可以通过从服务器发送和检索值来使两个玩家具有相同的速度。 package main; import
我在 Android 中的 DataOutputStream 上收到了一个 NullPointerException。 我首先编写了一些代码,使用 DataOutputStream 在 Java 中写
我正在为包装 TCP 套接字的类编写单元测试(使用 Mockito 作为我的模拟库)。问题是任何在 DataOutputStream 上调用 writeBytes 的测试都会抛出空指针异常。 我在下面
我有一个基本的聊天应用程序。有 3 个类:服务器、客户端和 clientHandler。问题是服务器只能第一次读取(使用 DataInputStream)然后抛出异常。 Hello. I have a
我正在编写一个客户端服务器应用程序,但是当它们从服务器端发送时,我在客户端没有收到相同的字节。在服务器端,我使用了 .write(bytes[]) 方法。在客户端,我使用了 .readFully(by
我正在为 HTTP post 请求将单个字节写入 java 中的 DataOutputStream。帖子的结构如下: /* Init Post */ URL PostToLink = new URL(
我正在尝试将一个大字符串写入我的 DataOutputStream,但是我收到了一个 UTFDataFormatException,上面写着: 长度超过 65535 个 UTF 字节的字符串 当我打电
我制作了一个简单的服务器应用程序,用于将图像发送到连接的客户端。我在主线程中有一个循环,它接受传入的连接,并从相应的套接字创建一个 Client 对象。我有一份活跃客户名单。每次有新图片可用时,我都会
我是一名优秀的程序员,十分优秀!