Как правильно расшифровать файл с помощью libsodium в Java (chacha20poly1305)?JAVA

Программисты JAVA общаются здесь
Ответить
Гость
 Как правильно расшифровать файл с помощью libsodium в Java (chacha20poly1305)?

Сообщение Гость »


Я хочу создать 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);
}
}
}

I will use the following command to run the decryption process:

Код: Выделить всё

java -jar decrypt.jar encrypted.jpg nonceHex keyHex additionalHex"
It appears that the decryption process works correctly when using the appropriate nonce, key, and additional values. However, when attempting to open the already decrypted file, I encountered difficulties and couldn't open it at all. The file seems to be corrupted, as indicated by the differing file sizes between the decrypted and original files.
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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»