实验1-2

openssl编程库

SM2

加解密

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$  openssl ecparam -genkey -name SM2 -out private_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ls
plain.txt  private_key.pem  sm2_encrypt  sm2_encrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ cat private_key.pem
-----BEGIN SM2 PARAMETERS-----
BggqgRzPVQGCLQ==
-----END SM2 PARAMETERS-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBG0wawIBAQQgtrLeLsDFsnFZ6sIw
0J4C+jnDoJFGbWK8G8zr7xNGC5mhRANCAAQmRe2LaRCI9nhBTw+3dlrDKuZ2PPDI
1OeFtzDh2U+caSXM9gRx47CkgibXfDT6XSQQAdyNizqObSO7ZCyWKrTH
-----END PRIVATE KEY-----
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ vim encrypted_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ vim decrypted_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ vim sm2_encrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ gcc -o sm2_encrypt sm2_encrypt.c -lcrypto
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ gcc -o sm2_decrypt sm2_decrypt.c -lcrypto
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ls
decrypted_file  private_key.pem  sm2_decrypt.c  sm2_encrypt.c
encrypted_file  sm2_decrypt      sm2_encrypt

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ openssl pkey -in private_key.pem -pubout -out pubkey.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ls
decrypted_file  private_key.pem  sm2_decrypt    sm2_encrypt
encrypted_file  pubkey.pem       sm2_decrypt.c  sm2_encrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ cat pubkey.pem
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEJkXti2kQiPZ4QU8Pt3Zawyrmdjzw
yNTnhbcw4dlPnGklzPYEceOwpIIm13w0+l0kEAHcjYs6jm0ju2Qsliq0xw==
-----END PUBLIC KEY-----
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ vim plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ vim encrypted_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ./sm2_encrypt plain.txt encrypted_file
Usage: ./sm2_encrypt <pubkey.pem> <input file> <output file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ./sm2_encrypt pubkey.pem  plain.txt encrypted_file
Encryption complete.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ./sm2_decrypt private_key.pem encrypted_file decrypted_file
Decryption successful.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ vim decrypted_file

签名验签

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ vim sign.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ gcc sign.c -o sign -lcrypto
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ls
decrypted_file  private_key.pem  sign.c         sm2_encrypt
encrypted_file  pubkey.pem       sm2_decrypt    sm2_encrypt.c
plain.txt       sign             sm2_decrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ./sign
Usage: ./sign <private_key.pem> <input_file> <output_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ vim signature
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ./sign private_key.pem plain.txt  signature
Signature successfully written to signature
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ vim verify.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ gcc verify.c -o verify -lcrypto
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ./verify
Usage: ./verify <public_key.pem> <input_file> <signature_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ls
decrypted_file  private_key.pem  signature    sm2_decrypt.c  verify
encrypted_file  pubkey.pem       sign.c       sm2_encrypt    verify.c
plain.txt       sign             sm2_decrypt  sm2_encrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ mv pubkey.pem  public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ls
decrypted_file  plain.txt        public_key.pem  signature  sm2_decrypt    sm2_encrypt    verify
encrypted_file  private_key.pem  sign            sign.c     sm2_decrypt.c  sm2_encrypt.c  verify.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ ./verify public_key.pem plain.txt signature 
Signature verification successful!
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm2_test$ git commit -m "sign & verify"
[master 68b769c] sign & verify
 6 files changed, 229 insertions(+)
 rename pubkey.pem => public_key.pem (100%)
 create mode 100755 sign
 create mode 100644 sign.c
 create mode 100644 signature
 create mode 100755 verify
 create mode 100644 verify.c

SM3

摘要

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ vim hash.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ gcc -o hash hash.c -lcrypto
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ ls
hash  hash.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ ./hash
Usage: ./hash <string_to_hash>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ ./hash "HELLO WORLD!"
SM3 hash: 36ed3c0919b78f76b6f7de3d4c04c8c49060cac731c67c82277fdcb68b9f2e86
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ git commit -m "sm3_hash"
[master d7fe9a5] sm3_hash
 2 files changed, 51 insertions(+)
 create mode 100755 shiyan1-2/openssl/sm3_test/hash
 create mode 100644 shiyan1-2/openssl/sm3_test/hash.c

HMAC计算

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ vim hmac.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ gcc -o hmac hmac.c -lcrypto
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ ./hmac
Usage: ./hmac <key> <data>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ ./hmac "20221425lyhlyhll" "The quick brown fox jumps over the lazy dog"
HMAC: 28fee9e93ac43f087b45eb62567b3c1ae788dbc2ad95c26959ae47bf675a2cd7
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ ./hmac "1234567812345678" "The quick brown fox jumps over the lazy dog"
HMAC: f289d6f4b36e9719579c4a12b12e9238f748e800d27bfa798a072683ef91b12a
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm3_test$ git commit -m "sm3_hmac"
[master 5614ae2] sm3_hmac
 2 files changed, 63 insertions(+)
 create mode 100755 shiyan1-2/openssl/sm3_test/hmac
 create mode 100644 shiyan1-2/openssl/sm3_test/hmac.c

SM4

加解密

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ vim plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ vim encrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ gcc -o encrypt encrypt.c -lcrypto
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ ls
encrypt  encrypt.c  plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ ./encrypt
Usage: ./encrypt <key> <iv> <input file> <output file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ vim encrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ ./encrypt 1234567812345678 1111111111111111 plain.txt ^C
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ ./encrypt 1234567812345678 1111111111111111 plain.txt encrypt_file
Encryption complete.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ ls
encrypt  encrypt.c  encrypt_file  plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ vim decrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ vim decrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ gcc -o decrypt decrypt.c -lcrypto
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ ls
decrypt  decrypt.c  decrypt_file  encrypt  encrypt.c  encrypt_file  plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ ./decrypt
Usage: ./decrypt <key> <iv> <input file> <output file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ ./decrypt 1234567812345678 1111111111111111 encrypt_file decrypt_file
Decryption complete.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ vim decrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ vim encrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ vim plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/openssl/sm4_test$ git commit -m "sm4_encrypt & sm4_decrypt"
[master 3118113] sm4_encrypt & sm4_decrypt
 7 files changed, 147 insertions(+)
 create mode 100755 shiyan1-2/openssl/sm4_test/decrypt
 create mode 100644 shiyan1-2/openssl/sm4_test/decrypt.c
 create mode 100644 shiyan1-2/openssl/sm4_test/decrypt_file
 create mode 100755 shiyan1-2/openssl/sm4_test/encrypt
 create mode 100644 shiyan1-2/openssl/sm4_test/encrypt.c
 create mode 100644 shiyan1-2/openssl/sm4_test/encrypt_file
 create mode 100644 shiyan1-2/openssl/sm4_test/plain.txt

gmssl编程库

SM2

加密

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ vim plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ touch encrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
encrypt_file  plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ vim encrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ gmssl sm2keygen -pass 1234 -out sm2_private_key.pem -pubout sm2_public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
encrypt_file  plain.txt  sm2_private_key.pem  sm2_public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ cat sm2_private_key.pem
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIBBjBhBgkqhkiG9w0BBQ0wVDA0BgkqhkiG9w0BBQwwJwQQ6B/KmCIrvhrGfmvu
3hfm3QIDAQAAAgEQMAsGCSqBHM9VAYMRAjAcBggqgRzPVQFoAgQQpvlaMXnq8JiG
iPkKHFTpkgSBoIxkqS+ThJFDz0aTD0f7LdjRT/0X7NNzXlOxRL+it2udQKxvQlo4
kN2RP10lANAv/+2mVbf7Poc0DKotGOX+fvUEjK/fLD42g5GTEsV3WiqfYtQjToyq
B+OuaQJtfe7c2P5j1yGadNdK3d7XQNgQJajT0bpa78nJ9kAhrJlo0hy95rFPBO8Q
n/RyiUp1wrn+BETChJPl+lyF+OicaNIZx1E=
-----END ENCRYPTED PRIVATE KEY-----
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ cat sm2_public_key.pem
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEKiUZ3v6qn3YeFrDEUZKdg4daOqSu
YAqHn1CcKS7inX2sgAwXDNPsQUOyxVOmwHzzniZENxLzXhF2ZDykEi3a2w==
-----END PUBLIC KEY-----
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ vim sm2_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
encrypt_file  plain.txt  sm2_keygen.c  sm2_private_key.pem  sm2_public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ gcc -o sm2_keygen sm2_keygen.c -lcrypto -lssl
/usr/bin/ld: /tmp/cc36TNKV.o: in function `main':
sm2_keygen.c:(.text+0x95): undefined reference to `sm2_key_generate'
/usr/bin/ld: sm2_keygen.c:(.text+0x123): undefined reference to `sm2_private_key_info_to_pem'
/usr/bin/ld: sm2_keygen.c:(.text+0x1cc): undefined reference to `sm2_public_key_info_to_pem'
collect2: error: ld returned 1 exit status
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ gcc -o sm2_keygen sm2_keygen.c
/usr/bin/ld: /tmp/ccgcpM13.o: in function `main':
sm2_keygen.c:(.text+0x95): undefined reference to `sm2_key_generate'
/usr/bin/ld: sm2_keygen.c:(.text+0x123): undefined reference to `sm2_private_key_info_to_pem'
/usr/bin/ld: sm2_keygen.c:(.text+0x1cc): undefined reference to `sm2_public_key_info_to_pem'
collect2: error: ld returned 1 exit status
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ gcc -o sm2_keygen sm2_keygen.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
encrypt_file  sm2_keygen    sm2_private_key.pem
plain.txt     sm2_keygen.c  sm2_public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_key_gen
bash: ./sm2_key_gen: 没有那个文件或目录
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_keygen
Usage: ./sm2_keygen <private_key_file> <public_key_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ touch private_key_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ touch public_key_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_keygen 1234 private_key_file public_key_file
Usage: ./sm2_keygen <private_key_file> <public_key_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_keygen private_key_file public_key_file
SM2 key pair generated successfully.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
encrypt_file  private_key_file  sm2_keygen    sm2_private_key.pem
plain.txt     public_key_file   sm2_keygen.c  sm2_public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ cat sm2_private_key.pem
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIBBjBhBgkqhkiG9w0BBQ0wVDA0BgkqhkiG9w0BBQwwJwQQ6B/KmCIrvhrGfmvu
3hfm3QIDAQAAAgEQMAsGCSqBHM9VAYMRAjAcBggqgRzPVQFoAgQQpvlaMXnq8JiG
iPkKHFTpkgSBoIxkqS+ThJFDz0aTD0f7LdjRT/0X7NNzXlOxRL+it2udQKxvQlo4
kN2RP10lANAv/+2mVbf7Poc0DKotGOX+fvUEjK/fLD42g5GTEsV3WiqfYtQjToyq
B+OuaQJtfe7c2P5j1yGadNdK3d7XQNgQJajT0bpa78nJ9kAhrJlo0hy95rFPBO8Q
n/RyiUp1wrn+BETChJPl+lyF+OicaNIZx1E=
-----END ENCRYPTED PRIVATE KEY-----
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ cat private_key_file
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgDVnf6CV2CxuZJIb2
HswvoRwkoFD1w+24Abq85bD49UGgCgYIKoEcz1UBgi2hRANCAAQdx9TdVzVbSx1i
NBk3BLe4V//q8maQD+wvg4Sy6PXLt8Qpp0bFE5+crndfb/isRTcmoCQCqfBUt0LE
1upFhgVn
-----END PRIVATE KEY-----
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ rm private_key_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ rm public_key_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
encrypt_file  sm2_keygen    sm2_private_key.pem
plain.txt     sm2_keygen.c  sm2_public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ touch private.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ touch public.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_keygen private.pem public.pem
SM2 key pair generated successfully.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ cat sm2_private_key.pem
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIBBjBhBgkqhkiG9w0BBQ0wVDA0BgkqhkiG9w0BBQwwJwQQ6B/KmCIrvhrGfmvu
3hfm3QIDAQAAAgEQMAsGCSqBHM9VAYMRAjAcBggqgRzPVQFoAgQQpvlaMXnq8JiG
iPkKHFTpkgSBoIxkqS+ThJFDz0aTD0f7LdjRT/0X7NNzXlOxRL+it2udQKxvQlo4
kN2RP10lANAv/+2mVbf7Poc0DKotGOX+fvUEjK/fLD42g5GTEsV3WiqfYtQjToyq
B+OuaQJtfe7c2P5j1yGadNdK3d7XQNgQJajT0bpa78nJ9kAhrJlo0hy95rFPBO8Q
n/RyiUp1wrn+BETChJPl+lyF+OicaNIZx1E=
-----END ENCRYPTED PRIVATE KEY-----
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ cat private.pem
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg5PghXdCaMitdnQfF
Ti1DYmRcJ40HaywFkSi1wz9zq76gCgYIKoEcz1UBgi2hRANCAAR8QdqRNWaKmIZB
C6l0mMSVDBqXpmrGJSRknFpRjTYzKntUE8Ip8KZCeP/pCzDNiPfj6p9xXd70VEK0
kjBX0gOr
-----END PRIVATE KEY-----
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ vim sm2_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
encrypt_file  private.pem  sm2_keygen    sm2_private_key.pem
plain.txt     public.pem   sm2_keygen.c  sm2_public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ rm sm2_private_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ rm sm2_public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
encrypt_file  plain.txt  private.pem  public.pem  sm2_keygen  sm2_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ vim sm2_encrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ gcc -o sm2_encrypt sm2_encrypt.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
encrypt_file  private.pem  sm2_encrypt    sm2_keygen
plain.txt     public.pem   sm2_encrypt.c  sm2_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_encypt
bash: ./sm2_encypt: 没有那个文件或目录
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_encrypt
Usage: sm2_encrypt <public_key.pem> <input_file> <output_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_encrypt public.pem plain.txt encrypt_file
Encryption successful, output written to encrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ vim encrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ gcc add .
/usr/bin/ld: 找不到 add: 没有那个文件或目录
/usr/bin/ld: 找不到 .: file format not recognized
collect2: error: ld returned 1 exit status
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ git commit -m "sm2_kengen & sm2_encrypt"
[master b6cff89] sm2_kengen & sm2_encrypt
 8 files changed, 148 insertions(+)
 create mode 100644 shiyan1-2/gmssl/sm2_test/encrypt_file
 create mode 100644 shiyan1-2/gmssl/sm2_test/plain.txt
 create mode 100644 shiyan1-2/gmssl/sm2_test/private.pem
 create mode 100644 shiyan1-2/gmssl/sm2_test/public.pem
 create mode 100755 shiyan1-2/gmssl/sm2_test/sm2_encrypt
 create mode 100644 shiyan1-2/gmssl/sm2_test/sm2_encrypt.c
 create mode 100755 shiyan1-2/gmssl/sm2_test/sm2_keygen
 create mode 100644 shiyan1-2/gmssl/sm2_test/sm2_keygen.c

解密

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ touch decrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ vim sm2_decrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ gcc -o sm2_decrypt sm2_decrypt.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_decrypt private.pem encrypt_file decrypt_file
 
/home/user/下载/GmSSL-master/src/asn1.c:1932:asn1_length_is_zero():
/home/user/下载/GmSSL-master/src/sm2_enc.c:517:sm2_decrypt():
SM2 decryption failed
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
decrypt_file  private.pem  sm2_decrypt.c  sm2_keygen
encrypt_file  public.pem   sm2_encrypt    sm2_keygen.c
plain.txt     sm2_decrypt  sm2_encrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_decrypt
Usage: sm2_decrypt <private_key.pem> <input_file> <output_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_decrypt private.pem encrypt_file decrypt_file
/home/user/下载/GmSSL-master/src/asn1.c:1932:asn1_length_is_zero():
/home/user/下载/GmSSL-master/src/sm2_enc.c:517:sm2_decrypt():
SM2 decryption failed
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ rm encrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
decrypt_file  private.pem  sm2_decrypt    sm2_encrypt    sm2_keygen
plain.txt     public.pem   sm2_decrypt.c  sm2_encrypt.c  sm2_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ touch encrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ vim encrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_encrypt public.pem plain.txt encrypt_file
Encryption successful, output written to encrypt_file
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_decrypt private.pem encrypt_file decrypt_file
Decryption successful, output written to decrypt_file

签名

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ touch signature.sig
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
decrypt_file  private.pem    sign.c         sm2_encrypt    sm2_keygen.c
encrypt_file  public.pem     sm2_decrypt    sm2_encrypt.c
plain.txt     signature.sig  sm2_decrypt.c  sm2_keygen
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ mv sign.c sm2_sign.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
decrypt_file  private.pem    sm2_decrypt    sm2_encrypt.c  sm2_sign.c
encrypt_file  public.pem     sm2_decrypt.c  sm2_keygen
plain.txt     signature.sig  sm2_encrypt    sm2_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ gcc -o sm2_sign sm2_sign.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
decrypt_file  private.pem    sm2_decrypt    sm2_encrypt.c  sm2_sign
encrypt_file  public.pem     sm2_decrypt.c  sm2_keygen     sm2_sign.c
plain.txt     signature.sig  sm2_encrypt    sm2_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_sign
Usage: ./sm2_sign <private_key.pem> <input.txt> <signature.sig>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_sign private.pem plain.txt signature.sig
Signature generated and saved to signature.sig
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ git commit -m "sm2_sign"
[master aae07da] sm2_sign
 3 files changed, 85 insertions(+)
 create mode 100644 shiyan1-2/gmssl/sm2_test/signature.sig
 create mode 100755 shiyan1-2/gmssl/sm2_test/sm2_sign
 create mode 100644 shiyan1-2/gmssl/sm2_test/sm2_sign.c

验签

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ vim sm2_verify.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ gcc -o sm2_verify sm2_verify.c -lgmssl

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ls
decrypt_file  public.pem     sm2_encrypt    sm2_sign
encrypt_file  signature.sig  sm2_encrypt.c  sm2_sign.c
plain.txt     sm2_decrypt    sm2_keygen     sm2_verify
private.pem   sm2_decrypt.c  sm2_keygen.c   sm2_verify.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_verify
Usage: ./sm2_verify <public key PEM file> <input file> <signature file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_verify public.pem plain.txt signature.sig
/home/user/下载/GmSSL-master/src/asn1.c:1932:asn1_length_is_zero():
/home/user/下载/GmSSL-master/src/sm2_sign.c:459:sm2_verify():
Signature is invalid. Please check the public key, input file, and signature file.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ rm signature.sig 
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_sign private.pem plain.txt signature.sig
Signature generated and saved to signature.sig
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ ./sm2_verify public.pem plain.txt signature.sig
Signature is valid.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm2_test$ git commit -m "sm2_verify"
[master 0b5ad41] sm2_verify
 3 files changed, 87 insertions(+)
 create mode 100755 shiyan1-2/gmssl/sm2_test/sm2_verify
 create mode 100644 shiyan1-2/gmssl/sm2_test/sm2_verify.c

SM3

摘要

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl$ cd sm3_test
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ vim plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ vim sm3_hash.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ gcc -o sm3_hash sm3_hash.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ ls
plain.txt  sm3_hash  sm3_hash.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ ./sm3_hash plain.txt
Usage: sm3_digest <input_file> <output_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ touch hash.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ ls
hash.bin  plain.txt  sm3_hash  sm3_hash.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ ./sm3_hash plain.txt hash.bin
SM3 digest computed and written to hash.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ vim hash.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ vim plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ git commit -m "sm3_hash"
[master a56a6bb] sm3_hash
 4 files changed, 72 insertions(+)
 create mode 100644 shiyan1-2/gmssl/sm3_test/hash.bin
 create mode 100644 shiyan1-2/gmssl/sm3_test/plain.txt
 create mode 100755 shiyan1-2/gmssl/sm3_test/sm3_hash
 create mode 100644 shiyan1-2/gmssl/sm3_test/sm3_hash.c

HMAC

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ vim sm3_hmac.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ gcc -o sm3_hmac sm3_hmac.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ ./sm3_hmac
Usage: ./sm3_hmac <key_hex> <input_string>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ ./sm3_hmac "0123456789abcdef0123456789abcdef" "Hello, World!"
sm3hmac: illegal option 'World!'
HMAC-SM3 result: user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ ls
hash.bin  plain.txt  sm3_hash  sm3_hash.c  sm3_hmac  sm3_hmac.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ vim sm3_hmac.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ gcc -o sm3_hmac sm3_hmac.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ ls
hash.bin  plain.txt  sm3_hash  sm3_hash.c  sm3_hmac  sm3_hmac.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ ./sm3_hmac "0123456789abcdef0123456789abcdef" "Hello, World!"
HMAC-SM3 result: accf3b95ad80f2b127dc8e05105d8b4125ad0c9f0c6a674b066c95bf35a7a651
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm3_test$ git commit -m "sm3_hmac"
[master ea70c70] sm3_hmac
 2 files changed, 41 insertions(+)
 create mode 100755 shiyan1-2/gmssl/sm3_test/sm3_hmac
 create mode 100644 shiyan1-2/gmssl/sm3_test/sm3_hmac.c

SM4

加解密

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ vim sm4_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ gcc -o sm4_keygen sm4_keygen.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ ./sm4_keygen
Usage: ./sm4_keygen <output_filename>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ touch sm4_key
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ rm sm4_key
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ touch sm4_key.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ ls
sm4_key.bin  sm4_keygen  sm4_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ ./sm4_keygen sm4_key.bin
Key generated and saved to sm4_key.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ ls
sm4_key.bin  sm4_keygen  sm4_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ vim plain.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ vim sm4_encrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ gcc -o sm4_encrypt sm4_encrypt.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ ./sm4 _encrypt
bash: ./sm4: 没有那个文件或目录
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ sm4_encrypt
sm4_encrypt:未找到命令
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ ./sm4_encrypt
Usage: ./sm4_encrypt <key_file> <input_file> <output_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ touch encrypt.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ ./sm4_encrypt sm4_key.bin plain.txt encrypt.bin
Encryption completed successfully.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ touch decrypt.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ ls
decrypt.bin  plain.txt    sm4_encrypt.c  sm4_keygen
encrypt.bin  sm4_encrypt  sm4_key.bin    sm4_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ vim sm4_decrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ gcc -o sm4_decrypt sm4_decrypt.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ ./sm4_decrypt
Usage: ./sm4_decrypt <key_file> <input_file> <output_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ ./sm4_decrypt sm4_key.bin encrypt.bin decrypt.bin
Decryption completed successfully.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ vim decrypt.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ vim encrypt.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/gmssl/sm4_test$ git commit -m "sm4_encrypt & sm4_decrypt"
[master 8e3f7ae] sm4_encrypt & sm4_decrypt
 10 files changed, 206 insertions(+)
 create mode 100644 shiyan1-2/gmssl/sm4_test/decrypt.bin
 create mode 100644 shiyan1-2/gmssl/sm4_test/encrypt.bin
 create mode 100644 shiyan1-2/gmssl/sm4_test/plain.txt
 create mode 100755 shiyan1-2/gmssl/sm4_test/sm4_decrypt
 create mode 100644 shiyan1-2/gmssl/sm4_test/sm4_decrypt.c
 create mode 100755 shiyan1-2/gmssl/sm4_test/sm4_encrypt
 create mode 100644 shiyan1-2/gmssl/sm4_test/sm4_encrypt.c
 create mode 100644 shiyan1-2/gmssl/sm4_test/sm4_key.bin
 create mode 100755 shiyan1-2/gmssl/sm4_test/sm4_keygen
 create mode 100644 shiyan1-2/gmssl/sm4_test/sm4_keygen.c

openssl数字信封

交换密钥

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl$ cd sm2_test
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ ls
decrypted_file  private_key.pem  signature    sm2_decrypt.c  verify
encrypted_file  public_key.pem   sign.c       sm2_encrypt    verify.c
plain.txt       sign             sm2_decrypt  sm2_encrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ ./sm2_encrypt
Usage: ./sm2_encrypt <pubkey.pem> <input file> <output file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ ls
decrypted_file  private_key.pem  signature    sm2_decrypt.c  sm2_public_key.pem
encrypted_file  public_key.pem   sign.c       sm2_encrypt    verify
plain.txt       sign             sm2_decrypt  sm2_encrypt.c  verify.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ vim input.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ rm input.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ vim input.txt
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ git commit -m "key_change"
[master 7ca0802] key_change
 3 files changed, 5 insertions(+)
 create mode 100644 input.txt
 create mode 100644 sm2_public_key.pem

加密明文签名加密SM4的密钥

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ ./sm4_encrypt
Usage: ./sm4_encrypt <key_file> <iv_file> <input_file> <output_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ cat input.txt
20221425lyh&20221414xlm
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ touch encrypt_input.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ ./sm4_encrypt key.bin  iv.bin input.txt encrypt_input.bin
Encryption complete.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ ./sm2_encrypt
Usage: ./sm2_encrypt <pubkey.pem> <input file> <output file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ touch encrypted_sm4_key.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ ./sm2_encrypt sm2_public_key.pem encrypt_input.bin encrypted_sm4_key.bin
Encryption complete.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ touch signen_encrypted_txt.sig
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ ./verify
Usage: ./verify <public_key.pem> <input_file> <signature_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ ./sign
Usage: ./sign <private_key.pem> <input_file> <output_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ mv signen_encrypted_txt.sig signed_encrypted_txt.sig

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ ./sign private_key.pem encrypt_input.bin signed_encrypted_txt.sig
Signature successfully written to signed_encrypted_txt.sig
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/copenssl/sm2_test$ git commit -m "encrypt_sm4_key & sign encrypted_mingwen & encrypted_mingwen"
[master d8b274f] encrypt_sm4_key & sign encrypted_mingwen & encrypted_mingwen
 7 files changed, 121 insertions(+)
 create mode 100644 encrypt_input.bin
 create mode 100644 encrypted_sm4_key.bin
 create mode 100644 iv.bin
 create mode 100644 key.bin
 create mode 100644 signed_encrypted_txt.sig
 create mode 100755 sm4_encrypt
 create mode 100644 sm4_encrypt.c

gmssl数字信封

交换密钥

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ./sm2_keygen bob_prikey.pem bob_pubkey.pem
SM2 key pair generated successfully.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ls
bob_prikey.pem  bob_pubkey.pem  sm2_keygen  sm2_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ls
bob_prikey.pem  bob_pubkey.pem  sm2_keygen  sm2_keygen.c  sm2_public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ls
bob_prikey.pem       encrypted_sm4_key.bin       sm2_keygen.c
bob_pubkey.pem       signed_encrypted_plain.sig  sm2_public_key.pem
encrypted_plain.bin  sm2_keygen

验签

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ vim sm2_verify.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ gcc -o sm2_verify sm2_verify.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ls
bob_prikey.pem         signed_encrypted_plain.sig  sm2_verify
bob_pubkey.pem         sm2_keygen                  sm2_verify.c
encrypted_plain.bin    sm2_keygen.c
encrypted_sm4_key.bin  sm2_public_key.pem
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ./sm2_verify
Usage: ./sm2_verify <public key PEM file> <input file> <signature file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ./sm2_verify sm2_public_key.pem encrypted_plain.bin signed_encrypted_plain.sig
Signature is valid.

解密出SM4密钥

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ vim sm2_decrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ gcc -o sm2_decrypt sm2_decrypt.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ls
bob_prikey.pem         signed_encrypted_plain.sig  sm2_keygen.c
bob_pubkey.pem         sm2_decrypt                 sm2_public_key.pem
encrypted_plain.bin    sm2_decrypt.c               sm2_verify
encrypted_sm4_key.bin  sm2_keygen                  sm2_verify.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ./sm2_decrypt
Usage: sm2_decrypt <private_key.pem> <input_file> <output_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ touch decrypted_key.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ./sm2_decrypt bob_prikey.pem encrypted_sm4_key.bin decrypted_key.bin
/home/user/下载/GmSSL-master/src/sm2_enc.c:517:sm2_decrypt():
SM2 decryption failed
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ls
bob_prikey.pem         signed_encrypted_plain.sig  sm2_public_key.pem
bob_pubkey.pem         sm2_decrypt                 sm2_verify
decrypted_key.bin      sm2_decrypt.c               sm2_verify.c
encrypted_plain.bin    sm2_keygen
encrypted_sm4_key.bin  sm2_keygen.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ./sm2_decrypt bob_prikey.pem encrypted_sm4_key.bin decrypted_key.bin
Decryption successful, output written to decrypted_key.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ cat decrypted_key.bin
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ hexdump decrypted_key.bin
0000000 a72f a2e7 7bb8 429a 1d1e c426 0fd8 4864
0000010
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ git commit -m "sm2_change & decryptkey & verifysing"
[master 95cfb8a] sm2_change & decryptkey & verifysing
 13 files changed, 240 insertions(+)
 create mode 100644 shiyan1-2/cgmssl/bob_prikey.pem
 create mode 100644 shiyan1-2/cgmssl/bob_pubkey.pem
 create mode 100644 shiyan1-2/cgmssl/decrypted_key.bin
 create mode 100644 shiyan1-2/cgmssl/encrypted_plain.bin
 create mode 100644 shiyan1-2/cgmssl/encrypted_sm4_key.bin
 create mode 100644 shiyan1-2/cgmssl/signed_encrypted_plain.sig
 create mode 100755 shiyan1-2/cgmssl/sm2_decrypt
 create mode 100644 shiyan1-2/cgmssl/sm2_decrypt.c
 create mode 100755 shiyan1-2/cgmssl/sm2_keygen
 create mode 100644 shiyan1-2/cgmssl/sm2_keygen.c
 create mode 100644 shiyan1-2/cgmssl/sm2_public_key.pem
 create mode 100755 shiyan1-2/cgmssl/sm2_verify
 create mode 100644 shiyan1-2/cgmssl/sm2_verify.c

用SM4密钥解密出明文

user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ vim sm4_decrypt.c
                                                        ls
bob_prikey.pem         output.txt                  sm2_keygen.c
bob_pubkey.pem         signed_encrypted_plain.sig  sm2_public_key.pem
decrypted_key.bin      sm2_decrypt                 sm2_verify
encrypted_plain.bin    sm2_decrypt.c               sm2_verify.c
encrypted_sm4_key.bin  sm2_keygen                  sm4_decrypt.c
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ 
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ gcc -o sm4_decrypt sm4_decrypt.c -lgmssl
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ./sm4_decrypt
Usage: ./sm4_decrypt <key_file> <input_file> <output_file>
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ ./sm4_decrypt decrypted_key.bin encrypted_plain.bin output.txt
Decryption completed successfully.
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ cat output.txt
20221414xlm&20221425lyh
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ git add .
user@user-VirtualBox:~/shiyan/shiyan1/shiyan1-2/cgmssl$ git commit -m "sm4_decrypt"
[master 7590904] sm4_decrypt
 3 files changed, 77 insertions(+)
 create mode 100644 shiyan1-2/cgmssl/output.txt
 create mode 100755 shiyan1-2/cgmssl/sm4_decrypt
 create mode 100644 shiyan1-2/cgmssl/sm4_decrypt.c

git log

commit 7846dde95188c726e68910993121956b8340633a (HEAD -> master)
Author: lyh <2931957608@qq.com>
Date:   Sun Oct 20 18:00:17 2024 +0800

    alice

commit 759090437c4f9fb11de945243f801fd0d7491b01
Author: lyh <2931957608@qq.com>
Date:   Sun Oct 20 11:28:00 2024 +0800

    sm4_decrypt

commit 95cfb8acb476b7012526cac7ccc91adc70aff224
Author: lyh <2931957608@qq.com>
Date:   Sun Oct 20 11:20:07 2024 +0800

    sm2_change & decryptkey & verifysing

commit 8e3f7ae65756678358cb0ac7d96b5db14d795faa
Author: lyh <2931957608@qq.com>
Date:   Sat Oct 19 13:29:05 2024 +0800

    sm4_encrypt & sm4_decrypt

commit ea70c7014a56076280e0a0b5d6ed17ef1a029752
Author: lyh <2931957608@qq.com>

commit 7846dde95188c726e68910993121956b8340633a (HEAD -> master)
Author: lyh <2931957608@qq.com>
Date:   Sun Oct 20 18:00:17 2024 +0800

    alice

commit 759090437c4f9fb11de945243f801fd0d7491b01
Author: lyh <2931957608@qq.com>
Date:   Sun Oct 20 11:28:00 2024 +0800

    sm4_decrypt

commit 95cfb8acb476b7012526cac7ccc91adc70aff224
Author: lyh <2931957608@qq.com>
Date:   Sun Oct 20 11:20:07 2024 +0800

    sm2_change & decryptkey & verifysing

commit 8e3f7ae65756678358cb0ac7d96b5db14d795faa
Author: lyh <2931957608@qq.com>
Date:   Sat Oct 19 13:29:05 2024 +0800

    sm4_encrypt & sm4_decrypt

commit ea70c7014a56076280e0a0b5d6ed17ef1a029752
Author: lyh <2931957608@qq.com>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值