Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8708465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:08:43+00:00 2026-06-13T04:08:43+00:00

I have a program that has to encrypt an audio file and then decrypt

  • 0

I have a program that has to encrypt an audio file and then decrypt it if needed. I tested my program on some other types of files, like .bin or .txt. The problem I get is that the decrypted file has some weird characters before the actual content, like the source file contains “010101” and after encryption-decryption it has “¬íw0w 010101”.

My encryption method code goes here:

public void cipherTheAudioFile(String fileDir, String fileToCipher) throws            FileNotFoundException, IOException, NoSuchAlgorithmException, InvalidKeySpecException,  InvalidKeyException, NoSuchPaddingException {
    File audioSourceFile = new File(fileDir + "\\" + fileToCipher);
    ObjectOutputStream oos = new ObjectOutputStream(
        new CipherOutputStream(new FileOutputStream(
            new java.io.File("").getAbsolutePath().toString() + "/encrypted/" + fileToCipher + ".sky"), cipher));

    byte[] audioFileInBytes = FileUtils.readFileToByteArray(audioSourceFile);
    oos.write(audioFileInBytes);

    fos = new FileOutputStream(KEY_FILE);
    SecretKeyFactory skf = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
    DESKeySpec keyspec = (DESKeySpec) skf.getKeySpec(key, DESKeySpec.class);
    fos.write(keyspec.getKey());

    fos.close();
    oos.close();
}

My decryption method code goes here:

public void decryptTheAudioFile(String fileDir, String fileToDecipher) throws NoSuchAlgorithmException, NoSuchPaddingException, FileNotFoundException, IOException, ClassNotFoundException, InvalidKeySpecException, InvalidKeyException {
    fis = new FileInputStream(keyFile);
    byte[] keyspecbytes = new byte[fis.available()];

    File fileToWriteIn = createFileToWriteIn(fileDir, fileToDecipher);

    fis.read(keyspecbytes);
    SecretKeyFactory skf = SecretKeyFactory.getInstance(encryptionAlgorithm);
    DESKeySpec keyspec = new DESKeySpec(keyspecbytes);
    SecretKey key = skf.generateSecret(keyspec);
    Cipher cipher = Cipher.getInstance(encryptionAlgorithm);
    cipher.init(Cipher.DECRYPT_MODE, key);

    ObjectInputStream ois = new ObjectInputStream(
        new CipherInputStream(
            new FileInputStream(new java.io.File("").getAbsolutePath().toString() + "/encrypted/" + fileToDecipher + ".sky"), cipher));
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileToWriteIn));

    byte[] audioFileInBytes = new byte[1024];

    int numRead = 0;
    while ((numRead = ois.read(audioFileInBytes)) >= 0) {
        oos.write(audioFileInBytes, 0, numRead);
    }

    oos.close();
    fis.close();
    ois.close();
}

P.S. It could be something with the encoding, but I am not really sure.

EDITED

Ok, I have changed to the FileWriters, but still no change. Here goes the code:

 OutputStream os = new FileOutputStream(new java.io.File("").getAbsolutePath().toString() + "/encrypted/" + fileToCipher + ".sky");
    CipherInputStream cis = new CipherInputStream(new FileInputStream(audioSourceFile), cipher);
    byte[] audioFileInBytes = new byte[1024];
    int numRead = 0;
    while ((numRead = cis.read(audioFileInBytes)) >= 0) {
            os.write(audioFileInBytes, 0, numRead);
    }

Likewise goes the decryptor.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:08:47+00:00Added an answer on June 13, 2026 at 4:08 am

    The problem is in the way that the decryptTheAudioFile method writes the file. Specifically, the problem is that it is using an ObjectOutputStream. That is adding an object serialization header. But it doesn’t belong there at all.

    The solution is to get rid of this from decryptTheAudioFile:

    ObjectOutputStream oos = new ObjectOutputStream(
             new FileOutputStream(fileToWriteIn));
    

    and replace it with this:

    OutputStream os = new FileOutputStream(fileToWriteIn);
    

    and change the rest of the code to write to os. Your code needs to mirror how you are reading the file in cipherTheAudioFile.


    It would be a good idea to get rid of the other ObjectStream instances too and simply read and write to plain Streams. The other ObjectStreams are harmless (mostly), but they don’t actually achieve anything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that imports a text file that has many entrys: ###
I have a program that has a file type associated with it (*.cqd). When
Hey, I have an iphone program that has a parser that parses some XML
I have a java program that has some number of classes. Three methods taken
I have program that has a variable that should never change. However, somehow, it
I have a program that loads a tab that has roughly 332 text boxes
I have a PHP program that has been written keeping in mind a single
I have a program in Octave that has a loop - running a function
I have a task killer program that has a listbox of current running processes.
I have a .NET 2.0 program that has a reference to Interop.WIA.dll (.NET), which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.