I am implementing a Cipher Block Chaining for school work and the question asks for a method taking String and returning another String. At first, I thought it was odd and that byte[] variables would be much more adequate, but implemented a method still. Basically, here's the code :
static public String encode(String message) {
byte[] dataMessage = message.getBytes();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte last = (byte) (Math.random() * 256);
byte cur;
out.write(last);
for (byte b : data) {
cur = (byte) (b^last);
System.out.println("Encode '" + (char) b + "' = " + b + "^" + last + " > " + cur );
out.write( cur );
last = cur;
}
System.out.println("**ENCODED BYTES = " + Arrays.toString(out.toByteArray()));
System.out.println("**ENCODED STR = " + Arrays.toString(out.toString().getBytes()));
return out.toString();
}
The decode method works similarly. Some times, the method will spit results like
Encode 'H' = 72^109 > 37
Encode 'e' = 101^37 > 64
Encode 'l' = 108^64 > 44
Encode 'l' = 108^44 > 64
Encode 'o' = 111^64 > 47
**ENCODED BYTES = [109, 37, 64, 44, 64, 47]
**ENCODED STR = [109, 37, 64, 44, 64, 47]
But sometimes will also spit things like
Encode 'H' = 72^-63 > -119
Encode 'e' = 101^-119 > -20
Encode 'l' = 108^-20 > -128
Encode 'l' = 108^-128 > -20
Encode 'o' = 111^-20 > -125
**ENCODED BYTES = [-63, -119, -20, -128, -20, -125]
**ENCODED STR = [-17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67]
I presume that this has something to do with UTF-8 (the system's default encoding), but I'm not familiar enough to figure out exactly why such a string would be returned with the given bytes.