Database Reference
In-Depth Information
return rtrnRaw;
}
During development, I had occasions to catch exceptions in getRSACryptData() and similar
methods, but in the debugged code, I'm depending on another mechanism to report errors. You can see
that the catch block has a close brace } immediately after the open brace { . That is a way of ignoring the
exception. This method will successfully return a valid RAW value whether an exception is thrown or not.
If there is a problem completing the encryption (the last line within the try block), then our original
“error” message is returned in the RAW . That original message informs the client that the method failed.
We translate the message into a RAW by getting the bytes of the String from getBytes() and sending them
to the RAW constructor. If there is no problem when executing the code, then our member rtrnRaw is set
to the encrypted value, and that is returned; else the error message is returned in RAW form.
The first test we do in getRSACryptData() is to see whether we have already built a copy of the RSA
public key. We pass this method the public artifacts each time we call it, but only want to go through
building the key one time. So, we test if extRSAPubKey is null . We also test to see if the value of the
modulus artifact has changed (that should never be the case). If either of these is true, we build our copy
of the RSA public key by calling makeExtRSAPubKey() .
We initialize the cipherRSA to do encryption, Cipher.ENCRYPT_MODE . Then we encrypt our clear text
by calling the doFinal() method.
After this chapter, we will not be calling getRSACryptData() directly from our client code, so we will
be changing this from a public to a private method. Eventually, the only thing we will be encrypting
with our RSA public key is a secret passphrase. We will be exploring that in the next chapter.
Use Stacked Calls
Let me point out the syntax in one line of code from Listing 5-13:
rtrnRaw = new RAW( cipherRSA.doFinal( clearText.getBytes() ) );
We call the getBytes() method of the clearText String , then instead of pointing a byte array
member at it, we pass the bytes directly to the cipherRSA.doFinal() method to encrypt (it takes a byte
array and returns a byte array). Then instead of pointing a local byte array member at the encrypted
bytes, we pass the array to a new RAW instance constructor.
I call this syntax stacked calls, and I like it. I prefer densely packed code over spacious code. It's just
my opinion that it reads and prints easier. The key to reading stacked calls is to look at the innermost
pair of parentheses first, and read from the inside out. This is more difficult to read from left to right than
from right to left.
Decrypting Data with Private Key
The next method that we need for the demonstrations in this chapter is a method to decrypt the data on
the client with our RSA private key. Listing 5-14 defines that method.
Listing 5-14. Decrypingt Data with Private Key, getRSADecryptData()
public static final String getRSADecryptData( RAW cryptData ) {
String rtrnString = "getRSADecryptData failed";
try {
cipherRSA.init( Cipher.DECRYPT_MODE, locRSAPrivKey );
rtrnString = new String( cipherRSA.doFinal( cryptData.getBytes() ) );
} catch ( Exception x ) {
 
Search WWH ::




Custom Search