I'm not a cryptography expert, and while I have experience applying encryption with a standard RSA public key, I've never tried encrypting with an elliptic curve public key. If there's a developer familiar with applying elliptic curve public keys, I'd like to ask for answers to the following questions: The encryption and decryption with the code provided work well, but I want to confirm if the method applied in the question is correct.
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.KeyPairGenerator;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;
import javax.crypto.Cipher;
import java.util.Base64;
fun main() {
Security.addProvider(new BouncyCastleProvider());
// 1. Key pair generation
val spec = new ECGenParameterSpec("secp256r1");
val keyPairGen = KeyPairGenerator.getInstance("EC");
keyPairGen.initialize(spec);
val keyPair = keyPairGen.generateKeyPair();
val publicKey = keyPair.getPublic();
val privateKey = keyPair.getPrivate();
// 2. Generation of crand (32 bytes)
val crand = "12345678901234567890123456789012";
val crandBytes = crand.getBytes();
val cipher = Cipher.getInstance("ECIES");
/*
3. Encryption
Question: Is setting ENCRYPT_MODE and applying the publicKey the same expression as applying Encrypt(crandBytes, publicKey) in RSA?
Question:To apply encryption with an elliptic curve key pair, is it correct to use ECIES like below?
*/
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
val cryptoWcranddData = cipher.doFinal(crandBytes);
val encryptedDataStr = Base64.getEncoder().encodeToString(cryptoWcranddData);
System.out.println("Encrypted Data (Base64 Encoded): " + encryptedDataStr);
// Decryption
cipher.init(Cipher.DECRYPT_MODE, privateKey);
val decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedDataStr));
val decryptedDataStr = new String(decryptedData);
System.out.println
"Decrypted Data: " + decryptedDataStr);
}