当前位置:   article > 正文

openssl中SM2、SM3、SM4使用实例_openssl sm3

openssl sm3

目录

openssl的版本如下:

SM3使用实例

1. SM3的扎凑实例

SM2使用实例

1. 生成SM2密钥对

2.查看SM2密钥对

3.生成自签名证书

4.查看证书详情

5.私钥签名消息

6.证书验证消息签名

SM4 使用实例


openssl的版本如下:

SM3使用实例

SM3是中华人民共和国政府采用的一种密码散列函数标准,由国家密码管理局于2010年12月17日发布。相关标准为“GM/T 0004-2012 《SM3密码杂凑算法》”。

1. SM3的扎凑实例

  1. echo "adsfad" > plain.dat
  2. openssl sm3 plain.dat

SM2使用实例

1. 生成SM2密钥对

 openssl ecparam -genkey -name SM2 -out sm2keypair.pem -text

2.查看SM2密钥对

openssl ecparam -in sm2keypair.pem -text

3.生成自签名证书

 openssl req -x509 -key sm2keypair.pem -subj "/C=CN/ST=BJ/L=BJ/O=HD/OU=dev/CN=hello/emailAddress=hello@world.com" -out sm2cert.pem

4.查看证书详情

openssl x509 -in sm2cert.pem -text

执行结果如下: 

  1. Certificate:
  2. Data:
  3. Version: 3 (0x2)
  4. Serial Number:
  5. 34:05:7d:7a:a5:5a:c5:1b:5c:ae:df:24:cc:3d:17:6f:47:10:83:b9
  6. Signature Algorithm: sm3WithSM2Sign
  7. Issuer: C = CN, ST = BJ, L = BJ, O = HD, OU = dev, CN = hello, emailAddress = hello@world.com
  8. Validity
  9. Not Before: Nov 22 08:42:26 2022 GMT
  10. Not After : Dec 22 08:42:26 2022 GMT
  11. Subject: C = CN, ST = BJ, L = BJ, O = HD, OU = dev, CN = hello, emailAddress = hello@world.com
  12. Subject Public Key Info:
  13. Public Key Algorithm: id-ecPublicKey
  14. Public-Key: (256 bit)
  15. pub:
  16. 04:07:c1:b5:88:20:df:8b:a8:1d:26:9f:f7:aa:77:
  17. d3:75:3f:28:31:49:ab:32:9e:11:cd:6e:16:f0:ea:
  18. df:c3:1d:c3:08:58:15:2e:b3:cd:5f:37:38:e0:11:
  19. c4:ec:53:d6:07:66:35:1e:91:20:8d:29:f1:17:eb:
  20. 73:7d:c5:78:83
  21. ASN1 OID: SM2
  22. NIST CURVE: SM2
  23. X509v3 extensions:
  24. X509v3 Subject Key Identifier:
  25. E8:93:9C:C1:AC:5A:5C:E4:4D:B9:E1:7A:54:E0:82:42:8D:0C:57:92
  26. X509v3 Authority Key Identifier:
  27. keyid:E8:93:9C:C1:AC:5A:5C:E4:4D:B9:E1:7A:54:E0:82:42:8D:0C:57:92
  28. X509v3 Basic Constraints: critical
  29. CA:TRUE
  30. Signature Algorithm: sm3WithSM2Sign
  31. 30:45:02:20:65:28:4e:e0:64:b8:05:b1:58:12:cc:5d:48:fc:
  32. 16:57:5f:fa:2a:f3:97:4d:f8:2f:a7:f1:ee:d4:c5:15:b1:f9:
  33. 02:21:00:be:73:e3:af:9f:d7:1b:28:62:c4:11:52:3c:aa:b1:
  34. 19:79:13:25:9b:6f:a1:1f:5b:5d:5f:fd:bf:a6:da:b9:f2
  35. -----BEGIN CERTIFICATE-----
  36. MIICOTCCAd+gAwIBAgIUNAV9eqVaxRtcrt8kzD0Xb0cQg7kwCgYIKoEcz1UBg3Uw
  37. cjELMAkGA1UEBhMCQ04xCzAJBgNVBAgMAkJKMQswCQYDVQQHDAJCSjELMAkGA1UE
  38. CgwCSEQxDDAKBgNVBAsMA2RldjEOMAwGA1UEAwwFaGVsbG8xHjAcBgkqhkiG9w0B
  39. CQEWD2hlbGxvQHdvcmxkLmNvbTAeFw0yMjExMjIwODQyMjZaFw0yMjEyMjIwODQy
  40. MjZaMHIxCzAJBgNVBAYTAkNOMQswCQYDVQQIDAJCSjELMAkGA1UEBwwCQkoxCzAJ
  41. BgNVBAoMAkhEMQwwCgYDVQQLDANkZXYxDjAMBgNVBAMMBWhlbGxvMR4wHAYJKoZI
  42. hvcNAQkBFg9oZWxsb0B3b3JsZC5jb20wWTATBgcqhkjOPQIBBggqgRzPVQGCLQNC
  43. AAQHwbWIIN+LqB0mn/eqd9N1PygxSasynhHNbhbw6t/DHcMIWBUus81fNzjgEcTs
  44. U9YHZjUekSCNKfEX63N9xXiDo1MwUTAdBgNVHQ4EFgQU6JOcwaxaXORNueF6VOCC
  45. Qo0MV5IwHwYDVR0jBBgwFoAU6JOcwaxaXORNueF6VOCCQo0MV5IwDwYDVR0TAQH/
  46. BAUwAwEB/zAKBggqgRzPVQGDdQNIADBFAiBlKE7gZLgFsVgSzF1I/BZXX/oq85dN
  47. +C+n8e7UxRWx+QIhAL5z46+f1xsoYsQRUjyqsRl5EyWbb6EfW11f/b+m2rny
  48. -----END CERTIFICATE-----

5.私钥签名消息

msg.dat : 代签名数据

sm2keypair.pem:带签名私钥

openssl   dgst -sm3 -sign sm2keypair.pem  -out sm2_file.sign  plain.dat

6.证书验证消息签名


SM4 使用实例

SM4:(原名SMS4.0)是中华人民共和国政府采用的一种分组密码标准,由国家密码管理局于2012年3月21日发布。相关标准为“GM/T 0002-2012《SM4分组密码算法》(原SMS4分组密码算法)”。

  1. echo "asdf" | openssl sm4-cbc > ci.bin
  2. enter sm4-cbc encryption password:
  3. Verifying - enter sm4-cbc encryption password:
  4. *** WARNING : deprecated key derivation used.
  5. Using -iter or -pbkdf2 would be better.
  6. openssl sm4-cbc -d -in ci.bin
  7. enter sm4-cbc decryption password:
  8. *** WARNING : deprecated key derivation used.
  9. Using -iter or -pbkdf2 would be better.
  10. asdf

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/998309
推荐阅读
相关标签
  

闽ICP备14008679号