Database Reference
In-Depth Information
In my member naming scheme, I could have chosen about any names with the same effect, but I
chose to name the keys and artifacts that I generate locally with a loc prefix, like locRSAPrivkey . Artifacts
passed to me and keys generated from those artifacts I prefix with an ext prefix (for “external”), like
extRSAPubKey . This will help us keep things straight, especially as we execute both the client and the
server side code on our workstation.
Testing on the Client
Our code in OracleJavaSecure is intended to be run as a client-server application to encrypt data on one
end (e.g., server) and decrypt it at the other (e.g., client). However, it is nice to be able to test our code
without introducing some of the complexities of client-server communication. So we will test all of our
code so far on the client only. On the client we will generate the keys, encrypt data with the private key,
build an equivalent public key from the public key artifacts, and decrypt data with the equivalent public
key.
Writing the main() Method
In my experience, the best place to put test code is right in the class whose functions you are testing.
Obviously you have to run your code in order to test it, and if you can run it from the command prompt,
then you needn't build or depend on another class to do your testing. Whenever we are talking about
executing from the command prompt, we are talking about using the main() method. The client-only
testing portion of the OracleJavaSecure.main() method is shown in Listing 5-10.
Listing 5-10. Method for Testing on the Client Only, OracleJavaSecure.main() , Part A
public static void main( String[] args ) {
try {
// As a client application
String clientPubModulus = getLocRSAPubMod();
String clientPubExponent = getLocRSAPubExp();
// Send modulus and exponent to Oracle Database, then
// As if I were the Oracle Database
makeExtRSAPubKey( clientPubModulus, clientPubExponent );
cipherRSA.init( Cipher.ENCRYPT_MODE, extRSAPubKey, random );
Date clientDate = new Date();
String sampleData = clientDate.toString();
byte[] clearBytes = sampleData.getBytes();
byte[] cryptBytes = cipherRSA.doFinal( clearBytes );
// Send the cryptBytes back to the client application, then
// As a client application
cipherRSA.init( Cipher.DECRYPT_MODE, locRSAPrivKey );
byte[] newClearBytes = cipherRSA.doFinal( cryptBytes );
String newSampleData = new String( newClearBytes );
System.out.println( "Client date: " + newSampleData );
} catch ( Exception x ) {
x.printStackTrace();
}
System.exit( 0 );
}
 
Search WWH ::




Custom Search