Java Reference
In-Depth Information
To convert to base-64, this program makes use of an array that holds the entire base-64
alphabet. For the normal number system, base 10, this alphabet would be “0”, “1”, “2”, “3”,
“4”, “5”, “6”, “7”, “8”, and “9”. For hexadecimal (base 16) the alphabet would be “0”, “1”, “2”,
“3”, “4”, “5”, “6”, “7”, “8”, “9”, “A”, “B”, “C”, “D”, “E” and “F”.
Base-64's alphabet has so many elements that both upper and lower case letters are
needed to represent different numerical digits. The alphabet representation used by base-64
shown in Listing 5.3 above is stored in the toBase64 variable.
The majority of the encryption work is done by the write method. Base-64 conversion
requires the input data to be broken up into 3-byte chunks.
buffer[index] = c;
index++;
if (index == 3)
{
Once we have three bytes, then we write the base-64 version of the number. This is
accomplished by taking the 24 bits gathered, 6 bits at a time. This results in four digits, as
shown here:
super.write(toBase64[(buffer[0] & 0xfc) >> 2]);
super.write(toBase64[((buffer[0] & 0x03) << 4)
| ((buffer[1] & 0xf0) >> 4)]);
super.write(toBase64[((buffer[1] & 0x0f) << 2)
| ((buffer[2] & 0xc0) >> 6)]);
super.write(toBase64[buffer[2] & 0x3f]);
index = 0;
}
Each write statement segments one of the groups of 6-bits, and writes that digit.
The flush method is needed because the data to be converted may not end exactly
on a 3-byte boundary. The flush method works like the write method, in that it fills the
remaining buffer with zeroes and writes the final digits of the conversion.
This is a very useful recipe that can be applied anytime you must access an HTTP authen-
ticated web page. The addAuthHeader method can be used to add HTTP authentication
support to programs of your own.
Summary
This chapter showed you how to make use of HTTP security mechanisms. HTTP has two
built-in security mechanisms. First, HTTP supports encryption through HTTPS. Secondly,
HTTP provides authentication, which requires users to identify themselves.
Search WWH ::




Custom Search