Cryptography in Java. KeyPair Class
Hello, Habr! I present to you the translation of 6, 7 and 8 articles by Jakob Jenkov from a series of articles for beginners who want to learn the basics of cryptography in Java.
Table of contents:
- Java cryptography
- Java cipher
The KeyPair class ( java.security.KeyPair ) is a pair of asymmetric keys (public key + private key). A KeyPair instance is commonly used in asymmetric cryptography (data encryption or signature). Typically, a KeyPair instance is obtained from the Java key store or KeyPairGenerator, which will be discussed later in this article.
Public key access
You can access the public key of an instance of KeyPair by calling its getPublic () method . An example of obtaining a public key:
PublicKey publicKey = keyPair.getPublic();
Private Key Access
You can also access the private key of the KeyPair instance by calling its getPrivate () method . Here is an example of getting a private key:
PrivateKey privateKey = keyPair.getPrivate();
The KeyGenerator class ( javax.crypto.KeyGenerator ) is used to generate symmetric encryption keys. A symmetric encryption key is a key that is used to encrypt and decrypt data using a symmetric encryption algorithm.
Creating an instance of KeyGenerator
Before you can use the KeyGenerator class, you must create an instance of KeyGenerator. A KeyGenerator instance is created by calling the static method getInstance () , as a parameter that takes the name of the encryption algorithm for which the key is being created. Here is an example of creating an instance of KeyGenerator:
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
This example creates an instance of KeyGenerator that can generate keys for the AES encryption algorithm.
After creating an instance of KeyGenerator, you must initialize it. Initialization of an instance is accomplished by calling the init () method . KeyGenerator instance initialization example:
SecureRandom secureRandom = new SecureRandom(); int keyBitSize = 256; keyGenerator.init(keyBitSize, secureRandom);
The init () method accepts two parameters: the key length and SecureRandom , which is used during key generation.
After initializing the KeyGenerator instance, you can use it to generate the keys. Key generation is performed by calling the generateKey () method . Here is an example of generating a symmetric key:
SecretKey secretKey = keyGenerator.generateKey();
Key pair generator
The KeyPairGenerator class ( java.security.KeyPairGenerator ) is used to generate asymmetric key pairs. A pair of asymmetric keys consists of two keys: the first key is usually used to encrypt data, and the second key is used to decrypt data encrypted with the first key.
Public and private keys
The most famous type of asymmetric key pair is the type of key pair of the form: public key + private key. The private key is used to encrypt data, and the public key is used to decrypt data. In fact, you can also encrypt data using the public key and decrypt it using the private key. The private key is usually kept secret, and the public key may be known to everyone. Thus, if Jack encrypts some data with his private key, everyone who owns Jack's public key can decrypt it.
Creating an instance of KeyPairGenerator
To use KeyPairGenerator, you must first create an instance of the KeyPairGenerator class. An instance of KeyPairGenerator is created by calling the getInstance () method . Here is an example of creating an instance:
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
The getInstance () method accepts the name of the encryption algorithm to be used. In this example, we use the RSA algorithm.
Depending on the algorithm, you may need to initialize an instance of KeyPairGenerator. KeyPairGenerator is initialized by calling its initialize () method . KeyPairGenerator instance initialization example:
In this example, a KeyPairGenerator is initialized to generate 2048-bit keys.
Key pair generation
To generate a key pair using KeyPairGenerator, the generateKeyPair () method is called . Here is an example of key pair generation:
KeyPair keyPair = keyPairGenerator.generateKeyPair();