文章目录

《密码系统设计》实验

实验项目

实验序号 实验名称 实验学时数 实验目的 实验内容 实验类型 学生学习预期成果
实验一 嵌入式开发基础 6 掌握Linux系统使用与开发方法 Linux命令,OpenSSL(GmSSL)命令与开发 验证性 1.掌握常见的Linux命令与C语言开发方法;
2.掌握OpenSSL(GmSSL)的基本用法与开发;
3.掌握常见商用密码算法的使用

实验一 嵌入式开发基础

1-3 学时实践要求(30 分)

1. 参考云班课相关教学视频,在 Ubuntu或openEuler中(推荐 openEuler)中实践课程思维导图中OpenSSL相关内容,使用Markdown记录详细记录实践过程,每完成一项git commit 一次。(5分)
[wzy@LAPTOP-PRC71A0C practice]$ openssl help
help:

Standard 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

Message Digest commands (see the `dgst' command for more details)
blake2b512        blake2s256        md2               md4
md5               rmd160            sha1              sha224
sha256            sha3-224          sha3-256          sha3-384
sha3-512          sha384            sha512            sha512-224
sha512-256        shake128          shake256          sm3

Cipher commands (see the `enc' command for more details)
aes-128-cbc       aes-128-ecb       aes-192-cbc       aes-192-ecb
aes-256-cbc       aes-256-ecb       aria-128-cbc      aria-128-cfb
aria-128-cfb1     aria-128-cfb8     aria-128-ctr      aria-128-ecb
aria-128-ofb      aria-192-cbc      aria-192-cfb      aria-192-cfb1
aria-192-cfb8     aria-192-ctr      aria-192-ecb      aria-192-ofb
aria-256-cbc      aria-256-cfb      aria-256-cfb1     aria-256-cfb8
aria-256-ctr      aria-256-ecb      aria-256-ofb      base64
bf                bf-cbc            bf-cfb            bf-ecb
bf-ofb            camellia-128-cbc  camellia-128-ecb  camellia-192-cbc
camellia-192-ecb  camellia-256-cbc  camellia-256-ecb  cast
cast-cbc          cast5-cbc         cast5-cfb         cast5-ecb
cast5-ofb         des               des-cbc           des-cfb
des-ecb           des-ede           des-ede-cbc       des-ede-cfb
des-ede-ofb       des-ede3          des-ede3-cbc      des-ede3-cfb
des-ede3-ofb      des-ofb           des3              desx
idea              idea-cbc          idea-cfb          idea-ecb
idea-ofb          rc2               rc2-40-cbc        rc2-64-cbc
rc2-cbc           rc2-cfb           rc2-ecb           rc2-ofb
rc4               rc4-40            rc5               rc5-cbc
rc5-cfb           rc5-ecb           rc5-ofb           seed
seed-cbc          seed-cfb          seed-ecb          seed-ofb
sm4-cbc           sm4-cfb           sm4-ctr           sm4-ecb
sm4-ofb           zlib

//文本
[wzy@LAPTOP-PRC71A0C practice]$ echo 123 | openssl sm3
SM3(stdin)= e95001aed4b6f7de59169913997dace404f05091ed49c37133a9950a69405a9c

[wzy@LAPTOP-PRC71A0C practice]$ echo "123" | openssl sm3
SM3(stdin)= e95001aed4b6f7de59169913997dace404f05091ed49c37133a9950a69405a9c

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

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

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

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

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

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

//二进制
$ echo "obase=16;123" | bc
7B

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

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

$ echo -ne "\x7B" | openssl sm3
SM3(stdin)= 2ed59fea0dbe4e4f02de67ee657eb6be8e22a7db425103402d8a36d7b6f6d344
[wzy@LAPTOP-PRC71A0C practice]$ git log
commit 6d89d88567c2eeed2eaa0389a97607cc123dfc90 (HEAD -> master)
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 20:21:37 2024 +0800

    add 123.bin

commit cce039154368b4f8d260d033280be0f911d8e908
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 20:20:15 2024 +0800

    add 123.txt
$ 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
[wzy@LAPTOP-PRC71A0C practice]$ git log
commit 639073878a664f18d2f98525401800337ad2c0cf (HEAD -> master)
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 20:24:23 2024 +0800

    add ljp.txt

commit 6d89d88567c2eeed2eaa0389a97607cc123dfc90
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 20:21:37 2024 +0800

    add 123.bin

commit cce039154368b4f8d260d033280be0f911d8e908
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 20:20:15 2024 +0800

    add 123.txt
2. 参考云班课相关教学视频,在 Ubuntu或openEuler中(推荐 openEuler)中实践课程课程思维导图中GmSSL相关内容,使用Markdown记录详细记录实践过程,每完成一项git commit 一次。(5‘)

在这里插入图片描述(5分)

[wzy@LAPTOP-PRC71A0C practice]$ gmssl version
GmSSL 3.1.2 Dev
[wzy@LAPTOP-PRC71A0C practice]$ gmssl sm3 -help
usage: sm3 [-hex|-bin] [-pubkey pem [-id str]] [-in file|-in_str str] [-out file]
Options

    -hex                   Output hash value as hex string (by default)
    -bin                   Output hash value as binary
    -pubkey pem            Signer's SM2 public key
                           When `-pubkey` is specified, hash with SM2 Z value
    -id str                SM2 Signer's ID string
    -id_hex hex            SM2 Signer's ID in hex format
                           `-id` and `-id_hex` should be used with `-pubkey`
                           `-id` and `-id_hex` should not be used together
                           If `-pubkey` is specified without `-id` or `id_hex`,
                           the default ID string '1234567812345678' is used
    -in_str str            To be hashed string
    -in file | stdin       To be hashed file path
                           `-in_str` and `-in` should not be used together
                           If neither `-in` nor `-in_str` specified, read from stdin
    -out file | stdout     Output file path. If not specified, output to stdout

Examples

    gmssl sm3 -in_str abc

    gmssl sm3 -in_str abc -bin

    gmssl sm3 -in /path/to/file

    gmssl sm3 -pubkey sm2pubkey.pem -id alice -in /path/to/file -bin

  When reading from stdin, make sure the trailing newline character is removed

  Linux/Mac:
    echo -n abc | gmssl sm3

  Windows:
    C:\> echo |set/p="abc" | gmssl sm3

//字符串
$ 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

//文件
$ 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

//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
[wzy@LAPTOP-PRC71A0C practice]$ git log
commit bd6e12afcf704d860651f93a40f238e0d29a5e19 (HEAD -> master)
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 20:46:57 2024 +0800

    add sm2pub.pem

commit 639073878a664f18d2f98525401800337ad2c0cf
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 20:24:23 2024 +0800

    add ljp.txt

commit 6d89d88567c2eeed2eaa0389a97607cc123dfc90
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 20:21:37 2024 +0800

    add 123.bin

commit cce039154368b4f8d260d033280be0f911d8e908
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 20:20:15 2024 +0800

    add 123.txt
3. 两人一组,在 Ubuntu或openEuler中(推荐 openEuler)中使用OpenSSL命令实现带签名的数字信封协议。使用OpenSSL时Alice发送,Bob接收。Ailice,Bob在实验中要替换为自己的8位学号+姓名。 使用Markdown记录详细记录实践过程,每完成一项git commit 一次。(10分)
  • Alice,Bob生成自己的公私钥匙对,记作:(PKa,SKa),(PKb,SKb),Alice,Bob分别拥有:(PKa,SKa,PKb),(PKb,SKb,PKa),实验中把公钥文件拷贝给对方
  • Alice发给Bob的明文plain.txt,内容为自己的姓名学号
  • Alice:sm4 key使用gmssl rand 产生,16字节,记作k
  • Alice:Sm4Enc(k,P) = C
  • Alice:Sm2Enc(PKb,k) = KC
  • Alice:Sm2Sign(SKa,C)= S1
  • Alice: 数字信封 C||KC||S1 发给Bob
  • Bob:Sm2Very(PKa,S1)
  • Bob:Sm2Dec(SKb,KC)= k
  • Bob:Sm4Dec(k,C)= P

实验步骤记录

环境准备
确保你的系统是openEuler或Ubuntu,并已安装OpenSSL和GmSSL工具。实验中将使用OpenSSL进行加密和签名,GmSSL生成SM4密钥。

  1. 生成密钥对
    Alice和Bob分别生成自己的公私钥对。

Alice生成密钥对

openssl ecparam -name sm2p256v1 -genkey -noout -out 20221417wzy_private_key.pem 
openssl pkey -in alice_private_key.pem -pubout -out 20221417_public_key.pem 

Bob生成密钥对

openssl ecparam -name sm2p256v1 -genkey -noout -out 20221312fax_private_key.pem 
openssl pkey -in bob_private_key.pem -pubout -out 20221312fax_public_key.pem 

Git Commit

git add alice_private_key.pem alice_public_key.pem bob_private_key.pem bob_public_key.pem 
git commit -m "生成Alice和Bob的密钥对"
  1. Alice生成SM4密钥
    Alice使用GmSSL生成16字节的随机SM4密钥。
gmssl rand -hex 16 | tee sm4_key.bin 

Git Commit

git add sm4_key.bin 
git commit -m "Alice生成SM4密钥"
  1. Alice加密明文
    Alice使用SM4密钥加密明文文件plain.txt
echo "学号姓名" > plain.txt 
gmssl sm4-cbc -salt -in plain.txt -out cipher.bin -kfile sm4_key.bin 

Git Commit

git add plain.txt cipher.bin 
git commit -m "Alice使用SM4密钥加密明文"
  1. Alice加密SM4密钥
    Alice使用Bob的公钥加密SM4密钥。
openssl sm2 encrypt -in sm4_key.bin -out key_encrypted.bin -pubin -inkey bob_public_key.pem 

Git Commit

git add key_encrypted.bin 
git commit -m "Alice加密SM4密钥"
  1. Alice签名加密后的数据
    Alice使用自己的私钥对加密后的数据进行签名。
openssl sm2 sign -in cipher.bin -out signature.bin -key alice_private_key.pem 

Git Commit

git add signature.bin 
git commit -m "Alice签名加密后的数据"
  1. Alice发送数字信封给Bob
    Alice将加密后的数据、加密的SM4密钥和签名发送给Bob。

数字信封内容

C||KC||S1 = cipher.bin||key_encrypted.bin||signature.bin 
  1. Bob验证签名
    Bob使用Alice的公钥验证签名。
openssl sm2 verify -in cipher.bin -sig signature.bin -pubin -inkey alice_public_key.pem 

Git Commit

git commit -m "Bob验证Alice的签名"
  1. Bob解密SM4密钥
    Bob使用自己的私钥解密SM4密钥。
openssl sm2 decrypt -in key_encrypted.bin -out sm4_key_decrypted.bin -inkey bob_private_key.pem 

Git Commit

git add sm4_key_decrypted.bin 
git commit -m "Bob解密SM4密钥"
  1. Bob解密数据
    Bob使用解密后的SM4密钥解密数据。
gmssl sm4-cbc -d -in cipher.bin -out plain_decrypted.txt -kfile sm4_key_decrypted.bin 

Git Commit

git add plain_decrypted.txt 
git commit -m "Bob解密数据"

实验总结
通过以上步骤,Alice成功地创建了一个带签名的数字信封,并将其发送给Bob。Bob成功地验证了签名,解密了密钥,并最终解密了数据。每个步骤都通过Git进行了版本控制,以确保实验过程的完整性和可追溯性。

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

bob
私钥

 gmssl sm2keygen -pass 1417 -out 20221417wzy_private_key.pem

公钥

gmssl sm2keygen -pass 1417 -out 20221417wzy_private_key.pem -pubout 20221417wzy_public_key.pem

在这里插入图片描述
生成sm4密钥16位

gmssl rand -out sm4_key.bin -outlen 16

创建并加密消息
Bob创建一个消息文件,并使用Alice的公钥加密。

echo "This is a secret message from 20221417wzy" > plain.txt  
gmssl pkeyutl -encrypt -pubin -inkey 20221417wzy_public_key.pem  -in plain.txt  -out signature.bin  
gmssl pkeyutl -sign -inkey 20221417wzy_private_key.pem  -in encrypted_message.bin  -out signature.bin  

Bob将加密后的消息和签名发送给Alice。
Alice验证并解密消息
Alice收到加密消息和签名后,首先验证签名,然后解密消息。

Alice验证签名

gmssl pkeyutl -verify -pubin -inkey bob_public_key.pem  -sigfile signature.bin  -in encrypted_message.bin  

Alice解密消息

gmssl pkeyutl -decrypt -inkey alice_private_key.pem  -in encrypted_message.bin  -out decrypted_message.txt  
5. 实验记录中提交 gitee 课程项目链接,提交本次实验相关 git log运行结果
commit 32a69f47e7f91fc12a589b4f750a8ee60d33c05a (HEAD -> master)
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 17:29:09 2024 +0800

    Add plain.txt

commit 7788890053aded92cb1eaf9dd61f28097f677845
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 16:54:36 2024 +0800

    Add 20221417wzy.enc
[wzy@LAPTOP-PRC71A0C exp2]$ git log
commit 2a95781f6626855dcbe1700611e5529829a18f29 (HEAD -> master)
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 16:05:51 2024 +0800

    Add gmssl_s1.sig

commit ee6052f8508177d23b70f551e53d3355d44a6950
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 13:34:46 2024 +0800

    完成bob【生成密钥对】
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 20:00:10 2024 +0800

    add gmssl_plain.txt gmssl_s1.sig

commit 2a95781f6626855dcbe1700611e5529829a18f29
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 16:05:51 2024 +0800

    Add gmssl_s1.sig

commit ee6052f8508177d23b70f551e53d3355d44a6950
Author: wei-zhengyi <https://gitee.com/wei-zhengyi/projects>
Date:   Sun Oct 13 13:34:46 2024 +0800    
gmssl rand -out sm4_key.bin -outlen 16
gmssl sm4 -cbc -encrypt -in plain.txt -out encrypted_message.bin -key $(xxd -p -c 256 sm4_key.bin) -iv 0
gmssl sm4_cbc -encrypt -in plain.txt -out encrypted_message.bin -key $(xxd -p -c 256 sm4_key.bin) -iv 0
 
6. 提交要求:
  • 提交实践过程Markdown和转化的PDF文件
  • 代码,文档托管到gitee或github等,推荐 gitclone
  • 记录实验过程中遇到的问题,解决过程,反思等内容,完成实验报告相关内容
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐