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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:43:58+00:00 2026-06-18T06:43:58+00:00

Possible Duplicate: InvalidKeyException Illegal key size public static byte[] encryptBytes(byte[] bytes, byte[] key) {

  • 0

Possible Duplicate:
InvalidKeyException Illegal key size

public static byte[] encryptBytes(byte[] bytes, byte[] key)
{
    Cipher cipher = null;

    try
    {
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        return Base64.encodeBase64(cipher.doFinal(bytes));
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return null;
}

public static byte[] decrpytBytes(byte[] encryptedData, String key)
{
    byte[] keyBytes = convertToByteArray(key);
    Cipher cipher = null;

    try
    {
        cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        return cipher.doFinal(Base64.decodeBase64(encryptedData));
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return null;
}
//Simply takes every other two characters an terms them into a byte value 
    //then stuffs them into  a byteArray
public static byte[] convertToByteArray(String key)
{
    byte[] b = new byte[key.length()/2];

    for(int i=0, bStepper=0; i<key.length()+2; i+=2)
        if(i !=0)
            b[bStepper++]=((byte) Integer.parseInt((key.charAt(i-2)+""+key.charAt(i-1)), 16));

    return b;
}

public static void main(String[] args) throws Exception
{
            //This string has 64 characters. When sent to convertToByteArray it returns a byte array or 32 bytes
    String key = "00112233445566778899AABBCCDDEEFF0123456789ABCDEF0123456789ABCDEF";

            //Test it out
    byte f[] = {2,4,7};
    byte[] encrypted = encryptBytes(f, convertToByteArray(key));
    byte[] unencrypted = decrpytBytes(encrypted, key);

    System.out.print(unencrypted[0]);
}

Error:

Illegal key size or default parameters

I dont know why I’m getting an invalid key size. It should be able to take 32 byte key with 256 encryption

  • 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-18T06:44:00+00:00Added an answer on June 18, 2026 at 6:44 am

    http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files.shtml

    “It turns out that the Cipher class will generally not allow encryption with a key size of more than 128 bits. The apparent reason behind this is that some countries (although increasingly fewer) have restrictions on the permitted key strength of imported encryption software, although the actual number 128 is questionable (see below). The good news is that:

    You can easily remove the restriction by overriding the security policy files with others that Sun provides.”

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

Sidebar

Related Questions

Possible Duplicate: objects classes and arrays - why is it returning ‘null’ ? [java]
Possible Duplicate: get name of a variable or parameter I have this function. public
Possible Duplicate: Get the size of a list in python? I need to be
Possible Duplicate: How to change stack size for a .NET program? I want to
Possible Duplicate: .NET Enumeration allows comma in the last field public enum SubPackageBackupModes {
Possible Duplicate: Find length (size) of an array in jquery I have a json
Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? I
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want

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.