Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 545 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 440 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
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:
@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.
Best 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 newString
is a function of the charset, and hence may not be equal to the length of the byte array.https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte%5B%5D)
Answers
-
Why should encrypted string length be 16 also? AES Encryption changes string and string length.
-
I think we were not in the same page yet.
Please let me describe more carefully,
Case1:
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.
Case2:
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.
-
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 newString
is a function of the charset, and hence may not be equal to the length of the byte array.https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte%5B%5D)
-
@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.