簡體   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