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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:43:28+00:00 2026-05-24T01:43:28+00:00

So basically I’m encrypting an image from asses folder and after that I decrypt

  • 0

So basically I’m encrypting an image from asses folder and after that I decrypt it.I put Logs in a few places in code so I can see the real size of InputStream, bytes after the copying inputstream in bytearrayoutputstream, the size of image after encryption and after decryption (i think there is no problem with encryption/decryption,but not really sure).And the problem is when I run the program, I get this in LogCat :

07-27 13:41:08.091: VERBOSE/Size(10546): Size of inputstream 29199
07-27 13:41:17.340: WARN/ActivityManager(52): Launch timeout has expired, giving up wake lock!
07-27 13:41:17.670: WARN/ActivityManager(52): Activity idle timeout for HistoryRecord{44defa50 com.example.pbe/.PBEencryptdecryptActivity}

and thats it.The code I’m using is :

package com.example.aes;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class PBEencryptdecryptActivity extends Activity {
    private int IO_BUFFER_SIZE;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        KeyGenerator keygen;
        try {
            keygen = KeyGenerator.getInstance("AES");
            SecretKey aesKey = keygen.generateKey();
            Cipher aesCipher,aesCipherDec;

            AssetManager am = this.getAssets();
            InputStream is = am.open("007FRAMESUPERIOR.jpg"); // get the encrypted image from assets folder
            Log.v("Size","Size of inputstream "+is.available());


            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            byte[] b = new byte[IO_BUFFER_SIZE];  

            int read;  
            while ((read = is.read(b)) != -1) {  //convert inputstream to bytearrayoutputstream
                baos.write(b, 0, read);
            }
            Log.v("Size","Size of b "+b.length);

            aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");     // Create the cipher            
            aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);    // Initialize the cipher for encryption                         
            byte[] ciphertext = aesCipher.doFinal(b);   // Encrypt the cleartext
            Log.v("Size","Size of image encrypted "+ciphertext.length);


            aesCipherDec =  Cipher.getInstance("AES/ECB/PKCS5Padding");
            aesCipherDec.init(Cipher.DECRYPT_MODE, aesKey); // Initialize the same cipher for decryption                        
            byte[] cleartext1 = aesCipher.doFinal(ciphertext);  // Decrypt the ciphertext                   

            //Bitmap bitmap = BitmapFactory.decodeByteArray(cleartext1 , 0,  cleartext1.length);    //decoding bytearrayoutputstream to bitmap
            Log.v("Size","Size of image decrypted "+cleartext1.length);

        } catch (Exception e) {
            e.printStackTrace();
            Log.v("Error", "Error Occured "+e);
        }

    }
}

Actually I’m thinking how can I set the size of private int IO_BUFFER_SIZE; so I can copy all inputstream to the outputstream without loosing any data.Any help will be really appreciate!

  • 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-24T01:43:29+00:00Added an answer on May 24, 2026 at 1:43 am

    I think there is a mistake: you are encrypting only the buffer not the content of the whole file.

    After the reading loop you have to call

    b = baos.tobyteArray();
    

    Afterwards you can reduce IO_BUFFER_SIZE to any number you want. Typically 4094 would be a good number.

    Additionally I would initialize the byteArrayOutputStream with the exact size you need:

    ByteArrayOutputStream baos = new ByteArrayOutputStream(is.available());  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically from a database I am getting data that is formatted like this nameofproject101
Basically I have an iframe loaded that is accessed from the parent whenever it
Basically I have some code to check a specific directory to see if an
Basically I'm going to go a bit broad here and ask a few questions
Basically I am trying to restart a service from a php web page. Here
Basically I’ve heard that certain conditions will cause .NET to blow past the finally
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
Basically I have written a game plugin that will allow server admins to update
Basically i have a query string that when i hardcode in the catalogue value
Basically what I have here is a button that adds a new list item

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.