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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:38:09+00:00 2026-05-30T05:38:09+00:00

How do I fix this it keeps throwing exceptions. As you can see i

  • 0

How do I fix this it keeps throwing exceptions. As you can see i am trying to use an image as a password would you please help program of fix my encrypt/decrypt method so this works. I need help my current code is as follows:

import java.awt.image.*;
import java.io.*;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
import javax.swing.*;

/**
*
* @author Lance Gerday
*/
public class Encrypt {

   private static final String ALGORITHM = "AES";
   public static byte[] keyValue;
   // 500 KB max
   public static byte[] valuesRead = new byte[512000];

   public static void encrypt(File f) throws Exception {
       FileInputStream in = null;
       FileOutputStream out = null;
       in = new FileInputStream(f);
       Key key = generateKey();
       Cipher c = Cipher.getInstance(ALGORITHM);
       c.init(Cipher.ENCRYPT_MODE, key);//my code seems to fail here


       String name = f.getName();
       String newFileName = name.substring(0, name.lastIndexOf("."))
               + ".enc" + name.substring(name.lastIndexOf("."), name.length());
       File newFile = new File(f.getParentFile(), newFileName);
       out = new FileOutputStream(newFile);
       //reads the file into valueToEnc and returns the number of bytes read
       valuesRead = new byte[Integer.MAX_VALUE];
       int numberRead = in.read(valuesRead);
       keyValue = new byte[numberRead];
       for (int i = 0; i < numberRead; i++) {
           keyValue[i] = valuesRead[i];
       }
       byte[] encValue = c.doFinal(keyValue);
       String encryptedValue = new BASE64Encoder().encode(encValue);
       out.write(encryptedValue.getBytes());
   }

   public static void decrypt(File f) throws Exception {
       Key key = generateKey();
       Cipher c = Cipher.getInstance(ALGORITHM);
       c.init(Cipher.DECRYPT_MODE, key);

       FileInputStream in = null;
       FileOutputStream out = null;

       if (f.canRead()) {
           in = new FileInputStream(f);
       }

       String name = f.getName();
       String newFileName = name.substring(0, name.lastIndexOf(".enc"));
       File newFile = new File(f.getParentFile(), newFileName);
       out = new FileOutputStream(newFile);
       //reads the file into valueToEnc and returns the number of bytes read
       valuesRead = new byte[Integer.MAX_VALUE];
       int numberRead = in.read(valuesRead);
       keyValue = new byte[numberRead];
       for (int i = 0; i < numberRead; i++) {
           keyValue[i] = valuesRead[i];
       }
       String encryptedValue = new String(keyValue);
       byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
       byte[] decValue = c.doFinal(decordedValue);
       out.write(decValue);
   }

   private static Key generateKey() throws Exception {
       Key key = new SecretKeySpec(keyValue, ALGORITHM);
       return key;
   }

   public static void setKeyValue(File f) {
       BufferedImage img = null;
       try {
           img = javax.imageio.ImageIO.read(f);
       } catch (Exception e) {
           JOptionPane.showMessageDialog(null, "Fail error at line 92");
       }
       Raster r = img.getData();
       int[] data = r.getPixels(r.getMinX(), r.getMinY(), r.getWidth(), r.getHeight(), (int[]) null);
       for (int a : data) {
       }
       int dataLength = data.length;
       keyValue = new byte[dataLength << 2];

       for (int i = 0; i < dataLength; i++) {
           int x = data[i];
           int k = i << 2;
           keyValue[k++] = (byte) ((x >>> 0) & 0xff);
           keyValue[k++] = (byte) ((x >>> 8) & 0xff);
           keyValue[k++] = (byte) ((x >>> 16) & 0xff);
           keyValue[k++] = (byte) ((x >>> 24) & 0xff);
       }
   }
}
  • 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-30T05:38:10+00:00Added an answer on May 30, 2026 at 5:38 am

    Though you don’t really ask for a question, your secret key isn’t really standard. Usually the way to generate is:

    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128);  // or 192 or 256
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    

    Reference AES and JCE

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

Sidebar

Related Questions

I've been trying to fix this for two hours straight and I can't figure
I have been trying to fix this problem for hours. My app keeps crashing
How can I fix this? REGEX: //REGEX $match_expression = '/Rt..tt<\/td> <td>(.*)<\/td>/'; preg_match($match_expression,$text,$matches1); $final =
How would you fix this code? template <typename T> void closed_range(T begin, T end)
How can I fix this error, and install thin or other gems? $ sudo
Ok I want some opinions how I can fix this mess of a method!
Could someone please help me to understand why the following block of code keeps
I have been trying to fix this for a while but I have had
How do I fix this so that when php calls $fontcolor and the user
Any ideas how to fix this? UserService.UserServiceClient userServiceClient = new UserServiceClient(); userServiceClient.GetUsersCompleted += new

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.