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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:18:44+00:00 2026-06-02T15:18:44+00:00

I wrote an encrypt and a decrypt function. The encrypt works fine, but I

  • 0

I wrote an encrypt and a decrypt function. The encrypt works fine, but I always get IllegalBlockSizeException in the decrypt.

public static String aes_encrypt (String text, String key) 
{
    SecretKey skey = new SecretKeySpec(key.getBytes(), "AES"); 
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
    cipher.init(Cipher.ENCRYPT_MODE, skey);

    return new String((cipher.doFinal(text.getBytes())));

}

And here’s the decrypt function:

public static String aes_decrypt (String text, String key) 
{

    SecretKey skey = new SecretKeySpec(key.getBytes(), "AES"); 
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
    cipher.init(Cipher.DECRYPT_MODE, skey);

    return new String((cipher.doFinal(text.getBytes())));
}

Here’s the simple main method that tests this:

public static void main (String args[])
{
    String text = "Hello, world!";
    String key = "nv93h50sk1zh508v";
    String en, de;

    System.out.println("Text: " + text);
    System.out.println("Encrypted: " + (en = aes_encrypt(text, key)) 
            + " length = " + en.length());
    System.out.println("Decrypted: " + (de = aes_decrypt(en, key)));
}

Does anyone know how to “pad” the encrypted string properly so that I can decrypt it?
(I tried padding the string with 0 until the length is a multiple of 16, but got something like string not properly padded.)

Thanks

  • 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-02T15:18:47+00:00Added an answer on June 2, 2026 at 3:18 pm

    I think the problem is in your using the String constructor. This is converting to string using a text encoding mechanism, which may not preserve every value in the byte array – unsupported ones in the system default encoding may be discarded, leaving the encoded data shorter than it should be. If you want a string representation, convert to hex or base 64 encoding instead. And reverse whatever encoding you use here at the start of the decryption method.

    It’s not a padding issue – the encryption call will pad this fine, you are short of bytes because of your means of encoding the byte array to a string.

    You’ll find some base 64 instructions in answers to this SO question.

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

Sidebar

Related Questions

I wrote this code in C# to encrypt a string with a key: private
I'm trying to figure out how to encrypt / decrypt a string in VB.Net.
I am trying to encrypt/decrypt a string using eith Rijndael or Aes and the
I wrote these two classes that simple encrypt and decrypt strings: Encode.php class Encode
I encrypt and decrypt a string with a private key and following functions. so
I tried to encrypt data using this function in PHP and decrypt it with
I wrote Decrypt and Encrypt with C# and used it in my project, and
I'm using javax.crypto.Cipher to encrypt a file and then decrypt it but I'm still
I am using Blowfish algorithm to encrypt and decrypt the contents of the text.
I am trying to encrypt some plain text with a public key using RSA_public_encrypt(),

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.