...
Code Block | ||
---|---|---|
| ||
BigInteger x = new BigInteger("530500452766"); String s = x.toString(); // valid character data byte[] byteArray = s.getBytes(); String ns = new String(byteArray); x = new BigInteger(ns); |
Compliant Solution (Java 8)
While Java does not provide a Charset
that guarantees lossless encoding of byte data, there are many other solutions for safely converting an arbitrary byte array into a string and back. Java 8 provides the java.util.Base64
class which provides encoders and decoders for the Base64 encoding scheme. This compliant solution uses Base64 to safely convert the number to a string and back without corruption of data.
Code Block | ||||
---|---|---|---|---|
| ||||
BigInteger x = new BigInteger("530500452766");
byte[] byteArray = x.toByteArray();
String s = Base64.getEncoder().encodeToString( byteArray);
byteArray = Base64.getDecoder().decode(s);
x = new BigInteger(byteArray);
|
Risk Assessment
Storing numeric data as a string is likely to result in a loss of data integrity.
...