Forum Stats

  • 3,826,778 Users
  • 2,260,707 Discussions


An String And StringCoding Decode Issue

Dear Oracle:

I found a strange issue when I used JDK17, it's simple to reproduce this issue.

Generally speaking:

step1: I create a String like "hello world", the I get the byte array of this string, then I encrypted(ASE encryption) this byte array.

step2: invoke String(byte[] data) method to re-create a new String with the encrypted byte array.

step3, get the byte array of the new string.

Then I found the the byte array had changed, it's not the encrypted byte array, so I couldn't decrypt the byte normally. and I didn't put the encrypted the dyte array to String, just decrypt the encrypted byte array, everything is fine.

let me show this issue with code:


public void testStringEn() throws UnsupportedEncodingException {

// the original/test String.

String orinalString = "This is a test";

// the byte array of string.

byte[] strData = orinalString.getBytes();

// show the byte array length, it's 16, correctly.

System.out.println("orinalString length->" + strData.length);

// re-create a string with the byte array.

String orinalString_check = new String(strData);

// the new string is "This is a test", correctly.

System.out.println("orinalString_check->" + orinalString_check);

// a security key for encryption.

String keyStr = "123";

// byte array of key.

char[] key = keyStr.toCharArray();

// the AES encryption.

byte[] dataEn = EncryptUtils.en(strData, key, Const.ENCRYPT_TYPE);

// show the byte array length after encryption. it's 16.

System.out.println("encrypted String byte length-> " + dataEn.length);

// re-create the a string with the encrypted byte array.

String processedStr = new String(dataEn);

// show the string byte length, I HOPE/THINK it should be 16, BUT it's 26!

System.out.println("encrypted String byte length-> " + processedStr.getBytes().length);

// so decryption fail, and stringDataDE is null.

byte[] stringDataDE =, key, Const.ENCRYPT_TYPE);



the output :

orinalString length->14

orinalString_check->This is a test

encrypted String byte length-> 16

encrypted String byte length-> 26



So would you help me to finger out what's wrong with my code, or how could I solve my issue.

Appreciate for your respone, have a nice day.

Best Answer


  • dvohra21
    dvohra21 Member Posts: 14,576 Gold Crown

    Why should encrypted string length be 16 also? AES Encryption changes string and string length.

  • User_WOE2P
    User_WOE2P Member Posts: 3 Green Ribbon


    I think we were not in the same page yet.

    Please let me describe more carefully,


    step1: I created a string: String a = "abc", then I got the byte arrary length, it's 3;

    step2: then I encrypted  the string "abc", now it's length of array is 16.

    step3: then I decrypted the "carray" to a new array, we named it as "backArray",

    step4: then I create a new string with the "backArray", like : String b = new String(backArray);

    then I got String "b" is "abc",

    it's ok, everything is fine.


    step1: likes case1.step1;

    step2: likes case1.step2;

    step3: I create new String with the "encrypted  array", like: String d = new String(encrypted_array); I think this constructor shouldn't change the content of the "encrypted array", BUT it did changed the content!

    step4: I got the array from string "d";

    step5: likes case1. step3;

    step6: likes case1.step4

    BUT something wrong, the String "b" is not "abc", it's null.

    So the key point is step3:" String d = new String(encrypted_array)", it changed the encrypted  content, I've no idea why it would happen. As I think the "new String(byte[] buffer)" likes a contenter, I hope I got what I put, but it isn't now.

  • dvohra21
    dvohra21 Member Posts: 14,576 Gold Crown
    Answer ✓

    Why the length of encrypted String changes is explained:

    public String(byte[] bytes)

    Constructs a new String by decoding the specified array of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

  • User_WOE2P
    User_WOE2P Member Posts: 3 Green Ribbon

    @dvohra21 thanks for your explain, I got it now, I should read the doc more carefully(new String(byte[]) is not a container of byte array).

    Have a nice day.