Search code examples
opensslprime256v1

openssl command for encryption/decryption with prime256v1 private key?


I want to encrypt a simply message with an EC private key, specifically, prime256v1 by CMD; and decrypt with the corresponding EC public key.

I have only found references for RSA in the asymmetric cryptographic case or encryption with an ec public key, but i need to use ECDSA algorithm and encrypt with a private key.


Solution

  • Generate a self-signed P-256 certificate and associated private key like this:

    openssl ecparam -name prime256v1 -out p256-params.pem
    
    openssl req -x509 -nodes -days 3650 -newkey  ec:p256-params.pem -keyout p256-key.pem -out p256-cert.pem
    

    Encrypt a file like this:

    openssl cms -encrypt -binary -aes-256-cbc -in plaintext.dat -out ciphertext.dat p256-cert.pem
    

    Decrypt a file like this:

    openssl cms -decrypt -in ciphertext.dat -out plaintext2.dat -inkey p256-key.pem
    

    Sign a file like this:

    openssl cms -sign -binary -in plaintext.dat -out signedtext.dat -inkey p256-key.pem -signer p256-cert.pem -nodetach
    

    Verify a signed file like this:

    openssl cms -verify -in signedtext.dat -out plaintext2.dat -CAfile p256-cert.pem