Database Reference
In-Depth Information
Listing 10-31.
Test for Class Equality
Class
testClass = classObject.getClass()
;
if(
testClass.equals( providedClass )
) {
// further tests are unnecessary
} else return "Failed to setDecryptConns()";
}
Decrypt Connection Strings for Storage and Reuse
Once we have the identity questions settled (still in the
setDecryptConns()
method), whether we are
poised to insert or overwrite a registration entry, we are left to deal with the
connsHash
we received from
the client. Currently, the connection strings are encrypted with our shared session DES key—a key that
will disappear when the current session closes. That would be an unusable state to store them in. The
next user in our application would not be able to read them, nor would we in our own next session.
So, we will decrypt all the connection strings and store them unencrypted. When the next session
comes to get the connection strings for this application, we will encrypt them with that session's key
before delivery. Notice in Listing 10-32, when we set the
cryptConnsHash
member that we cast it as a
HashMap<String, RAW>
, no questions asked. This kind of blind trust, which is appropriate at this point, is
what the Java compiler warns about with the “unchecked” warning. Notice also that we will transition
from a
HashMap<String, RAW>
to a new
HashMap<String, String>
.
Listing 10-32.
Cast Encrypted List of Connections and Prepare to Decrypt
oins = new ObjectInputStream( new ByteArrayInputStream(
connections.getBytes() ) );
classObject = oins.readObject();
oins.close();
HashMap<String, RAW> cryptConnsHash =
(HashMap<String, RAW>)classObject
;
HashMap<String, String> clearConnsHash
=
new HashMap<String, String>();
oins.close();
We initialize our shared secret password cipher to do decryption (see Listing 10-33), then walk
through the
cryptConnsHash
HashMap
to decrypt each value. We store each decrypted value, using the
same key in our new
clearConnsHash
. With classes like the
HashMap
, from the
Collections
classes, we can
traverse their members using the for each syntax. You can read our
for
statement as “for each key in the
cryptConnsHash
set of keys.”
Listing 10-33.
Decrypt Each Connection String and Save to New List
cipherDES.init( Cipher.
DECRYPT_MODE
, sessionSecretDESKey, paramSpec );
for( String key : cryptConnsHash.keySet() )
{
// Decrypt each one
clearConnsHash.put
( key,
new String(