Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encrypting data using my own key

 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,
I have a database with the following fields

Name, id, and salary.

And let say there are 100 people can extract the above data from my database. Here I would like to encrypt the data using my own key and write the unreadable format data to output file.

Also if somepoint later, user wants to decrypt to data to view, I should be able to decrypt the data.

Could you please someone share me if you have any sample code?

Thanks
Bala
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start reading up on the JCE API: http://www.coderanch.com/how-to/java/SecurityFaq#encryption. A cipher like AES-128 would be suitable.
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your reply. Just I happened to see your reply today. I gone thorugh the code and I have created the sample program as well. is the any way we can avoid special characters in the encoded output? is there any way the output can only be restricted to data type specific?

Your help would be really appreciated and it will help me a lot.

Thanks
Bala
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be precise: the data is *encrypted*, not *encoded*. As such it is binary, not text - so it doesn't have any "characters" in it, and can't be treated as text (or stored as a String). If you need to treat it as text, then you need to encode the ciphertext using something like base-64 encoding.
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the correction. Could you please show me any sample program? I am getting the text only not binary format

Thanks
Bala
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the text only not binary format

What do you mean by that? Encryption normally hands you a byte[] - that is the binary data. base-64 encoding can be done with a library such as Apache Commons Codec.
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my sample java code and output as below. In the encrypted output I should not have any special character. It should have only alphanumeric. And another thing is possible to get output length as specified by us? Thank you for your help


Before Encryption : Testing encryption
After Encryption : 5Mpp1Vwej3jfnyck99F0u0UaqxUkNH4ky3Z9BbxlvUA=
After Decryption : Testing encryption



 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balasubramaniam Muthusamy wrote:Here is my sample java code and output as below. In the encrypted output I should not have any special character. It should have only alphanumeric.

When you use Base64 encoding, as you are doing, you will not get only alphanumeric characters (i.e. characters in the ranges A-Z, a-z and 0-9); you might also get characters like '='. If you don't want that, then use some other encoding algorithm instead of Base64, that only produces alphanumeric characters.

Note also that catching exceptions and doing nothing with them, as you do in lines 59 - 60, is a bad idea. If an exception happens, you'll never know.

Also, it's not a good idea to use internal Sun classes, such as sun.misc.BASE64Encoder and sun.misc.BASE64Decoder. These classes are not part of Java's public API and they might not exist on all JVM implementations, or they might not even exist in a future version of Oracle's JVM implementation. If you do need to use Base64 encoding, then use a third-party library with a supported API instead, for example Apache Commons Codec.
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you much for your good guidelines. I have not captured the exception as it is for simply testing purpose. Going forward i will avoid in testing classes as well.

As far base64 concern i will use default API as you said and will try to do some workaround. I will let you know once i find something.

Thanks for your invaluable time.
Bala
 
TIMUS ANARUHK
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bala,
Were you able to do this? It can be done by using a Base32 encoder as well? What did you use?
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic