繁体   English   中英

字节数组到 wav 文件

[英]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);

帮助?

  1. 创建单独的方法:一种加载音频,一种加密,一种解密,一种播放音频。
  2. 创建单元测试,以便您可以验证加密和解密(解密后的值必须与加密前的值相同)
  3. 您可能已经有工作代码来加载和播放音频
  4. 将所有四个方法调用放入您的测试应用程序中,看看它是否仍然可以播放声音

这不会解决您的问题,但会为您提供结构化和可维护的方法。 您的加密很可能与解密不匹配。

您可能希望用Apache commons-crypto替换您的加密工作。 查看他们的字节数组加密/解密示例。 但是由于您想流式传输结果,因此查看byte stream 的加密/解密可能会更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM