aspose file tools*
The moose likes Java in General and the fly likes Encrypting data using my own key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Encrypting data using my own key" Watch "Encrypting data using my own key" New topic
Author

Encrypting data using my own key

Balasubramaniam Muthusamy
Ranch Hand

Joined: Nov 30, 2010
Posts: 51
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
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
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

Joined: Nov 30, 2010
Posts: 51
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
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
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

Joined: Nov 30, 2010
Posts: 51
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
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
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

Joined: Nov 30, 2010
Posts: 51
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

Joined: Aug 16, 2005
Posts: 14420
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Balasubramaniam Muthusamy
Ranch Hand

Joined: Nov 30, 2010
Posts: 51
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

Joined: Feb 04, 2007
Posts: 1
Bala,
Were you able to do this? It can be done by using a Base32 encoder as well? What did you use?
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encrypting data using my own key