[英]byte array to wav file
编辑:谢谢大家! 问题是我们必须逐块解密并保存原始文件格式。
我正在尝试按 16 字节大小的块加密声音文件。 然后将其解密并输出解密后的文件。 它向我尝试保存的解密文件引发“不支持格式的流”异常的问题。
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);
帮助?
这不会解决您的问题,但会为您提供结构化和可维护的方法。 您的加密很可能与解密不匹配。
您可能希望用Apache commons-crypto替换您的加密工作。 查看他们的字节数组加密/解密示例。 但是由于您想流式传输结果,因此查看byte stream 的加密/解密可能会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.