This is a design question, I am going to try to narrow it down as much as possible to avoid flagging it as open-ended.
Problem
Need an effective way to design a class or two that hold the same data, but one has the data fields as encrypted strings, and the second have them as either decrypted strings or ints. I receive the data encrypted, I decrypt it once and allow my application to use the decrypted version.
What I have
I came up with different solutions, but not satifisfied with any of them. This is what I tried:
- One class with boolean flag `encrypted`, initially set to be `true` and then unset after the data is decrypted. The cons, the same fields are multi-used, sounds unsafe even though there is a private flag; another con, some fields are of different type once decrypted, say an ID, Ideally I want the decrypted version to be in the correct final type, so that wont work with adding new fields to the same class, which makes it messy and will complicate other tasks such as data binding, etc
- Thought about decorator pattern, but the way it works is to have the decorator and the subject class implement the same interface, but again return types will vary after decryption
- thought about having 2 classes, one for the decrypted version of the class and other for the encrypted. however, code reuses is abandoned in this approach, to fix that thought about having the decrypted version extend the encrypted one and reuse the getters of the encrypted fields that don’t change type after decryption, but fields that change type cannot be overriden
- thought about leaving data in String format even after decryption, and convert to integers outside of the class, but this is redundant and will clutter my code with silly code fragments whenever I need to use the integer fields of the class
Any ideas for how to go about solving this design problem?
The best way to encrypt fields is to simply serialize the object instance and encrypt the resulting stream. For getting the decrypted object instance, simply decrypt the stream and deserialize the object.