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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:23:23+00:00 2026-05-16T08:23:23+00:00

I want to encrypt a string using AES with my own key. But I’m

  • 0

I want to encrypt a string using AES with my own key. But I’m having trouble with the bit length of the key. Can you review my code and see what I need to fix/change.

public static void main(String[] args) throws Exception {
    String username = "bob@google.org";
    String password = "Password1";
    String secretID = "BlahBlahBlah";
    String SALT2 = "deliciously salty";

    // Get the Key
    byte[] key = (SALT2 + username + password).getBytes();
    System.out.println((SALT2 + username + password).getBytes().length);

    // Need to pad key for AES
    // TODO: Best way?

    // Generate the secret key specs.
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");

    // Instantiate the cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

    byte[] encrypted = cipher.doFinal((secrectID).getBytes());
    System.out.println("encrypted string: " + asHex(encrypted));

    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    byte[] original = cipher.doFinal(encrypted);
    String originalString = new String(original);
    System.out.println("Original string: " + originalString + "\nOriginal string (Hex): " + asHex(original));
}

Right now I get an exception “Invalid AES key length: 86 bytes“. Do I need to pad my key? How should I do it?

Also do I need to set anything for ECB or CBC?

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-05-16T08:23:24+00:00Added an answer on May 16, 2026 at 8:23 am

    Edit:

    As written in the comments the old code is not “best practice”.
    You should use a keygeneration algorithm like PBKDF2 with a high iteration count.
    You also should use at least partly a non static (meaning for each “identity” exclusive) salt. If possible randomly generated and stored together with the ciphertext.

        SecureRandom sr = SecureRandom.getInstanceStrong();
        byte[] salt = new byte[16];
        sr.nextBytes(salt);
    
        PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 1000, 128 * 8);
        SecretKey key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(spec);
        Cipher aes = Cipher.getInstance("AES");
        aes.init(Cipher.ENCRYPT_MODE, key);
    

    ===========

    Old Answer

    You should use SHA-1 to generate a hash from your key and trim the result to 128 bit (16 bytes).

    Additionally don’t generate byte arrays from Strings through getBytes() it uses the platform default Charset. So the password “blaöä” results in different byte array on different platforms.

    byte[] key = (SALT2 + username + password).getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    key = sha.digest(key);
    key = Arrays.copyOf(key, 16); // use only first 128 bit
    
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    

    Edit:
    If you need 256 bit as key sizes you need to download the “Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files” Oracle download link, use SHA-256 as hash and remove the Arrays.copyOf line.
    “ECB” is the default Cipher Mode and “PKCS5Padding” the default padding.
    You could use different Cipher Modes and Padding Modes through the Cipher.getInstance string using following format: “Cipher/Mode/Padding”

    For AES using CTS and PKCS5Padding the string is: “AES/CTS/PKCS5Padding”

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

Sidebar

Related Questions

I'm using Crypto++ to encrypt string with AES. Ok it works fine, but now
I'm trying to encrypt/decrypt a string using 128 bit AES encryption (ECB). What I
I use some code to encrypt & decrypt string in C# but i want
I am having some problems in using CryptoStream when I want to encrypt a
I want to create an application that can encrypt and decrypt my passwords using
I want to encrypt an arbitrary-length string with a password in Python. I would
I want to encrypt a text file using AES encryption. However,I am not so
i want to encrypt string in java and decrypt the same string in C#
I want to encrypt a string and embed it in a URL, so I
I want to encrypt a URL variable so that the user can't see 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.