Create Triple DES SecretKey in Java

For a project at work, I needed to integrate our website with another web application. The web application in question also had their own built in user management system. For seamless integration we wanted our users to only have to log in once.

As the web application was quite a large application, they already had a REST api that allowed you to supply them with a encrypted token in the url that authenticated you on their systems. You simply had to encrypt the user id (on their system) and the current time stamp.

As I had never done anything to do with encryption before in Java. I had to learn from scratch.

The token had to be encrypted with Triple DES CBC scheme. They supply you with an access key. Here is where the problem lay. I could find quite a few examples that allowed you to use the inbuilt key generator in Java, but what about if you have your own key (our access key). This was a bit more difficult to find, so I thought I would blog a code snipit here. In the end it turned out to simple (it always is 🙂

The key needed to be Triple DES (DESede)

String keyString ="4d89g13j4j91j27c582ji69373y788r6"; // I made this key up by the way!


byte[] keyB = new byte[24]; // a Triple DES key is a byte[24] array


for (int i = 0; i < keyString.length() && i < keyB.length; i++) {
keyB[i] = (byte) keyString.charAt(i);
}

// Make the Key
SecretKey key = new SecretKeySpec(keyB, "DESede");

A good resource that I found was here :


Hamza

Advertisements

17 comments so far

  1. John Doe from Portugal on

    Two years have gone by and this is still very useful, thanks a lot!

  2. My name is... on

    Looks like Portugal is looking here! Another help to me! Tks!

  3. Dirtbox on

    Thanks for this snippet Hamza, exactly what I was looking for.

  4. Nilesh on

    Your made up key is 32 chars. Didn’t need to be, did it? you could have made it 24 chars and avoided the extra condition in your for loop.

  5. marko on

    Besides that you can shorten the string to 24 chars: There is a getBytes() method on string. So instead of the loop simply code

    byte[] keyB = keyString.getBytes();

  6. Nikunj Jhala on

    Hi,

    Thanks for posting this solution. I am struggling to implement this. I was thinking about serialization. write key object in File to store the state of the object. But simple solution that you have given is a straight forward way.

    Thanks ….
    Regards,
    Nikunj Jhala

  7. Wynand on

    Fantastic! Thanks for posting! Exactly what I was looking for.

  8. Shreeharsha on

    Hi Hamza,

    thanks a lot man…. this is great… my problem solved

  9. Nusry on

    Thanks lot your code it is very help

  10. Suresh on

    Hi,

    can you help out in understanding how Triple DES will work in encrypting a string.

    Providing an example for Triple DES will be more helpful for me

    Many Thanks
    Suresh

  11. Samuel on

    This post is very very well… I went search from a site web to anothers but not find nothing . Here yes, thank for all man!

  12. Madhu on

    Hi I am new to Cryptography, I have question in triple des(3des): encryption is done in Oracle function DBMS_OBFUSCATION_TOOLKIT.DES3Encrypt , is that possible I can decrypt same in using java API.
    I have only key and encrypted password to decrypt. I have tried couple of examples, when I decrypt in java , Ii am getting different value.
    Is that possible if something is encrypted in oracle and can be decrypt in java

  13. Bonnie on

    Hello there I am not sure if it is me or even your website but it is launching clearly sluggish to me, it took me
    sort of a minute or two to successfully load up
    however , google does work well to me. Anyways, Thank you for creating such type of fabulous blog post.
    In my opinion it really has already been extremely useful to user who seem to visit here.
    I am hoping I will be able to find even more remarkable content and I
    should really complement your site by saying you have carried out remarkable
    job. After looking at your content, I’ve book marked your webblog.

  14. refnearn.com on

    Hello there Google performs well to me however your website is
    starting slowly which actually went on just about a minute to successfully load, I
    am not sure whether or not it’s my problem or perhaps your web site problems.
    Anyway, Thank you for putting up an incredibly brilliant articles.
    In my opinion it really has been literally beneficial to individual who actually visit here.
    I hope I’ll be able to find further incredible stuff and I also should really compliment your site simply by telling you have done good writing.
    Right after taking a look at the blog post, I have book marked your web
    site.

  15. Domenic on

    Hi Google performs great for my situation yet your web site is starting steadily which took about a
    few minutes to actually load up, I don’t know whether it is my own problem or maybe your website problem.
    Anyways, Thank you for creating an incredibly amazing articles.

    In my opinion this has been honestly useful to user who click here.
    I hope I will be able to get further incredible content and I also really should flatter simply by stating you have carried out wonderful work.
    I ‘ve got you book-marked to check out blog you publish.

  16. www.free-fifa15coins.com on

    Hey! Your webpage is running slowly if you ask me, the
    site took like a minute or so to load, I have no idea whether it is just simply me or maybe websites then again google worked acceptable for me.
    Anyways, I would like to thank you very much for including excellent article.

    Perhaps it really has been helpful to many people who came at this
    point. This is definitely fantastic what you actually have concluded and would like to check out a
    lot more articles by you. I already have your site book-marked to
    see new stuff you post.

  17. Linnea on

    Hey Your main web site starts up seriously slow in my opinion, I don’t know who’s issue is that although flickr starts quite immediate.

    Well, I want to appreciate you for putting brilliant blog post.
    Most people who discovered this great site should have noticed this
    content absolutely handy. I hope I’ll be able to find even more awesome content and I really should flatter you simply
    by telling you’ve done incredible work. I ‘ve got your site bookmarked to see blog you post.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: