Расшифровка wav-файла в Java не работает, файлы, зашифрованные с помощью скрипта PythonPython

Программы на Python
Ответить
Anonymous
 Расшифровка wav-файла в Java не работает, файлы, зашифрованные с помощью скрипта Python

Сообщение Anonymous »

Я пытаюсь расшифровать файл WAV, зашифрованный с помощью скрипта Python. Я использую один и тот же жестко закодированный ключ как в Java, так и в Python. Я использую два разных сценария Python в двух разных местах. Дешифрование на Java работает с WAV-файлами, зашифрованными с помощью одного сценария Python, но не работает с другим WAV-файлом сценария Python. Я делюсь обоими скриптами Python.
Вот скрипт Python, с помощью которого работает расшифровка Java
Python Script A

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

predefined_key = bytes.fromhex('ABC')

def encrypt_file(mixedRecordingPathName, key):
iv = b'1234567890123456'
backend = default_backend()
cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=backend)
encryptor = cipher.encryptor()

with open(mixedRecordingPathName, 'rb') as f:
data = f.read()

padder = padding.PKCS7(algorithms.AES.block_size).padder()
padded_data = padder.update(data) + padder.finalize()

encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

encrypted_file_path = mixedRecordingPathName

with open(encrypted_file_path, 'wb') as f:
f.write(iv + encrypted_data)

return encrypted_file_path
Вот Script B (для которого расшифровка Java не работает)

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

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
import sys

def encrypt_file(mixedRecordingPathName, key):
iv = b'1234567890123456'
backend = default_backend()
cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=backend)
encryptor = cipher.encryptor()

with open(mixedRecordingPathName, 'rb') as f:
data = f.read()

padder = padding.PKCS7(algorithms.AES.block_size).padder()
padded_data = padder.update(data) + padder.finalize()

encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

decrypted_file_path = mixedRecordingPathName

with open(decrypted_file_path, 'wb') as f:
f.write(iv + encrypted_data)

return decrypted_file_path

if __name__ == "__main__":
key = bytes.fromhex('ABC')
mixedRecordingPathName = sys.argv[1]
encrypt_file(mixedRecordingPathName, key)

А вот код Java, в котором я расшифровываю волновые файлы, используя тот же ключ

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

private boolean decrypt(int cipherMode, String hexKey, File inputFile, File outputFile) {
boolean fileProcessedSuccessfully = false;
FileOutputStream outputStream = null;
FileInputStream inputStream = null;

try {
byte[] keyBytes = hexStringToByteArray(hexKey);
Key secretKey = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");

inputStream = new FileInputStream(inputFile);

byte[] iv = new byte[16];
inputStream.read(iv); // read IV from the start of the file
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(cipherMode, secretKey, ivSpec);

byte[] inputBytes = new byte[(int)inputFile.length() - 16];
inputStream.read(inputBytes);

byte[] outputBytes = cipher.doFinal(inputBytes);
outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
fileProcessedSuccessfully = true;
} catch (Exception e) {
logger.error("Exception Occurred While Decrypting File: " + inputFile.getName(), e);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
logger.error("Exception Occurred while Closing Input Stream after Recording Decryption: " + inputFile.getName(), e);
}

try {
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
logger.error("Exception Occurred while Closing Output Stream after Recording Decryption: " + inputFile.getName(), e);
}
}

return fileProcessedSuccessfully;
}

private byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) 

Подробнее здесь: [url]https://stackoverflow.com/questions/79335658/decryption-of-wav-file-in-java-doesnt-work-the-files-encrypted-with-python-scri[/url]
Ответить

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

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

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

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

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