首页 > 解决方案 > 使用 prime256v1 私钥加密/解密的 openssl 命令?

问题描述

我想用 EC 私钥加密一条简单的消息,特别是 CMD 的prime256v1;并使用相应的 EC 公钥解密

我只在非对称加密情况下或使用 ec 公钥加密中找到了 RSA 的参考资料,但我需要使用 ECDSA 算法并使用私钥进行加密。

标签: opensslprime256v1

解决方案


生成一个自签名的 P-256 证书和相关的私钥,如下所示:

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

像这样加密文件:

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

像这样解密文件:

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

签署这样的文件:

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

验证这样的签名文件:

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

推荐阅读