简体   繁体   English

字节数组到 wav 文件

[英]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?帮助?

  1. Create separate methods: One to load audio, one to encrypt, one to decrypt, one to play the audio.创建单独的方法:一种加载音频,一种加密,一种解密,一种播放音频。
  2. Create unit tests so you can verify encryption and decryption (the value after decrypt must be the same as before encryption)创建单元测试,以便您可以验证加密和解密(解密后的值必须与加密前的值相同)
  3. You probably have working code to load and play audio already您可能已经有工作代码来加载和播放音频
  4. Put all four method calls into your test application and see if it still can play a sound将所有四个方法调用放入您的测试应用程序中,看看它是否仍然可以播放声音

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.

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