Hi, Habr! I present to you the translation of articles 6, 7 and 8 by Jakob Jenkov from the series of articles for beginners who want to learn the basics of cryptography in Java.
The KeyPair class ( java.security.KeyPair ) is a pair of asymmetric keys (public key + private key). The KeyPair instance is typically used in asymmetric cryptography (encryption or data signature). Typically, an instance of KeyPair is obtained from the Java keystore or KeyPairGenerator, which will be discussed later in this article.
You can access the public key of a KeyPair instance by calling its getPublic () method. An example of obtaining a public key:
PublicKey publicKey = keyPair.getPublic();
You can also access the private key of the KeyPair instance by calling its getPrivate () method. Here is an example of obtaining 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.
Before you can use the KeyGenerator class, you must create an instance of KeyGenerator. An instance of KeyGenerator is created by calling the static method getInstance () , as a parameter that accepts 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, it must be initialized. An instance is initialized by calling the init () method. An example of initializing an instance of KeyGenerator:
SecureRandom secureRandom = new SecureRandom(); int keyBitSize = 256; keyGenerator.init(keyBitSize, secureRandom);
The init () method takes two parameters: the key length and SecureRandom , which is used during key generation.
After initializing the KeyGenerator instance, you can use it to generate keys. Key generation is performed by calling the generateKey () method. Here is an example of generating a symmetric key:
SecretKey secretKey = keyGenerator.generateKey();
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.
The most famous type of asymmetric key pair is the key pair type: 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 with a public key and decrypt it with a private key. The private key is usually kept secret, and the public key can be known to everyone. Thus, if Jack encrypts some data with his private key, anyone who owns the public key of Jack can decrypt it.
To use KeyPairGenerator, you must first create an instance of the KeyPairGenerator class. Creating an instance of KeyPairGenerator is done by calling the getInstance () method. Here is an example of creating an instance:
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
The getInstance () method takes 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 the KeyPairGenerator instance. The KeyPairGenerator is initialized by calling its initialize () method. An example of initializing an instance of KeyPairGenerator:
keyPairGenerator.initialize(2048);
In this example, KeyPairGenerator is initialized to generate keys of 2048 bits in size.
To generate a key pair using the KeyPairGenerator, the generateKeyPair () method is called. Here is an example of key pair generation:
KeyPair keyPair = keyPairGenerator.generateKeyPair();
Source: https://habr.com/ru/post/445560/
All Articles