Я хочу создать jar-файл, который можно использовать для расшифровки файла. Код показан ниже:
Код: Выделить всё
package org.example;
import com.goterl.lazysodium.LazySodium;
import com.goterl.lazysodium.LazySodiumJava;
import com.goterl.lazysodium.SodiumJava;
import com.goterl.lazysodium.interfaces.AEAD;
import com.goterl.lazysodium.utils.Key;
import javax.crypto.AEADBadTagException;
import javax.xml.bind.DatatypeConverter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
if (args.length != 4) {
System.out.println("Usage: java -jar YourJarFileName.jar cryptoContentFilePath nonceHex keyHex additionalHex");
System.exit(1);
}
String cryptoContentFilePath = args[0];
String nonceHex = args[1];
String keyHex = args[2];
String additionalHex = args[3];
byte[] nonceBytes = DatatypeConverter.parseHexBinary(nonceHex);
byte[] keyBytes = DatatypeConverter.parseHexBinary(keyHex);
Key key = Key.fromBytes(keyBytes);
LazySodium sodium = new LazySodiumJava(new SodiumJava());
try (FileInputStream fis = new FileInputStream(cryptoContentFilePath)) {
byte[] cryptoContentBytes = new byte[fis.available()];
fis.read(cryptoContentBytes);
String rawData = sodium.decrypt(new String(cryptoContentBytes), additionalHex, nonceBytes, key, AEAD.Method.CHACHA20_POLY1305);
System.out.println(rawData);
// Write decrypted content to a text file
writeToFile("decrypted_output.jpg", rawData);
} catch (IOException | AEADBadTagException e) {
throw new RuntimeException(e);
}
}
private static void writeToFile(String filePath, String content) {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(content.getBytes());
} catch (IOException e) {
throw new RuntimeException("Error writing to file", e);
}
}
}
Код: Выделить всё
java -jar decrypt.jar encrypted.jpg nonceHex keyHex additionalHex"
Upon inspecting the byte text of both files using VS Code, I observed that they contain exactly the same content. This suggests that the decryption logic is functioning properly. The issue seems to be related to preserving metadata in the decrypted file.
It seems that my decrypted file lacks metadata. For instance, when decrypting an audio file, the resulting decrypted file does not include information such as duration or sample rate, which is present in the original file.
What am I doing wrong? How to properly decrypt a file using libsodium. Thank you.
Источник: https://stackoverflow.com/questions/781 ... 20poly1305
Мобильная версия