gpt4 book ai didi

java - 写入和读取文件后字符串发生变化

转载 作者:行者123 更新时间:2023-11-30 07:18:52 25 4
gpt4 key购买 nike

我正在将一些数据写入文件(在本例中为字符串,但可以是任何内容,因为我使用的是 byte[]),之后我正在读取它并比较结果。当我比较两个 byte[] 时,它们对我来说看起来相同,但运行 .equals() 返回 false

请在下面找到我的代码:

private void sign(ActionEvent e) throws Exception {
byte[] data = read(file);

Signature sig = Signature.getInstance("SHA1withRSA");
sig.initSign(keyPair.getPrivate());
sig.update(data);
byte[] signatureBytes = sig.sign();

String newFilePath = "output.file";

int signatureLength = signatureBytes.length;
String signString = new BASE64Encoder().encode(signatureBytes);

RandomAccessFile raf = new RandomAccessFile(newFilePath, "rw");
raf.writeInt(signatureLength);
raf.write(signatureBytes);
raf.write(data);
raf.close();

verify(newFilePath, signatureBytes, data);
}

private void verify(String path, byte[] signature, byte[] originalMessage) throws Exception {
File f = new File(path);

RandomAccessFile raf = new RandomAccessFile(f.getPath(), "r");
int sigLength = raf.readInt();
byte[] signatureBytes = new byte[sigLength];
raf.read(signatureBytes);

Long remainingLength = raf.length() - 1 - sigLength;
byte[] message = new byte[remainingLength.intValue()];
raf.read(message);

raf.close();

System.out.println("==== VERIFYING SIGNATURE ====");
String originalString = new BASE64Encoder().encode(signature);
String newString = new BASE64Encoder().encode(signatureBytes);

System.out.println("originalString\n" + originalString);
System.out.println("newString\n" + newString);

System.out.println("Signatures match: " + newString.equals(originalString));

System.out.println("==== VERIFYING MESSAGE ====");
String originalMessageString = new String(originalMessage);
String messageString = new String(message);
System.out.println("originalMessageString = " + originalMessageString);
System.out.println("messageString = " + messageString);
System.out.println("Messages match: " + originalMessageString.equals(messageString));

Signature sig = Signature.getInstance("SHA1withRSA");
sig.initVerify(keyPair.getPublic());
sig.update(message);

System.out.println(sig.verify(signatureBytes)); // false
}

我得到以下输出:(该文件是一个包含字符串“Hoi”的txt文件)

==== VERIFYING SIGNATURE ====
originalString
DU00HopnNgOnwPL4Y50lwR5CJFVCRqsj3hkdyareMqHPe4RKdCUr2FSNEuh0yq5LyzcP96MNDzNJ
jVTTI8+V3SPMDzQIjw4Ibb+xM3/XZHQi5ej8XfVXao234QytRD5y1bWKxlZEh4BlnzXcqD9tmANB
BOQTG3P15Ea0fFkDYpg=
newString
DU00HopnNgOnwPL4Y50lwR5CJFVCRqsj3hkdyareMqHPe4RKdCUr2FSNEuh0yq5LyzcP96MNDzNJ
jVTTI8+V3SPMDzQIjw4Ibb+xM3/XZHQi5ej8XfVXao234QytRD5y1bWKxlZEh4BlnzXcqD9tmANB
BOQTG3P15Ea0fFkDYpg=
Signatures match: true
==== VERIFYING MESSAGE ====
originalMessageString = Hoi
messageString = Hoi
Messages match: false <------ WTF
false

任何有关解决此问题的帮助将不胜感激。

最佳答案

我猜你在messageString后面有一些空格。尝试一下

System.out.println("Messages match: " + originalMessageString.equals(messageString.trim()));

关于java - 写入和读取文件后字符串发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37917011/

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