avatar
童琦杰
Feb 28, 2023Technology

ES256(ECDSA with SHA-256)私钥和公钥生成方法

标准格式ES256私钥生成

bash
openssl ecparam -genkey -name prime256v1 -noout -out es256_private_key.pem

生成的标准格式es256私钥示例

es256_private_key.pem
txt
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIAhaV62YXNexj9/GY9EUcz25G69/afOiF2Vh0CxYlKRvoAoGCCqGSM49
AwEHoUQDQgAEuXR/yRJaNAAMGC0gaB/5H9f8ySyN5gGP3pUUotZq5UwrFTufXCG3
QC0rLKQpqy8t1/GQU+S2Hsrwf/PvB/o7KQ==
-----END EC PRIVATE KEY-----

将标准格式ES256私钥转成PKCS8格式ES256私钥

bash
openssl pkcs8 -topk8 -nocrypt -in es256_private_key.pem -out es256_pkcs8_private_key.pem

生成的pkcs8格式es256私钥示例

es256_pkcs8_private_key.pem
txt
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgCFpXrZhc17GP38Zj
0RRzPbkbr39p86IXZWHQLFiUpG+hRANCAAS5dH/JElo0AAwYLSBoH/kf1/zJLI3m
AY/elRSi1mrlTCsVO59cIbdALSsspCmrLy3X8ZBT5LYeyvB/8+8H+jsp
-----END PRIVATE KEY-----

将PKCS8格式ES256私钥转成标准格式ES256私钥

bash
openssl ec -in es256_pkcs8_private_key.pem -out es256_private_key.pem

生成的标准格式es256私钥示例

es256_private_key.pem
txt
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIAhaV62YXNexj9/GY9EUcz25G69/afOiF2Vh0CxYlKRvoAoGCCqGSM49
AwEHoUQDQgAEuXR/yRJaNAAMGC0gaB/5H9f8ySyN5gGP3pUUotZq5UwrFTufXCG3
QC0rLKQpqy8t1/GQU+S2Hsrwf/PvB/o7KQ==
-----END EC PRIVATE KEY-----

用标准格式ES256私钥或PKCS8格式ES256私钥生成公钥

bash
openssl ec -in es256_private_key.pem -pubout -out es256_public_key.pem

bash
openssl ec -in es256_pkcs8_private_key.pem -pubout -out es256_public_key.pem

生成的公钥示例

es256_public_key.pem
txt
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEuXR/yRJaNAAMGC0gaB/5H9f8ySyN
5gGP3pUUotZq5UwrFTufXCG3QC0rLKQpqy8t1/GQU+S2Hsrwf/PvB/o7KQ==
-----END PUBLIC KEY-----
© 2015-2022 tongqijie.com 版权所有沪ICP备17000682号