Using a Custom Attribute to Handle an Encrypted Field


For certain types of sensitive information, such as credit card numbers for example, you may need to store the information as encrypted data in the database. At the same time you want to work with the clear raw decrypted data in your code. Creating a custom class that can act as a custom EO attribute type can simplify this feature of your application by providing "on the fly" encryption and decryption as the attribute gets saved and fetched in and out of the database.....

There are a few elements of your app that need to be setup to have this feature. In my case, I have used a couple of utility methods from Project Wonder's ERXCrypto class and Apache Jakarta Commons Lang StringUtils class (Why reinvent the wheel?!). If you don't want to install those open source projects, then you can always download the source and integrate the functionality directly into the code I provided.

Summary
I created a custom class named EncodedString . I use it as a custom attribute in my entities where I require encryption. In code, usage is something like this:

Assuming you have a clear string like this: 12345678901234567

Then to create the attribute 'numberEncrypted' we would use:
myEntityInstance.takeValueForKey(new EncodedString( "12345678901234567" ),"numberEncrypted");

To display a 'masked' version of the clear string, we would use:
String displayString = myEntityInstance.valueForKey("numberEncrypted").toString();
.... this conveniently returns something like:
1****************567

To retrieve the clear value, we would use:
EncodedString mySensitiveAttribute = (EncodedString)myEntityInstance.valueForKey("numberEncrypted");
String clearString = mySensitiveAttribute.value();

Implementation
To support the custom EncodedString class shown below, add the ERExtensions framework (and dependencies such as ERJars) to your project. Also download and install Jakarta Commons Lang jar file and drop it in /Library/Java/Extensions. If you don't want to do that, then you will have to examine the source code for those opensource projects and integrate the necessary methods called in my class into your project.


The EncodedString class
Download or view from this URL . This provides the foundation for the implementation.


EOModel Implementation
In EOModeler, an attribute will be setup like this in the attribute inspector:

External Type: VARCHAR (or whatever your SQL database supports for character strings)
Internal Data Type: Custom
External Width: This needs to at least double the size of the clear strings. Make it 3 times for safety.
Class: EncodedString
Factory Method: createInstanceFromEncodedString:
Conversion Method: toEncodedString
Init Argument: NSString

Blowfish Support Implementation:
In your project properties file, implement a 16 character cipher key for ERXCrypto class as a property like this:
# The string must represent a key between 32 and 448 bits.
# If too big or small Cipher creation will fail.
# If using standard ASCII letters and upper/lower case letters,
# it seems 16 is the maximum string length that will work.
er.extensions.ERXBlowfishCipherKey=Y2eC6lAfiE3hleyO

You can generate random keys at one of many internets sites. Don't ever lose this key or you _will_ never be able to access your encrypted data in the database!

I recommend Translucent Databases by Peter Wayner, the first book shown below, which specifically deals with strategies for protecting sensitive information in SQL databases using the Java API.


Translucent Databases

Posted: Tue - July 5, 2005 at 10:16 AM        


Published by