![]() |
VOOZH | about |
Cryptography is the study of different techniques to secure data from an unauthorized entity. In computer science, we try to develop strategies and practices for protecting sensitive data. Most of the cryptography involves very advanced Mathematical functions used for securing data. The sole purpose of the algorithms developed for cryptography is to hide data from the attacker or middleman.
In the previous article, we have studied the different methods, classes and approach to perform the symmetric encryption and decryption. In this article, we will understand asymmetric encryption.
Asymmetric Encryption also called as private/public key Encryption is a mathematical relation between two keys, one for encryption and the other for decryption. For example, if there are two keys "K1" and "K2", then if key "K1" is used for encryption and "K2" is used for decryption. If "K1" is used for decryption, then "K2" is used for encryption.
Before implementing the asymmetric encryption using the RSA algorithm, we will first see how to generate a keypair(public, private). The following steps can be followed in order to generate asymmetric key:
SecureRandom random = new SecureRandom();
KeyPairGenerator KPGenerator = KeyPairGenerator.getInstance(Key_Generation_Algorithm_string_variable);
keyPairGenerator.initialize(2048, secureRandom);
Below is the implementation of the above approach:
Output:
Encryption and Decryption using the asymmetric key: In the above steps, we have created the public & private keys for Encryption and Decryption. Now, let us implement Asymmetric Encryption using the RSA algorithm. The following steps can be followed in order to implement the encryption and decryption.
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
Below is the implementation of the above approach: