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

  • Home
  • SEARCH
  • 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 699255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:21:59+00:00 2026-05-14T03:21:59+00:00

I’m trying to adapt this DES encrypting example to AES, so I made the

  • 0

I’m trying to adapt this DES encrypting example to AES, so I made the changes, and it try to run this:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

// Adapted from http://www.exampledepot.com/egs/javax.crypto/DesFile.html
public class AesEncrypter {

    private Cipher ecipher;
    private Cipher dcipher;

    // Buffer used to transport the bytes from one stream to another
    private byte[] buf = new byte[1024];

    public AesEncrypter(SecretKey key) throws Exception {
 // Create an 8-byte initialization vector
 byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A };
 AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

 ecipher = Cipher.getInstance("AES/CBC/NoPadding");
 dcipher = Cipher.getInstance("AES/CBC/NoPadding");

 // CBC requires an initialization vector
 ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
 dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    }

    public void encrypt(InputStream in, OutputStream out) throws Exception {
 // Bytes written to out will be encrypted
 out = new CipherOutputStream(out, ecipher);

 // Read in the cleartext bytes and write to out to encrypt
 int numRead = 0;
 while ((numRead = in.read(buf)) >= 0) {
     out.write(buf, 0, numRead);
 }
 out.close();
    }

    public void decrypt(InputStream in, OutputStream out) throws Exception {
 // Bytes read from in will be decrypted
 in = new CipherInputStream(in, dcipher);

 // Read in the decrypted bytes and write the cleartext to out
 int numRead = 0;
 while ((numRead = in.read(buf)) >= 0) {
     out.write(buf, 0, numRead);
 }
 out.close();
    }

    public static void main(String[] args) throws Exception {
 System.out.println("Starting...");

 SecretKey key = KeyGenerator.getInstance("AES").generateKey();

 InputStream in = new FileInputStream(new File("/home/wellington/Livros/O Alienista/speechgen0001.mp3/"));
 OutputStream out = System.out;

 AesEncrypter encrypter = new AesEncrypter(key);
 encrypter.encrypt(in, out);

 System.out.println("Done!");
    }

}

but I got the Exception:

InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long

So I tried to solve by changing the

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

for

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv, 0, 16);

but results in

IV buffer too short for given offset/length combination

I can just go trying till it works, but I would like to hear from who work with AES what is the commonly used buffer size?

  • 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-05-14T03:21:59+00:00Added an answer on May 14, 2026 at 3:21 am

    I think it’s saying it wants 16 bytes, but this is only 8 bytes:

    byte[] iv = new byte[] {
      (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A
    };
    

    Maybe try this?

    byte[] iv = new byte[] {
      (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A,
      (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.