gpt4 book ai didi

Java解密的文件被破坏

转载 作者:行者123 更新时间:2023-11-30 04:38:00 25 4
gpt4 key购买 nike

我有一个程序必须加密音频文件,然后在需要时对其进行解密。我在其他一些类型的文件(例如 .bin 或 .txt)上测试了我的程序。我遇到的问题是,解密的文件在实际内容之前有一些奇怪的字符,例如源文件包含“010101”,而加密解密后它有“Øíw0w 010101”。

我的加密方法代码如下:

public void cipherTheAudioFile(String fileDir, String fileToCipher) throws            FileNotFoundException, IOException, NoSuchAlgorithmException, InvalidKeySpecException,  InvalidKeyException, NoSuchPaddingException {
File audioSourceFile = new File(fileDir + "\\" + fileToCipher);
ObjectOutputStream oos = new ObjectOutputStream(
new CipherOutputStream(new FileOutputStream(
new java.io.File("").getAbsolutePath().toString() + "/encrypted/" + fileToCipher + ".sky"), cipher));

byte[] audioFileInBytes = FileUtils.readFileToByteArray(audioSourceFile);
oos.write(audioFileInBytes);

fos = new FileOutputStream(KEY_FILE);
SecretKeyFactory skf = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
DESKeySpec keyspec = (DESKeySpec) skf.getKeySpec(key, DESKeySpec.class);
fos.write(keyspec.getKey());

fos.close();
oos.close();
}

我的解密方法代码如下:

public void decryptTheAudioFile(String fileDir, String fileToDecipher) throws NoSuchAlgorithmException, NoSuchPaddingException, FileNotFoundException, IOException, ClassNotFoundException, InvalidKeySpecException, InvalidKeyException {
fis = new FileInputStream(keyFile);
byte[] keyspecbytes = new byte[fis.available()];

File fileToWriteIn = createFileToWriteIn(fileDir, fileToDecipher);

fis.read(keyspecbytes);
SecretKeyFactory skf = SecretKeyFactory.getInstance(encryptionAlgorithm);
DESKeySpec keyspec = new DESKeySpec(keyspecbytes);
SecretKey key = skf.generateSecret(keyspec);
Cipher cipher = Cipher.getInstance(encryptionAlgorithm);
cipher.init(Cipher.DECRYPT_MODE, key);

ObjectInputStream ois = new ObjectInputStream(
new CipherInputStream(
new FileInputStream(new java.io.File("").getAbsolutePath().toString() + "/encrypted/" + fileToDecipher + ".sky"), cipher));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileToWriteIn));

byte[] audioFileInBytes = new byte[1024];

int numRead = 0;
while ((numRead = ois.read(audioFileInBytes)) >= 0) {
oos.write(audioFileInBytes, 0, numRead);
}

oos.close();
fis.close();
ois.close();
}

附注这可能与编码有关,但我不太确定。

已编辑

好吧,我已经更改为FileWriters,但仍然没有变化。代码如下:

 OutputStream os = new FileOutputStream(new java.io.File("").getAbsolutePath().toString() + "/encrypted/" + fileToCipher + ".sky");
CipherInputStream cis = new CipherInputStream(new FileInputStream(audioSourceFile), cipher);
byte[] audioFileInBytes = new byte[1024];
int numRead = 0;
while ((numRead = cis.read(audioFileInBytes)) >= 0) {
os.write(audioFileInBytes, 0, numRead);
}

解密器也是如此。

最佳答案

问题出在 decryptTheAudioFile 方法写入文件的方式上。具体来说,问题在于它使用的是ObjectOutputStream。那就是添加一个对象序列化 header 。但它根本不属于那里。

解决方案是从 decryptTheAudioFile 中摆脱这个:

ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(fileToWriteIn));

并将其替换为:

OutputStream os = new FileOutputStream(fileToWriteIn);

并更改其余代码以写入os。您的代码需要反射(reflect)您在 cipherTheAudioFile 中读取文件的方式。

<小时/>

最好也摆脱其他 ObjectStream 实例并简单地读取和写入普通的 Streams。其他 ObjectStream 是无害的(大部分),但它们实际上没有实现任何目标。

关于Java解密的文件被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12955003/

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