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 458929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:43:19+00:00 2026-05-12T22:43:19+00:00

i am really not able to figure this out myself so please help me

  • 0

i am really not able to figure this out myself so please help me out.
this is a program to encrypt contents of an existing file keeper.txt using 128bit AES putting the encrypted text into newly created file called Encrypted.txt, then decrypting the contents of Encrypted.txt into newly created file called Decrypted.txt

everytime this program is run it generates a random key for the encryption.

i am just trying to figure out if i have to give somebody the Encrypted.txt file and how can he decrypt the file later using this code or by slightly modifying this code .

i think its not possible to send him the key generated by this program .. is it ?
cause when i try to print the key using system.out it doesn’t give the key.

help me out please

package org.temp2.cod1;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;

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

import java.security.spec.AlgorithmParameterSpec;

public class AESEncrypter
{
Cipher ecipher;
Cipher dcipher;

public AESEncrypter(SecretKey key)
{
// Create an 8-byte initialization vector
byte[] iv = new byte[]
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
try
{
ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

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

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

public void encrypt(InputStream in, OutputStream out)
{
try
{
// 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();
}
catch (java.io.IOException e)
{
}
}

public void decrypt(InputStream in, OutputStream out)
{
try
{
// 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();
}
catch (java.io.IOException e)
{
}
}

public static void main(String args[])
{
try
{
// Generate a temporary key. In practice, you would save this key.
// See also e464 Encrypting with DES Using a Pass Phrase.

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

// Create encrypter/decrypter class
AESEncrypter encrypter = new AESEncrypter(key);

// Encrypt
encrypter.encrypt(new FileInputStream("C:\\keeper.txt"),new FileOutputStream("C:\\Encrypted.txt"));
// Decrypt
encrypter.decrypt(new FileInputStream("C:\\Encrypted.txt"),new FileOutputStream("C:\\Decrypted.txt"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
} 
  • 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-12T22:43:19+00:00Added an answer on May 12, 2026 at 10:43 pm

    You can get encoded version of the key by calling

       byte[] keyBytes = secretKey.getEncoded();
    

    For AES, there is not really any encoding involved so you will get the raw bytes (16 bytes for 128-bit). The keyBytes is binary so you can’t print it. You can hex or base64 encode it if you need to send it in text format. The other party can reconstitute the key like this,

      SecretKey key = new SecretKeySpec(keyBytes, "AES");
    

    I answered another your question yesterday regarding Sun’s AES example. This is where the author was confused. He did both steps at the same time.

    Often, it’s easier for human to remember if you use password as the secret and generate the key from password. This is called PBE (Password-Based Encryption). Following is the code I use to generate keys,

       public static SecretKey getAesKey(char[] password, int keyLength)
       throws GeneralSecurityException {
    
      int count = 128; // Iteration count
      byte[] salt;
      try {
       salt = "This is a fixed salt string".getBytes("UTF-8");
      } catch (UnsupportedEncodingException e) {
       throw new IllegalStateException("No UTF-8");
      }
      PBEKeySpec keySpec = new PBEKeySpec(password, salt, count, keyLength);
      SecretKeyFactory skf = SecretKeyFactory
        .getInstance("PBKDF2WithHmacSHA1");
      SecretKey pbeKey = skf.generateSecret(keySpec);
      byte[] raw = pbeKey.getEncoded();
      return new SecretKeySpec(raw, "AES");
     }
    

    One more recommendation to your code, it’s much more secure if you use a different random IV every time. IV doesn’t need to be secret so you can prepend it to your ciphertext.

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

Sidebar

Related Questions

I'm not really asking about how programmers learn how to program. More about specific
I am not really familiar with Maven program but I've been using Eclipse for
I'm not really sure how to google this one . I would like to
I'm not really sure how to title this question but basically I have an
Is there really not a TSpinEdit control for floats in Delphi? It looks like
I'm working on a project which I'm really not sure how to unit test.
Not really getting the point of the map function. Can anyone explain with examples
(Not really a programming question, sorry) I'm working on benchmarking various filesystems (most importantly:
Not really a programming question, but relevant to many programmers... Let's say I have
NHibernate is not really a good fit for our environment due to all the

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.