[英]byte array to wav file
EDIT: Thanks everyone!编辑:谢谢大家! The problem was we had to decrypt block by block and save the original file format.问题是我们必须逐块解密并保存原始文件格式。
I'm trying to encrypt a sound file by blocks of 16 bytes size.我正在尝试按 16 字节大小的块加密声音文件。 Then decrypt it back and output the decrypted file.然后将其解密并输出解密后的文件。 The problem it throws "Stream of unsupported format" exception to the decrypted file I'm trying to save.它向我尝试保存的解密文件引发“不支持格式的流”异常的问题。
public class Main {
public static void main(String[] args)
throws InvalidKeyException, UnsupportedAudioFileException, IOException, LineUnavailableException {
File decryptedFile = new File("dec.wav");
File soundFile = new File("C:\\Users\\Daniel\\Desktop\\FROG1\\peeper5sec.wav");
String k = "KEY";
byte[] key = k.getBytes();
Object keyKey=frog_Algorithm.makeKey(key);
AudioInputStream st = AudioSystem.getAudioInputStream(soundFile);
byte[][] fileInByte = split(st.readAllBytes(),16);
if (fileInByte == null)
fileInByte[0] = st.readAllBytes();
byte enc[]=null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
for (int i = 0; i < fileInByte.length; i++) {
byte tmp3[]=frog_Algorithm.blockEncrypt(fileInByte[i], 0, keyKey);
outputStream.write(tmp3);
}
enc=outputStream.toByteArray();
FileOutputStream fos=new FileOutputStream(decryptedFile.getAbsolutePath());
//fos.AudioSystem.write(frog_Algorithm.blockDecrypt(enc, 0,keyKey));
//fos.close();
byte[] dec=frog_Algorithm.blockDecrypt(enc, 0,keyKey);
for (int i = 0; i < dec.length; i++) {
fos.write(dec[i]);
}
ByteArrayInputStream bais = new ByteArrayInputStream(dec);
AudioInputStream stream = AudioSystem.getAudioInputStream(bais);
AudioSystem.write(stream, getAudioFileFormat(stream), decryptedFile);
Help?帮助?
This will not solve your problem but give you a structured and maintainable approach.这不会解决您的问题,但会为您提供结构化和可维护的方法。 Very likely your encryption does not match the decryption.您的加密很可能与解密不匹配。
You may want to replace your crypto efforts withApache commons-crypto .您可能希望用Apache commons-crypto替换您的加密工作。 Look at their example for encryption/decryption of byte array .查看他们的字节数组加密/解密示例。 But since you want to stream the results, it may be even better to look at encryption/decryption of byte stream .但是由于您想流式传输结果,因此查看byte stream 的加密/解密可能会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.