Sometimes when you send data, it’s required that you prove that you actually sent the data and not some malicious third party. That’s when you have to sign some data. The most known type of signatures around currently are RSA signatures, with PGP being one of the most popular applications widely used around the world.
The same thing can be said with encryption. When you want to send certain data to a certain endpoint and you want to make sure only that endpoint can decrypt the data (not even the one that encrypted the data in the first place), you’ll need assymetric encryption like RSA.
Using RSA isn’t really that hard in Java, if you know your way around keystores and how to manage them. But in the REST age, interchanging keystores can be a serious pain in the ass, because it’s not really REST friendly. This now changed with the advent of the JWK (JSON Web Key) standard. Most of us know what a JSON Web Token is, and if you’re securing REST services, chances are you’re already using them (if you’re still using basic or digest authentication, shoot yourself now). JWTs can be signed using a certain key and can even be encrypted using a certain key. The problem is that most people tend to go for the easy solution and choose a simple symmetric algorithm. The problem with this is that anyone can sign data if they can verify that data, because they need to know the shared key, which opens up the possibility for forgery.
RSA is assymmetrical. This means you have a public and a private key (that can also be used to derive the public key). For encryption you encrypt using the public key and only the person with the private key can decrypt. For signatures this is vice versa, you need the private key to sign and the public key to verify.
So how does a JWK actually look like. Well, a RSA512 key with a key size of 2048 bits can look like this:
This is a private key, because of the presence of the
d element (which is the private exponent of the RSA key). If you omit this element from the above JSON, you have the corresponding public key.
To use this in Java, you can use the Nimbus JWT library that is available. It supports all the current standards like JWT, JWE and off course JWK.
To read this key into Java you need to put all the keys in a JSON structure that has a collection of
keys, so something like this:
Once you have this, you just need a little bit of code to use this file:
Now to get an RSA key out of the keyset, you can just do this:
You can see I also make a
javax.security.KeyPair from this
RSAKey. This now allows me to use standard Java cryptographic APIs to sign and encrypt data.
For example, if I wanted to sign some data, I can do this:
This example will print out a SHA512 signature (Base64 encoded for readability) and also indicate that the signature is also valid according to the public key.
To encrypt some data, this is actually easy:
This will encrypt a String using the RSA algorithm using the keysize you chose when generating the JWK and print it out (again Base64 encoded) and will then decrypt it using the private key and show you the decrypted string (which should be
Bear in mind that RSA is not suitable for large data encryptions. A 2048 bit RSA key can at most encrypt 256 bytes of data. If you need to encrypt a lot of data, this is what you normally do:
- you encrypt the data using a symmetrical algorithm (like AES or Blowfish) with a shared key
- you encrypt the shared key using RSA and the public key of the recipient
- you send both the encrypted data and the encrypted key to the recipient
This way you can encrypt large amounts of data and be sure only the recipient will be able to decrypt the data.
As you can see, encrypting using assymmetrical algorithms is really not that hard anymore. If you for example want to allow external parties to send you encrypted data, you can easily now provide a REST endpoint to your public key. At the same time you can also ensure your external parties that they can verify any data you send them because they can check the signature using that public key (or another one you specifically use for signing).
Now you literally have no excuse anymore to store sensitive data unencrypted in your system or send sensitive unencrypted data over the wire. You only need to guard your RSA private key with your life just like you do with your private SSH key. Which is also an RSA key by the way.Tweet this article