# java.security

***

**1. Generating a Key Pair for Encryption**

```java
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

public class GenerateKeyPair {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        // Create a key pair generator
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);

        // Generate the key pair
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
    }
}
```

**2. Encrypting Data with a Public Key**

```java
import java.security.Cipher;
import java.security.PublicKey;
import java.util.Base64;

public class EncryptWithPublicKey {

    public static void main(String[] args) throws Exception {
        // Get the public key
        PublicKey publicKey = ...

        // Create a cipher
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        // Encrypt the data
        String data = "Hello, world!";
        byte[] encryptedData = cipher.doFinal(data.getBytes());

        // Encode the encrypted data in Base64
        String encodedData = Base64.getEncoder().encodeToString(encryptedData);
    }
}
```

**3. Decrypting Data with a Private Key**

```java
import java.security.Cipher;
import java.security.PrivateKey;
import java.util.Base64;

public class DecryptWithPrivateKey {

    public static void main(String[] args) throws Exception {
        // Get the private key
        PrivateKey privateKey = ...

        // Create a cipher
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        // Decode the encrypted data from Base64
        byte[] encryptedData = Base64.getDecoder().decode(...);

        // Decrypt the data
        byte[] decryptedData = cipher.doFinal(encryptedData);

        // Convert the decrypted data to a string
        String data = new String(decryptedData);
    }
}
```

**4. Generating a Message Digest**

```java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class GenerateMessageDigest {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        // Get the message digest algorithm
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        // Update the message digest with the data
        byte[] data = "Hello, world!".getBytes();
        md.update(data);

        // Compute the message digest
        byte[] digest = md.digest();
    }
}
```

**5. Verifying a Message Digest**

```java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class VerifyMessageDigest {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        // Get the message digest algorithm
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        // Update the message digest with the data
        byte[] data = "Hello, world!".getBytes();
        md.update(data);

        // Compute the message digest
        byte[] digest1 = md.digest();

        // Update the message digest again
        data = "Goodbye, world!".getBytes();
        md.update(data);

        // Compute the message digest again
        byte[] digest2 = md.digest();

        // Verify the message digests
        boolean verified = Arrays.equals(digest1, digest2);
    }
}
```

**6. Signing Data with a Private Key**

```java
import java.security.PrivateKey;
import java.security.Signature;

public class SignDataWithPrivateKey {

    public static void main(String[] args) throws Exception {
        // Get the private key
        PrivateKey privateKey = ...

        // Create a signature
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initSign(privateKey);

        // Update the signature with the data
        byte[] data = "Hello, world!".getBytes();
        signature.update(data);

        // Sign the data
        byte[] signatureBytes = signature.sign();
    }
}
```

**7. Verifying a Signature with a Public Key**

```java
import java.security.PublicKey;
import java.security.Signature;

public class VerifySignatureWithPublicKey {

    public static void main(String[] args) throws Exception {
        // Get the public key
        PublicKey publicKey = ...

        // Create a signature
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initVerify(publicKey);

        // Update the signature with the data
        byte[] data = "Hello, world!".getBytes();
        signature.update(data);

        // Verify the signature
        byte[] signatureBytes = ...
        boolean verified = signature.verify(signatureBytes);
    }
}
```

**8. Generating a Secure Random Number**

```java
import java.security.SecureRandom;

public class GenerateSecureRandomNumber {

    public static void main(String[] args) {
        // Create a secure random number generator
        SecureRandom random = new SecureRandom();

        // Generate a secure random number
        byte[] randomNumber = new byte[32];
        random.nextBytes(randomNumber);
    }
}
```

**9. Generating a Password Hash**

```java
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class GeneratePasswordHash {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        // Get the password
        byte[] password = "password".getBytes(StandardCharsets.UTF_8);

        // Create a message digest
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        // Hash the password
        byte[] hash = md.digest(password);
    }
}
```

**10. Verifying a Password Hash**

```java
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class VerifyPasswordHash {

```
