信息安全设计/密码系统设计 实验1-1

完整版pdf链接(按打开速度排序):
【链接1】https://smallpdf.com/cn/share-document#r=result&t=ceb65455176bf1b7d3e4dbfc8b71bb0d&i=share
【链接2】https://maipdf.cn/file/at68f7a5525bd5e/pdf
【链接3】https://pdfhost.io/zh-CN/v/PBjpKUw3ny_实验1-1(1-3学时)

注意:下面图片加载不出来是正常的,因为图片太多,没放到博客园上,下面只是md的存档,阅读建议看上面的pdf

(一)实验1-1(1-3学时)

一、OpenSSL

openssl version

01

$ echo 123 | openssl sm3
SM3(stdin)= e95001aed4b6f7de59169913997dace404f05091ed49c37133a9950a69405a9c

02

$ echo "123" | openssl sm3
SM3(stdin)= e95001aed4b6f7de59169913997dace404f05091ed49c37133a9950a69405a9c

03

$ echo 123 | od -tx1 -tc
0000000 31 32 33 0a
1 2 3 \n
0000004

tmp8CB

$ echo -n 123 | od -tx1 -tc
0000000 31 32 33
1 2 3

tmp546C

$ echo 123 | openssl sm3
SM3(stdin)= e95001aed4b6f7de59169913997dace404f05091ed49c37133a9950a69405a9c

05

$ echo -n 123 | openssl sm3
SM3(stdin)= 6e0f9e14344c5406a0cf5a3b4dfb665f87f4a771a31f7edbb5c72874a32b2957

06

$ echo 123 > 123.txt
$ openssl sm3 -file 123.txt
SM3(123.txt)= e95001aed4b6f7de59169913997dace404f05091ed49c37133a9950a69405a9c

04

$ echo 123 | openssl sm3
SM3(stdin)= e95001aed4b6f7de59169913997dace404f05091ed49c37133a9950a69405a9c

08

$ echo "obase=16;123" | bc
7B

tmp26BB

$ echo -n -e "\x7B" > 123.bin
$ od -tx1 123.bin
0000000 7b
0000001

07

$ openssl sm3 -file 123.bin
SM3(123.bin)= 2ed59fea0dbe4e4f02de67ee657eb6be8e22a7db425103402d8a36d7b6f6d344

tmpA1F7

$ echo -ne "\x7B" | openssl sm3
SM3(stdin)= 2ed59fea0dbe4e4f02de67ee657eb6be8e22a7db425103402d8a36d7b6f6d344

09

$ openssl prime -help

prime

素数检查

$ openssl prime 3
3 (3) is prime

tmp698C

$ openssl prime 33
21 (33) is not prime

tmpA752

$ openssl prime -checks 10 33
21 (33) is not prime

tmpED36

$ openssl prime -hex 4F
4F (4F) is prime

tmp476C

素数产生

$ openssl prime -generate -bits 10
997
$ openssl prime 997
3E5 (997) is prime

$ openssl prime -generate -bits 10
821
$ openssl prime 821
335 (821) is prime

$ openssl prime -generate -bits 10 -hex
0377
$ openssl prime -hex 0377
377 (0377) is prime

tmp9798

rand

help

$ openssl rand -help

Usage: rand [options] num

General options:
 -help               Display this summary
 -engine val         Use engine, possibly a hardware device

Output options:
 -out outfile        Output file
 -base64             Base64 encode output
 -hex                Hex encode output

Random state options:
 -rand val           Load the given file(s) into the random number generator
 -writerand outfile  Write random data to the specified file

Provider options:
 -provider-path val  Provider load path (must be before 'provider' argument if required)
 -provider val       Provider to load (can be specified multiple times)
 -propquery val      Property query used when fetching algorithms

Parameters:
 num                 Number of bytes to generate

tmp288

随机数产生

$ openssl rand 10
eb�!t�t��

$ openssl rand 10 | od -tx1
0000000 e0 97 c1 45 ae 31 47 04 6a 71
0000012

$ openssl rand 10 | xxd -p
b303d64a1838d08f1a81

$ openssl rand -hex 10
496bdfc8f1453fcb23cb

$ openssl rand -base64 10
O1YkLrtRJHMS+Q==

tmpD848

随机数文件

$ openssl rand -out r1.bin 10
$ od -tx1 r1.bin
0000000 2d 61 d9 79 7b c9 48 02 62 a5
0000012

$ openssl rand 10 > r2.bin
$ cat r2.bin | xxd -p
bac2b83462b226ff380c

tmpC18F

base64

help

$ openssl base64 -help

Usage: base64 [options]

General options:
 -help               Display this summary
 -list               List ciphers
 -ciphers            Alias for -list
 -e                  Encrypt
 -d                  Decrypt
 -p                  Print the iv/key
 -P                  Print the iv/key and exit
 -engine val         Use engine, possibly a hardware device

Input options:
 -in infile          Input file
 -k val              Passphrase
 -kfile infile       Read passphrase from file

Output options:
 -out outfile        Output file
 -pass val           Passphrase source
 -v                  Verbose output
 -a                  Base64 encode/decode, depending on encryption flag
 -base64             Same as option -a
 -A                  Used with -[base64|a] to specify base64 buffer as a single line

Encryption options:
 -nopad              Disable standard block padding
 -salt               Use salt in the KDF (default)
 -nosalt             Do not use salt in the KDF
 -debug              Print debug info
 -bufsize val        Buffer size
 -K val              Raw key, in hex
 -S val              Salt, in hex
 -iv val             IV in hex
 -md val             Use specified digest to create a key from the passphrase
 -iter +int          Specify the iteration count and force the use of PBKDF2
                     Default: 10000
 -pbkdf2             Use password-based key derivation function 2 (PBKDF2)
                     Use -iter to change the iteration count from 10000
 -none               Don't encrypt
 -saltlen +int       Specify the PBKDF2 salt length (in bytes)
                     Default: 16
 -*                  Any supported cipher

Random state options:
 -rand val           Load the given file(s) into the random number generator
 -writerand outfile  Write random data to the specified file

Provider options:
 -provider-path val  Provider load path (must be before 'provider' argument if required)
 -provider val       Provider to load (can be specified multiple times)
 -propquery val      Property query used when fetching algorithms

tmp2B37

编码解码

$ echo ljp | openssl base64
bGpwCg==

$ echo ljp | openssl base64 -e
bGpwCg==

$ echo bGpwCg== | openssl base64 -d
ljp

$ echo -ne "\x11\x22\x33" | openssl base64
ESIz
$ echo ESIz | openssl base64 -d | xxd -p
112233

$ echo -ne "\x11\x22\x33\x44" | openssl base64
ESIzRA==
$ echo ESIzRA== | openssl base64 -d | xxd -p
11223344
tmp1903

文件编码解码

$ echo ljp > ljp.txt
$ openssl base64 -in ljp.txt -out ljp.b64
$ cat ljp.b64
bGpwCg==

$ openssl base64 -d -in ljp.b64 -out ljp2.txt
$ diff ljp.txt ljp2.txt

$ cat ljp2.txt
ljp

tmp1064

asn1parse

help

$ openssl asn1parse -help

Usage: asn1parse [options]

General options:
 -help           Display this summary
 -oid infile     file of extra oid definitions

I/O options:
 -inform parm    input format - one of DER PEM B64
 -in infile      input file
 -out outfile    output file (output format is always DER)
 -noout          do not produce any output
 -offset +int    offset into file
 -length +int    length of section in file
 -strparse +int  offset; a series of these can be used to 'dig'
 -genstr val     string to generate ASN1 structure from
                 into multiple ASN1 blob wrappings
 -genconf val    file to generate ASN1 structure from
 -strictpem      equivalent to '-inform pem' (obsolete)
 -item val       item to parse and print
                 (-inform  will be ignored)

Formatting options:
 -i              indents the output
 -dump           unknown data in hex form
 -dlimit +int    dump the first arg bytes of unknown data in hex form

tmp8130

密码工程中的格式

$ openssl asn1parse -help

Usage: asn1parse [options]

General options:
 -help           Display this summary
 -oid infile     file of extra oid definitions

I/O options:
 -inform parm    input format - one of DER PEM B64
 -in infile      input file
 -out outfile    output file (output format is always DER)
 -noout          do not produce any output
 -offset +int    offset into file
 -length +int    length of section in file
 -strparse +int  offset; a series of these can be used to 'dig'
 -genstr val     string to generate ASN1 structure from
                 into multiple ASN1 blob wrappings
 -genconf val    file to generate ASN1 structure from
 -strictpem      equivalent to '-inform pem' (obsolete)
 -item val       item to parse and print
                 (-inform  will be ignored)

Formatting options:
 -i              indents the output
 -dump           unknown data in hex form
 -dlimit +int    dump the first arg bytes of unknown data in hex form

tmpC7D0

Hash与HMAC:dgst

help

$ openssl dgst -help

Usage: dgst [options] [file...]

General options:
 -help               Display this summary
 -list               List digests
 -engine val         Use engine e, possibly a hardware device
 -engine_impl        Also use engine given by -engine for digest operations
 -passin val         Input file pass phrase source

Output options:
 -c                  Print the digest with separating colons
 -r                  Print the digest in coreutils format
 -out outfile        Output to filename rather than stdout
 -keyform format     Key file format (ENGINE, other values ignored)
 -hex                Print as hex dump
 -binary             Print in binary form
 -xoflen +int        Output length for XOF algorithms
 -d                  Print debug info
 -debug              Print debug info

Signing options:
 -sign val           Sign digest using private key
 -verify val         Verify a signature using public key
 -prverify val       Verify a signature using private key
 -sigopt val         Signature parameter in n:v form
 -signature infile   File with signature to verify
 -hmac val           Create hashed MAC with key
 -mac val            Create MAC (not necessarily HMAC)
 -macopt val         MAC algorithm parameters in n:v form or key
 -*                  Any supported digest
 -fips-fingerprint   Compute HMAC with the key used in OpenSSL-FIPS fingerprint

Random state options:
 -rand val           Load the given file(s) into the random number generator
 -writerand outfile  Write random data to the specified file

Provider options:
 -provider-path val  Provider load path (must be before 'provider' argument if required)
 -provider val       Provider to load (can be specified multiple times)
 -propquery val      Property query used when fetching algorithms

Parameters:
 file                Files to digest (optional; default is stdin)

tmp453E

list

$ openssl dgst -list

Supported digests:                                                                                                                       
-blake2b512                -blake2s256                -md4                                                                               
-md5                       -md5-sha1                  -ripemd                                                                            
-ripemd160                 -rmd160                    -sha1                                                                              
-sha224                    -sha256                    -sha3-224                                                                          
-sha3-256                  -sha3-384                  -sha3-512                                                                          
-sha384                    -sha512                    -sha512-224                                                                        
-sha512-256                -shake128                  -shake256                                                                          
-sm3                       -ssl3-md5                  -ssl3-sha1                                                                         
-whirlpool

tmp9A25

openssl dgst -sm3 <==> openssl.sm3

$ echo ljp | openssl dgst -sm3
SM3(stdin)= a4f888920807c7502ca20d027486dfe20be6259bf9287713f7744b120f282589
$ echo ljp | openssl sm3
SM3(stdin)= a4f888920807c7502ca20d027486dfe20be6259bf9287713f7744b120f282589

$ echo ljp | openssl sm3 -hex
SM3(stdin)= a4f888920807c7502ca20d027486dfe20be6259bf9287713f7744b120f282589

$ echo ljp | openssl sm3 -binary
t���P,�
�%��(w�tK(%�

$ echo ljp | openssl sm3 -binary | xxd -p
a4f888920807c7502ca20d027486dfe20be6259bf9287713f7744b120f282589

tmp61BC

没有 -in参数,直接传文件

$ echo ljp > ljp.txt
$ openssl sm3 ljp.txt
SM3(ljp.txt)= a4f888920807c7502ca20d027486dfe20be6259bf9287713f7744b120f282589

rocedu@RocEDUWork:~/diocs/sh/openssl$ echo ljp | openssl sm3
SM3(stdin)= a4f888920807c7502ca20d027486dfe20be6259bf9287713f7744b120f282589

tmp751D

对称算法:enc

help

$ openssl enc -help

Usage: enc [options]

General options:
 -help               Display this summary
 -list               List ciphers
 -ciphers            Alias for -list
 -e                  Encrypt
 -d                  Decrypt
 -p                  Print the iv/key
 -P                  Print the iv/key and exit
 -engine val         Use engine, possibly a hardware device

Input options:
 -in infile          Input file
 -k val              Passphrase
 -kfile infile       Read passphrase from file

Output options:
 -out outfile        Output file
 -pass val           Passphrase source
 -v                  Verbose output
 -a                  Base64 encode/decode, depending on encryption flag
 -base64             Same as option -a
 -A                  Used with -[base64|a] to specify base64 buffer as a single line

Encryption options:
 -nopad              Disable standard block padding
 -salt               Use salt in the KDF (default)
 -nosalt             Do not use salt in the KDF
 -debug              Print debug info
 -bufsize val        Buffer size
 -K val              Raw key, in hex
 -S val              Salt, in hex
 -iv val             IV in hex
 -md val             Use specified digest to create a key from the passphrase
 -iter +int          Specify the iteration count and force use of PBKDF2
 -pbkdf2             Use password-based key derivation function 2
 -none               Don't encrypt
 -*                  Any supported cipher

Random state options:
 -rand val           Load the given file(s) into the random number generator
 -writerand outfile  Write random data to the specified file

Provider options:
 -provider-path val  Provider load path (must be before 'provider' argument if required)
 -provider val       Provider to load (can be specified multiple times)
 -propquery val      Property query used when fetching algorithms

tmpCBE8

list

$ openssl dgst -list

Supported digests:                                                                                                                       
-blake2b512                -blake2s256                -md4                                                                               
-md5                       -md5-sha1                  -ripemd                                                                            
-ripemd160                 -rmd160                    -sha1                                                                              
-sha224                    -sha256                    -sha3-224                                                                          
-sha3-256                  -sha3-384                  -sha3-512                                                                          
-sha384                    -sha512                    -sha512-224                                                                        
-sha512-256                -shake128                  -shake256                                                                          
-sm3                       -ssl3-md5                  -ssl3-sha1                                                                         
-whirlpool

tmpA1B

加密解密

$ openssl sm4-cbc -K "2851fa25211a48023794ae9515909603" -iv "da80e405a4998c351b0717093cbe86ab" -in ljp.txt -out ljp.enc

$ openssl sm4-cbc -d -K "2851fa25211a48023794ae9515909603" -iv "da80e405a4998c351b0717093cbe86ab" -in ljp.enc -out ljp2.txt

$ diff ljp.txt ljp2.txt

tmpBEB7

非对称算法

RSA

RSA (Rivest-Shamir-Adleman)

  • 简介:RSA 是最常用的非对称加密算法之一,主要用于数据加密和数字签名。

  • 相关命令

    • 生成RSA密钥

      openssl genpkey -algorithm RSA -out private_key.pem
      
    • 从私钥提取公钥

      openssl rsa -pubout -in private_key.pem -out public_key.pem
      
    • 加密和解密

      • 加密:

        openssl rsautl -encrypt -inkey public_key.pem -pubin -in plaintext.txt -out encrypted.bin
        
      • 解密:

        openssl rsautl -decrypt -inkey private_key.pem -in encrypted.bin -out decrypted.txt
        
    • 签名和验证

      • 签名:

        openssl dgst -sha256 -sign private_key.pem -out signature.sig file.txt
        
      • 验证:

        openssl dgst -sha256 -verify public_key.pem -signature signature.sig file.txt
        

tmp5229

SM2

SM2 是中国国家密码管理局制定的椭圆曲线公钥密码算法,主要用于数字签名、密钥交换和公钥加密,是中国国家密码标准的一部分。SM2 算法基于椭圆曲线加密,提供安全的公钥加密和签名机制。OpenSSL 从 1.1.1 版本开始支持 SM2 算法。以下是如何使用 OpenSSL 对 SM2 算法进行操作的基本步骤:

1. 生成 SM2 密钥对

要生成一个 SM2 密钥对(包含私钥和公钥):

openssl ecparam -genkey -name SM2 -out private_key.pem

2. 提取公钥

从生成的私钥中提取公钥:

openssl ec -in private_key.pem -pubout -out public_key.pem

3. 使用 SM2 进行签名

使用生成的 SM2 私钥对文件进行签名:

openssl pkeyutl -sign -inkey private_key.pem -in file.txt -sm2-id "1234567812345678" -out signature.sig

在这个例子中,-sm2-id 参数用于指定 SM2 的鉴别字符串,这在签名中是可选的,但通常需要在验证过程中提供。

4. 验证 SM2 签名

使用对应的公钥进行签名验证:

openssl pkeyutl -verify -inkey public_key.pem -pubin -in file.txt -sigfile signature.sig -sm2-id "1234567812345678"

5. 使用 SM2 进行加密

用公钥加密数据:

openssl pkeyutl -encrypt -pubin -inkey public_key.pem -in plaintext.txt -out encrypted.bin

6. 使用 SM2 进行解密

用私钥解密数据:

openssl pkeyutl -decrypt -inkey private_key.pem -in encrypted.bin -out decrypted.txt

注意事项

  • SM2 规范:SM2 是专门为中国市场设计的密码算法标准。在使用时,应确保所使用的环境和设备支持 SM2,并符合相关标准规范。
  • 版本支持:确保使用的 OpenSSL 版本是 1.1.1 或更高版本,才会原生支持 SM2。
  • SM2 的 ID:在签名和验证处理中,-sm2-id 用于指定鉴别身份的字符串(可以理解为一种上下文数据),这在某些应用场景中是必需的。

通过这些命令和操作,您可以有效地使用 OpenSSL 来处理 SM2 算法的加密和签名任务。每次使用时请确保密钥的安全性以及环境的安全配置,以防止潜在的安全风险。

tmp797F

其他签名

DSA (Digital Signature Algorithm)

  • 简介:DSA 专用于数字签名,而不用于数据加密。

  • 相关命令

    • 生成DSA参数和密钥

      openssl dsaparam -genkey -out private_dsa.pem 2048
      
    • 从私钥提取公钥

      openssl dsa -pubout -in private_dsa.pem -out public_dsa.pem
      
    • 签名和验证

      • 签名:

        openssl dgst -sha256 -sign private_dsa.pem -out signature.dss file.txt
        
      • 验证:

        openssl dgst -sha256 -verify public_dsa.pem -signature signature.dss file.txt
        

ECDSA (Elliptic Curve Digital Signature Algorithm)

  • 简介:ECDSA 是基于椭圆曲线的签名算法,提供与 RSA 类似的安全性,但使用更短的密钥。

  • 相关命令

    • 生成ECDSA私钥

      openssl ecparam -genkey -name secp256r1 -out private_ecdsa.pem
      
    • 从私钥提取公钥

      openssl ec -pubout -in private_ecdsa.pem -out public_ecdsa.pem
      
    • 签名和验证

      • 签名:

        openssl dgst -sha256 -sign private_ecdsa.pem -out signature.ecdsa file.txt
        
      • 验证:

        openssl dgst -sha256 -verify public_ecdsa.pem -signature signature.ecdsa file.txt
        

EdDSA (Edwards-curve Digital Signature Algorithm)

  • 简介:EdDSA 是一种现代、高效的数字签名算法,支持曲线如 Ed25519。

  • 相关命令

    • 生成EdDSA密钥

      openssl genpkey -algorithm ed25519 -out private_eddsa.pem
      
    • 从私钥提取公钥

      openssl pkey -pubout -in private_eddsa.pem -out public_eddsa.pem
      
    • 签名和验证

      • 签名:

        openssl pkeyutl -sign -inkey private_eddsa.pem -out signature.eddsa -rawin -in file.txt
        
      • 验证:

        openssl pkeyutl -verify -pubin -inkey public_eddsa.pem -sigfile signature.eddsa -rawin -in file.txt
        

这些命令涉及到密钥生成、加密、解密、签名和验证,是使用 OpenSSL 处理非对称加密的基本工作流。从安全角度来看,确保密钥的安全存储和管理是至关重要的。

tmpAE46

其他命令

$ openssl list -commands

asn1parse         ca                ciphers           cmp
cms               crl               crl2pkcs7         dgst
dhparam           dsa               dsaparam          ec
ecparam           enc               engine            errstr
fipsinstall       gendsa            genpkey           genrsa
help              info              kdf               list
mac               nseq              ocsp              passwd
pkcs12            pkcs7             pkcs8             pkey
pkeyparam         pkeyutl           prime             rand
rehash            req               rsa               rsautl
s_client          s_server          s_time            sess_id
smime             speed             spkac             srp
storeutl          ts                verify            version
x509

tmp5EAC

二、GmSSL

help

$ gmssl help

usage: gmssl command [options]
command -help

Commands:
  help            Print this help message
  version         Print version
  rand            Generate random bytes
  sm2keygen       Generate SM2 keypair
  sm2sign         Generate SM2 signature
  sm2verify       Verify SM2 signature
  sm2encrypt      Encrypt with SM2 public key
  sm2decrypt      Decrypt with SM2 private key
  sm3             Generate SM3 hash
  sm3hmac         Generate SM3 HMAC tag
  sm4             Encrypt or decrypt with SM4
  zuc             Encrypt or decrypt with ZUC
  sm9setup        Generate SM9 master secret
  sm9keygen       Generate SM9 private key
  sm9sign         Generate SM9 signature
  sm9verify       Verify SM9 signature
  sm9encrypt      SM9 public key encryption
  sm9decrypt      SM9 decryption
  pbkdf2          Generate key from password
  reqgen          Generate certificate signing request (CSR)
  reqsign         Generate certificate from CSR
  reqparse        Parse and print a CSR
  crlget          Download the CRL of given certificate
  crlgen          Sign a CRL with CA certificate and private key
  crlverify       Verify a CRL with issuer's certificate
  crlparse        Parse and print CRL
  certgen         Generate a self-signed certificate
  certparse       Parse and print certificates
  certverify      Verify certificate chain
  certrevoke      Revoke certificate and output RevokedCertificate record
  cmsparse        Parse CMS (cryptographic message syntax) file
  cmsencrypt      Generate CMS EnvelopedData
  cmsdecrypt      Decrypt CMS EnvelopedData
  cmssign         Generate CMS SignedData
  cmsverify       Verify CMS SignedData
  sdfutil         SDF crypto device utility
  skfutil         SKF crypto device utility
  tlcp_client     TLCP client
  tlcp_server     TLCP server
  tls12_client    TLS 1.2 client
  tls12_server    TLS 1.2 server
  tls13_client    TLS 1.3 client
  tls13_server    TLS 1.3 server

run `gmssl <command> -help` to print help of the given command

tmp9AE2

gmssl version

$ gmssl version
GmSSL 3.1.1

tmpDBAE

sm3

help

$ gmssl sm3 -help

usage: sm3 [-hex|-bin] [-pubkey pem [-id str]] [-in file] [-out file]
usage: echo -n "abc" | sm3

tmp162E

字符串

$ echo -n "ljp" | gmssl sm3
778f6133eb1cc6cb09f59fe7833d0c7a9fac8cbe8e0097042105953a10709fd6

$ echo -n "ljp" | gmssl sm3 -hex
778f6133eb1cc6cb09f59fe7833d0c7a9fac8cbe8e0097042105953a10709fd6

$ echo -n "ljp" | gmssl sm3 -bin
w�a3��� ���=
z������!�:p��

$ echo -n "ljp" | gmssl sm3 -bin | od -tx1
0000000 77 8f 61 33 eb 1c c6 cb 09 f5 9f e7 83 3d 0c 7a
0000020 9f ac 8c be 8e 00 97 04 21 05 95 3a 10 70 9f d6
0000040

tmp6F3

文件

$ echo -n "ljp" > ljp.txt

$ od -tx1 -tc ljp.txt
0000000 6c 6a 70
l j p
0000003
$ gmssl sm3 -in ljp.txt -out ljp.sm3

$ cat ljp.sm3
778f6133eb1cc6cb09f59fe7833d0c7a9fac8cbe8e0097042105953a10709fd6

tmp2796

sm2

$ gmssl sm2keygen -pass 1234 -out sm2.pem -pubout sm2pub.pem
$ ls
ljp.sm3 ljp.txt sm2.pem sm2pub.pem

$ echo -n "ljp" | gmssl sm3 -pubkey sm2pub.pem -id 1234567812345678
3586f158d93bbcdd1660f3d316fd21c523f427bb0dbc0ade022e085488e43a68

tmpCA5F

sm3hmac

help

$ gmssl sm3hmac -help
usage: sm3hmac -key hex [-in file] [-bin|-hex] [-out file]

tmp2F44

rand key

$ gmssl rand -help
usage: rand [-hex] [-rdrand|-rdseed] -outlen num [-out file]

$ gmssl rand -hex -outlen 16
E5E9CE4A122AEF1B0302032F56B2BFAB

$ echo -n "ljp" | gmssl sm3hmac -key E5E9CE4A122AEF1B0302032F56B2BFAB
908d34c4ab3ec5330f229c0a66b249134a1613418e8e4b8c90ee9026e7625e5a

tmpC9FF

sm4

help

$ gmssl sm4 -help

usage: sm4 (-cbc|-ctr|-gcm|-cbc_sm3_hmac|-ctr_sm3_hmac) {-encrypt|-decrypt} -key hex -iv hex [-aad str| -aad_hex hex] [-in file] [-out file]
Options

  Modes

   -cbc                CBC mode with padding, need 16-byte key and 16-byte iv
   -ctr                CTR mode, need 16-byte key and 16-byte iv
   -gcm                GCM mode, need 16-byte key and any iv length
   -cbc_sm3_hmac       CBC mode with padding and HMAC-SM3 (encrypt-then-mac), need 48-byte key and 16-byte iv
   -ctr_sm3_hmac       CTR mode with HMAC-SM3 (entrypt-then-mac), need 48-byte key and 16-byte iv

   -encrypt            Encrypt
   -decrypt            Decrypt
   -key hex            Symmetric key in HEX format
   -iv hex             IV in HEX format
   -aad str            Authenticated-only message
   -aad_hex hex        Authenticated-only data in HEX format
   -in file | stdin    Input data
   -out file | stdout  Output data

Examples
  echo "hello" | gmssl sm4 -gcm -encrypt -key 11223344556677881122334455667788 -iv 112233445566778811223344 -out ciphertext.bin
  gmssl sm4 -gcm -decrypt -key 11223344556677881122334455667788 -iv 112233445566778811223344 -in ciphertext.bin

  echo "hello" | gmssl sm4 -cbc_sm3_hmac -encrypt \
                       -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
                       -iv 11223344556677881122334455667788 -out ciphertext.bin
  gmssl sm4 -cbc_sm3_hmac -decrypt \
                       -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
                       -iv 11223344556677881122334455667788 -in ciphertext.bin

tmp5895

sm4

$ gmssl rand -help
usage: rand [-hex] [-rdrand|-rdseed] -outlen num [-out file]

$ gmssl rand -outlen 16 -out key.bin
$ gmssl rand -outlen 16 -out iv.bin
$ ls
iv.bin key.bin ljp.sm3 ljp.txt sm2.pem sm2pub.pem

$ od -tx1 key.bin
0000000 79 40 06 5f 8e 01 a9 2f 93 e0 76 db b7 04 60 a8
0000020

$ od -tx1 iv.bin
0000000 23 45 90 50 70 40 da 08 61 2c a0 91 52 df a2 7f
0000020

tmpD4A8

$ echo -n "ljp" | gmssl sm4_cbc -encrypt -key $(xxd -p -c 32 key.bin) -iv $(xxd -p -c 32 iv.bin) -out ljpsm4.cbc

tmp28C5

$ gmssl sm4_cbc -decrypt -key $(xxd -p -c 32 key.bin) -iv $(xxd -p -c 32 iv.bin) -in ljpsm4.cbc
ljp

tmp2906

$ KEY=\((xxd -p -c 32 key.bin) \) echo $KEY
7940065f8e01a92f93e076dbb70460a8

tmpA130

$ IV=\((xxd -p -c 32 iv.bin) \) echo $IV
234590507040da08612ca09152dfa27f

tmpFFDB

$ echo -n "ljp" | gmssl sm4_cbc -encrypt -key $KEY -iv \(IV -out ljpsm4.cbc2 \) gmssl sm4_cbc -decrypt -key $KEY -iv $IV -in ljpsm4.cbc2
ljp

tmpF359

$ diff ljpsm4.cbc ljpsm4.cbc2

$ gmssl sm4_cbc -encrypt -key $(xxd -p -c 32 key.bin) -iv $(xxd -p -c 32 iv.bin) -in ljp.txt -out ljpsm4.cbc3

$ gmssl sm4_cbc -decrypt -key $(xxd -p -c 32 key.bin) -iv $(xxd -p -c 32 iv.bin) -in ljpsm4.cbc3
ljp

$ diff ljpsm4.cbc ljpsm4.cbc3

tmpE210

sm2

help

  • sm2keygen:Generate SM2 keypair
usage: sm2keygen -pass str [-out pem] [-pubout pem]

Options
    -pass pass                  Password to encrypt the private key
    -out pem                    Output password-encrypted PKCS #8 private key in PEM format
    -pubout pem                 Output public key in PEM format
  • sm2sign:Generate SM2 signature
usage: sm2sign -key pem -pass str [-id str] [-in file] [-out file]
  • sm2verify:Verify SM2 signature
usage: sm2verify (-pubkey pem | -cert pem) [-id str] [-in file] -sig file
  • sm2encrypt:Encrypt with SM2 public key
usage: sm2verify (-pubkey pem | -cert pem) [-id str] [-in file] -sig file
  • sm2decrypt:Decrypt with SM2 private key
usage: sm2decrypt -key pem -pass str [-in file] [-out file]

sm2

$ gmssl sm2keygen -pass 1234 -out sm2.pem -pubout sm2pub.pem
$ cat sm2.pem
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIBBjBhBgkqhkiG9w0BBQ0wVDA0BgkqhkiG9w0BBQwwJwQQ4s+NhlxLMad2ZmqR
lx/PuAIDAQAAAgEQMAsGCSqBHM9VAYMRAjAcBggqgRzPVQFoAgQQUYsRfZNsfb8l
Zkjxaifp4gSBoBt7v7ywMBDHFo4mxo6xJV3JpVJhyUDBCEoVUEvzxYVypCG1FKgg
xm9+HZo3B8yNS0cgzc120zMjohOoFjdcaEF1pnXstyr0+nszqDZF2AWBxPap90Qm
x2j7ugHNlZIFNgYOCqDgHsmKI47GAOSwPd3gNpdRRgTnTep3BpQ2UGqTHSbuROfp
Ymlpek3JZz1tLsyO0dBj3OtY3ie1zC8rR2Y=
-----END ENCRYPTED PRIVATE KEY-----

tmp112B

$ cat sm2pub.pem
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEWZ7IEnzQG8YRT9vbnNxGwDp3ciMI
krKc03Ja1HtQ9DjyA73gX0G8NpGXDMeSeJJms6d5HAl2tShpBsLlT8U3JQ==
-----END PUBLIC KEY-----

tmp5A4B

$ echo ljp | gmssl sm2sign -key sm2.pem -pass 1234 -out sm2.sig #-id 1234567812345678
$ od -tx1 sm2.sig
0000000 30 45 02 21 00 94 4a 5f 5b b0 61 a3 d3 9e f5 a9
0000020 02 38 52 11 34 8f 08 f5 25 fd e1 c3 04 92 f4 ea
0000040 57 37 bf 15 20 02 20 77 13 39 8b b5 0f 38 fe 58
0000060 bc 2b f0 44 a0 f4 e6 4e 1a 5f 8a 46 a3 2a d8 c9
0000100 8f be 91 9f 96 4a 40
$ echo ljp | gmssl sm2verify -pubkey sm2pub.pem -sig sm2.sig -id 1234567812345678
verify : success

tmpE19C

$ echo ljp | gmssl sm2encrypt -pubkey sm2pub.pem -out sm2.der
$ od -tx1 sm2.der
0000000 30 6d 02 21 00 dd 92 1c 28 a2 2f 64 96 ea 08 b0
0000020 47 0b 50 1a bb 5e 09 80 7a f2 26 b4 1b 2e a8 c5
0000040 41 4e 43 ae b6 02 20 18 31 76 f0 ca a3 6d 8f 74
0000060 a8 d6 c5 1f f8 a8 a3 49 36 49 ea 43 a9 37 99 33
0000100 e8 8d fa da 52 70 09 04 20 ad ae d2 98 48 e3 64
0000120 b9 11 26 da 3f 79 d5 8d a9 44 b7 95 67 e4 0f 1a
0000140 57 7c 30 89 ae f7 a2 f7 57 04 04 9c b3 ba 50
0000157

$ gmssl sm2decrypt -key sm2.pem -pass 1234 -in sm2.der
ljp

tmpAE73

三、使用OpenSSL命令实现带签名的数字信封协议

3.两人一组,在Ubuntu或openEuler中(推荐openEuler)中使用OpenSSL命令实现带签名的数字信封协议。使用OpenSSL时Alice发送,Bob接收。Ailice,Bob在实验中要替换为自己的8位学号+姓名。使用Markdown记录详细记录实践过程,每完成一项git commit一次。(10分)
·Alice,Bob生成自己的公私钥匙对,记作:(PKa,SKa),(PKb,SKb),Alice,Bob分别拥有:

tmp8C2C

(PKa,SKa,PKb),(PKb,SKb,PKa),实验中把公钥文件拷贝给对方
·Alice发给Bob的明文plain.txt,内容为自己的姓名学号
·Alice:sm4key使用gmsslrand产生,16字节,记作k
· Alice: Sm4Enc(k,P) = C
· Alice: Sm2Enc(PKb,k) = KC
· Alice: Sm2Sign (SKa, C) = S1
·Alice:数字信封 CllKCllS1发给Bob
· Bob: Sm2Very (PKa, S1)
· Bob: Sm2Dec (SKb, KC) = k

对方:

746c5cb9adc8036783c1f04f416a93a1

我:

64b13e2fd8d2aa33ba7aa47e8c69efa8

d9e3633b38617a2a1107647b098a4652

四、使用GmSSL命令实现带签名的数字信封协议

4.两人一组,在Ubuntu或openEuler中(推荐openEuler)中使用GmSSL命令实现带签名的数字信封协议。使用GmSSL,Bob发送,Alice接收。Ailice,Bob在实验中要替换为自己的8位学号+姓名。使用Markdown记录详细记录实践过程,每完成一项git commit一次。(10分)

生成公私钥

tmp961D

生成明文

tmpA476

生成sm4会话密钥

tmp15CB

运行

tmpCF43

tmpE25F

tmpF192

tmp1C0

对方(Alice)的界面

9ad0f60f7d919325201f055dab4b294b

98d1065c50e795e9615e9ecd03d7622d

posted @ 2025-10-12 22:49  20231420  阅读(6)  评论(0)    收藏  举报