importjava.security.KeyPair;importjava.security.KeyPairGenerator;importjava.security.NoSuchAlgorithmException;publicclassGenerateKeyPair{publicstaticvoidmain(String[]args)throwsNoSuchAlgorithmException{ // Create a key pair generatorKeyPairGenerator keyPairGenerator =KeyPairGenerator.getInstance("RSA");keyPairGenerator.initialize(2048); // Generate the key pairKeyPair keyPair =keyPairGenerator.generateKeyPair();}}
2. Encrypting Data with a Public Key
importjava.security.Cipher;importjava.security.PublicKey;importjava.util.Base64;publicclassEncryptWithPublicKey{publicstaticvoidmain(String[]args)throwsException{ // Get the public keyPublicKey publicKey =... // Create a cipherCipher cipher =Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, publicKey); // Encrypt the dataString data ="Hello, world!";byte[] encryptedData =cipher.doFinal(data.getBytes()); // Encode the encrypted data in Base64String encodedData =Base64.getEncoder().encodeToString(encryptedData);}}
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);
}
}
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();
}
}
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);
}
}
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();
}
}
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);
}
}
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);
}
}
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);
}
}
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class VerifyPasswordHash {