我正在尝试解密用Python脚本加密的WAV文件。我在 Java 和 Python 中使用相同的硬编码密钥。我在两个不同的地方使用两个不同的 Python 脚本,Java 中的解密适用于使用一个 Python 脚本加密的 WAV 文件,但它不适用于另一个 Python 脚本 WAV 文件。我正在分享这两个 Python 脚本。 这是 Java 解密所用的 Python 脚本 Python 脚本 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
这是脚本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) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
}
这是我在邮递员中收到的小回复样本
所以我的问题是为什么Java解密方法适用于脚本A但不适用于脚本B?我在这里做错了什么
感谢您的帮助。我能够找到原因或问题。我试图在 Postman 中播放音频 wav 文件,但 API 返回随机数据,如上图所示,由于某种原因,Postman 无法处理要播放的文件。我以为解密没有正确完成。
在我的 Web 应用程序(语音录制解决方案)中部署并运行代码后,解密和音频文件就可以播放。
再次感谢大家的帮助。
PS:两个脚本都可以工作。