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:
@Test
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 = EncryptUtils.de(processedStr.getBytes(), key, Const.ENCRYPT_TYPE);
System.out.println(stringDataDE);
}
the output :
orinalString length->14
orinalString_check->This is a test
encrypted String byte length-> 16
encrypted String byte length-> 26
null
------------------------------
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.