Hier ist das Python-Skript, mit dem die Java-Entschlüsselung funktioniert
Python Script A
Code: Select all
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
Code: Select all
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)
Code: Select all
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)