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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:22:46+00:00 2026-05-26T06:22:46+00:00

How can I encrypt an entire string with AES. The code that I have

  • 0

How can I encrypt an entire string with AES. The code that I have below only encrypts up to the first space recognized :(. How can I fix this? Thanks

SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    String result = new String(cipher.doFinal(message.getBytes()));
    System.out.println("Encrypted:" + result);

EDIT
OMG I CANT BELIEVE THIS, HOW COULD I MISS THIS 🙁 ITS BECAUSE MY SCANNER WAS TAKING next instead of nextLine… how embarrassing this bugged me all day and only now did i actually think about checking that. Problem solved 🙂 Thanks everyone

  • 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-26T06:22:47+00:00Added an answer on May 26, 2026 at 6:22 am

    I don’t see anything wrong with your code except trying to print an arbitrary byte[] using new String(byte[]). Try this on for size:

    public static byte[] encrypt(String message) throws Exception
    {
        String salt = "1111111111111111";
        SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(message.getBytes());
    }
    
    public static void main (String[] args) throws Exception
    {
        String hello = Arrays.toString(encrypt("hello"));
        System.out.println("hello:" + hello);
        String helloWorld = Arrays.toString(encrypt("hello world"));
        System.out.println("hello world:" + helloWorld);
    }
    

    Which prints:

    hello:[115, -73, -46, -121, 36, -106, -99, 100, 103, -24, -40, -38, 113, -8, 40, -57]
    hello world:[5, 88, -31, 115, 4, 48, -75, 44, 83, 21, 105, -67, 78, -53, -13, -28]
    

    I think we can all agree that those are two different byte arrays.

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

Sidebar

Related Questions

Does anyone know do there have any way that I can encrypt the array
Possible Duplicate: Java AES Encrypt Entire String I've run into a small problem. For
I have a DLL in C# that encrypts and decrypts string texts (something basic),
Is there any javascript function that can encrypt data: For example i want to
Where can I find a RSA encrypt example that does not use NoPadding? --update
I have to encrypt/decrypt some sensitive information in a Xml file? Yes I can
We know we can encrypt a file with openssl using this command: openssl aes-256-cbc
I've heard that one can encrypt data using drawings of the Mandlebrot set, and
I want to create an application that can encrypt and decrypt my passwords using
I'm aware that in Objective C I can encrypt using AES128 through their native

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.