I'm trying to create a custom inputstream. My problem is, the read() method returns an integer from 0-255, but I need to convert it to a byte, decrypt it, and convert it back to an integer. How?
I need something like:
InputStream in = ...;
OutputStream out = ...;
int unsigned = in.read();
byte signed = unsignedIntToSignedByte(unsigned); // from -128 to 127
... // Editing it here
outputstream.write(signedByteToUnsignedInt(signed)); // from 0 - 255
Noting that creating your own encryption is unsafe, and assuming you're doing it "just for fun" and don't think in any way that what you're doing is secure, you really don't need anything special...
int i = in.read();
byte b = (byte) i;
byte e = encrypt(b);
out.write(e);
would be the basic approach, assuming byte encrypt(byte b)
method which does the "encryption". Checking for end-of-stream, exception handling, performance considerations (you don't want to perform things 1 byte at a time) etc. have been left out from this example.