Asymmetric Encryption#
What Is The Asymmetric Encryption?#
-
Asymmetric encryption, also known as Public-Private-Key Cryptography, encrypts and decrypts the data using two separate cryptographic asymmetric keys. These two keys are known as a public key and a private key. As their names suggest. the private key must be kept secret, whereas the public can be know to everyone. When applying encryption, the public key is used, whereas descrypting requires the private key. Anyone should be able to send us encrypted data, but only we should be able to descrypt and read it! We have some common asymmetric encryption methods as RSA (named after computer scientists Ron Rivest, Adi Shamir, and Leonard Adleman), PKI (Public key infrastructure). More information
Generate Asymmetric Encryption Key Pair In Java 17#
- In Java 17, we don't need to add any more dependency to generate the asymmetric encryption key pair.
Model#
- Let's create a model for the response body which will contain the key pair as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Configuration#
- Then let's create a configuration property file for loading the length of the
RSA
key pair because the key pair can have value as1024
or2048
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
- Then we configure the
application.yaml
as below.
1 2 3 |
|
Service#
- So to generate the asymmetric key pair let's create a service as below.
KeyPairGeneratorService.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
- We will use the
RSA
algorithm for generating the key pair. Then to generate the key pair we also need to provide the key length. RSA keys tend to be 1024 or 2048 bits in length, making them extremely difficult to factorize, though 1024 bit keys are believed to breakable soon.
Controller#
- Now, we just simply create a controller to below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Testing#
- Now, let's open
postman
and call the api then we will see theRSA
key pair as below.
Using Asymmetric Encryption Key Pair In Java 17#
- In Java 17, we don't need to add any more dependency to generate the asymmetric encryption key pair.
Model#
- Let's create a model for the encryption and decryption request body which will contain the key pair as below.
EncryptionDataRequest.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
DecryptionDataRequest.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Configuration#
- Then let's create a configuration property file for loading the private key and public key of the
RSA
key pair.
ApplicationProperty.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
- Then we configure the
application.yaml
as below.
application.yaml | |
---|---|
1 2 3 4 5 |
|
Service#
- So to use the asymmetric key pair let's create a service as below.
AsymmetricEncryptionService.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
-
As we can see that we will create 2 methods, one is used for encrypt the data by using the public key and the other one is used for decrypt data using the private key. Also, we can use private key for encrypt data and the public key for decrypt data but this way is not common and recommended.
-
So for the method
encryptDataWithPublicKey
method, we will do some steps as below.- Firstly, we will create a
Cipher
instance for the RSA algorithm, which will be used to perform the encryption. - Then the public key, stored as a base64 encoded string, is decoded into its raw byte array representation. This assumes that the public key is stored in X.509 format.
- Then an
X509EncodedKeySpec
is created from the decoded public key bytes. This specification is used to reconstruct the public key. - Then a KeyFactory for the RSA algorithm is used to generate a PublicKey object from the key specification.
- Then the cipher is initialized in encryption mode with the public key.
- Then the input data is converted to a byte array and encrypted using the cipher. The result is a byte array of encrypted data.
- Then the encrypted byte array is encoded to a base64 string, which is returned as the result.
- Firstly, we will create a
-
For the method
decryptDataWithPrivateKey
method, we will have some steps.- Firstly, we will create a
Cipher
instance for the RSA algorithm, which will be used to perform the encryption. - Then we will decode the Base64 encoded private key to a byte array.
- Then the
PKCS8EncodedKeySpec
is used to specify the key material in the PKCS8 format. KeyFactory.getInstance(RSA_ALGORITHM)
gets aKeyFactory
object for the RSA algorithm.keyFactory.generatePrivate(keySpec)
generates aPrivateKey
object from the key specification.cipher.init(Cipher.DECRYPT_MODE, privateKey)
initializes theCipher
object in decryption mode with the private key.Base64.getDecoder().decode(encryptedData)
decodes the Base64 encoded encrypted data to a byte array.cipher.doFinal(encryptedBytes)
decrypts the byte array and returns the decrypted byte array.new String(decryptedBytes)
converts the decrypted byte array to a string and returns it.
- Firstly, we will create a
Controller#
- Now, we just simply create a controller to below.
AsymmetricEncryptionController.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
Testing#
-
Now, let's open
postman
and call the api for encrypting the data with public key as below. -
Then now, let use the response encrypted data and call the api for decrypting the data by private key as below.
- So the message has been encrypted and decrypted successfully.