Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.4 kB
5
Indexable
Never
import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.*;

public class RSA_KEM {


    public static void main(String[] args) {
        try {
            // Step 1: Generate RSA key pair
            KeyPair keyPair = generateRSAKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();

            // Step 2: Key Encapsulation Mechanism (KEM) - encapsulate key
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(256); // AES-256
            SecretKey secretKey = keyGenerator.generateKey();

            // Use the encapsulated key for encryption
            byte[] ciphertext = encryptWithRSA(publicKey, secretKey.getEncoded());
            System.out.println("Encrypted Key: " + bytesToHex(ciphertext));

            // Step 3: Key Decapsulation - decapsulate key
            byte[] decryptedKey = decryptWithRSA(privateKey, ciphertext);

            // Use the decapsulated key for decryption
            String originalMessage = "Hello, RSA with KEM!";
            byte[] encryptedMessage = encryptWithAES(decryptedKey, originalMessage.getBytes());
            System.out.println("Encrypted Message: " + bytesToHex(encryptedMessage));

            byte[] decryptedMessage = decryptWithAES(decryptedKey, encryptedMessage);
            System.out.println("Decrypted Message: " + new String(decryptedMessage));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static KeyPair generateRSAKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048); // Adjust key size as needed
        return keyPairGenerator.generateKeyPair();
    }

    private static byte[] encryptWithRSA(PublicKey publicKey, byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    }

    private static byte[] decryptWithRSA(PrivateKey privateKey, byte[] ciphertext) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(ciphertext);
    }

    private static byte[] encryptWithAES(byte[] key, byte[] data) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }

    private static byte[] decryptWithAES(byte[] key, byte[] ciphertext) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return cipher.doFinal(ciphertext);
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte b : bytes) {
            result.append(String.format("%02X", b));
        }
        return result.toString();
    }
}
Leave a Comment