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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:11:48+00:00 2026-06-09T17:11:48+00:00

I have an AES encrypted byte array. When I decrypt this array in java

  • 0

I have an AES encrypted byte array. When I decrypt this array in java application, everything is fine. But in a servlet container, decrypted wrong.

Here ise my byte array

       String [] str = new String[] {"41", "23", "67" ,"-124", "-56" ,"-35" ,"89", "-54" ,"-17" ,"-49" ,"-53", "-21" ,"125" ,"4", "98", "-13", "60" ,"-72", "12", "75" ,"-105" ,"-104", "107", "34", "1", "-109", "-19", "-102", "-72", "9" ,"26" ,"-39", "-60", "-15", "0" ,"112", "-5", "-86", "-7", "5" ,"75", "100" ,"94", "-47", "6", "-81", "-22", "82", "97" ,"114", "3", "-24", "-80", "67", "106", "-100" ,"-35", "-83", "54", "-95", "124", "-22", "-100", "-47" };

        byte [] inv = new byte[str.length];

        int count = 0;
        for (String s : str) {
            inv[count++] = Byte.valueOf(s);
        }

The result of Java Application

Decrypted: 000000.016*kWh|000000.007*kWh|000000.015*kWh|000000.000*kWh

My application code

 byte[] keyBytes = "vikoAmrPass12345".getBytes(); 
 SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); 
 Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); 
 cipher.init(Cipher.DECRYPT_MODE, key); 
 byte [] out = cipher.doFinal(inv); 
 System.out.println("Decrypted: " +new String(out));

My web code which is works on servlet container is definitly same with above one.
But the container decypted result is

'?f ??0??????b]5hJ?F*`U????.8??p@?]?u?~Nb??z?????{3??;?

What is wrong here ?

In my application, I got encrypted byte array as

ServerSocket ss = new ServetSocket(port); 
Socket socket = ss.accept(); 
InputStream in = socket.getInputStream(); 
byte [] sizeBuffer = new byte[5]; 
in.read(sizeBuffer); 
byte [] dataBuffer = new byte[Integer.valueOf(""+new String(sizeBuffer)];     
in.read(dataBuffer);

Encrypted data exists on dataBuffer array. And encryption handled in an embedded device.

My configuration

java 1.6
tomcat 7.1

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-09T17:11:49+00:00Added an answer on June 9, 2026 at 5:11 pm

    From the code you’ve posted:

    byte[] keyBytes = "vikoAmrPass12345".getBytes();
    

    That could be the problem. You should never use the platform default encoding if you’re expecting to get the same result in multiple places. Always specify the encoding.

    If you’ve done this, it’s also possible that you’ve got problems with how you’re transporting the encrypted data. If you’re ever creating a string from it, you should almost certainly use Base64, e.g. with this library. If you’re using:

    // Bad! Do not use!
    String encryptedText = new String(encryptedData);
    

    Then that will basically be losing information, and could well be the cause of the problem.

    EDIT: Okay, so now we know your receiving code is:

    ServerSocket ss = new ServetSocket(port);
    Socket socket = ss.accept();
    InputStream in = socket.getInputStream();
    byte [] sizeBuffer = new byte[5];
    in.read(sizeBuffer);
    byte [] dataBuffer = new byte[Integer.valueOf(""+new String(sizeBuffer)]; 
    in.read(dataBuffer);
    

    Here you have at least three problems:

    • Your code cannot possibly handle more than 99999 bytes, and it’s not clear how you would expect it to handle less than 10000 bytes.
    • You’re assuming the default character encoding is consistent across platforms yet again
    • Your code assumes that each time it calls read, that exact amount of information will be read. There’s a good chance it won’t, particularly for network streams. You should be using the return value of read to see how much has actually been read. You either need to loop round until you’ve read all the data required (or run out of data in the stream), or use a higher level API to do the same thing.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file encrypted with java application using AES. I also have a
i have a java program . this is a AES encryption - decryption program.
I have translate a C# based decrypt function into Java. It works fine and
I have an aes encrypted mp3 file that I need to play in iOS;
This is regarding AES algorithm. Suppose i have implemented a AES algorithm and encrypt
I need to have a Win32 application load a hard coded AES-256 key, ideally
I need a CORE that will perform AES-128 Encryption/Decryption. I have searched online but
I need to create an application that decrypts data that is encrypted using AES
I have a two very simple methods to encrypt/decrypt a byte[] using the AesCryptoServiceProvider.
I am trying to decrypt in JAVA a string that was encrypted in C#.

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.