实验二测试结果——openssl API使用

发布时间 2023-10-20 18:35:29作者: 20211115fyq

sm3加密代码及测试截图

sm3代码:用sm3加密“20211115fanyiqing”生成摘要值作为输出。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include "openssl/evp.h"
 4 #include "err.h"
 5 
 6 void tDigest(){
 7         unsigned char md_value[EVP_MAX_MD_SIZE];
 8         int md_len, i;
 9         EVP_MD_CTX *mdctx;
10         char msg1[] = "20211115";
11         char msg2[] = "fanyiqing";
12         mdctx = EVP_MD_CTX_new();
13         EVP_MD_CTX_init(mdctx);
14 
15         EVP_DigestInit_ex(mdctx, EVP_sm3(), NULL);
16         EVP_DigestUpdate(mdctx, msg1, strlen(msg1));
17         EVP_DigestUpdate(mdctx, msg2, strlen(msg2));
18         EVP_DigestFinal_ex(mdctx, md_value, &md_len);
19         EVP_MD_CTX_free(mdctx);
20 
21         printf("%s%s的SM3摘要值:\n", msg1, msg2);
22         for(i = 0; i < md_len; i++){
23                 printf("%02x", md_value[i]);
24         }
25         printf("\n");
26 }
27 
28 int main(){
29         OpenSSL_add_all_algorithms();
30         tDigest();
31         return 0;
32 }

用此代码对不同明文加密,重复测试了3次,测试截图如下:

测试1-1:明文为20211115fanyiqing

测试1-2:明文为20211115fan

 

 

测试1-3:明文为20211115asdfgh

 

sm4加密代码及测试截图

sm4代码:用sm4加密“20211115fanyiqing”生成摘要值作为输出。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <openssl/evp.h>
 5 
 6 void handleErrors()
 7 {
 8     printf("An error occurred\n");
 9     abort();
10 }
11 
12 int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
13   unsigned char *iv, unsigned char *ciphertext)
14 {
15     EVP_CIPHER_CTX *ctx;
16     int len;
17     int ciphertext_len;
18 
19     /* Create and initialize the context */
20     if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
21     if(1 != EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key, iv)) handleErrors();
22 
23     /* Encrypt the plaintext */
24     if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
25         handleErrors();
26     ciphertext_len = len;
27 
28     /* Finalize the encryption */
29     if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
30     ciphertext_len += len;
31 
32     /* Clean up the context */
33     EVP_CIPHER_CTX_free(ctx);
34 
35     return ciphertext_len;
36 }
37 
38 int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
39   unsigned char *iv, unsigned char *plaintext)
40 {
41     EVP_CIPHER_CTX *ctx;
42     int len;
43     int plaintext_len;
44 
45     /* Create and initialize the context */
46     if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
47     if(1 != EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key, iv)) handleErrors();
48 
49     /* Decrypt the ciphertext */
50     if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
51         handleErrors();
52     plaintext_len = len;
53 
54     /* Finalize the decryption */
55     if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
56     plaintext_len += len;
57 
58     /* Clean up the context */
59     EVP_CIPHER_CTX_free(ctx);
60 
61     return plaintext_len;
62 }
63 
64 int main(int arc, char *argv[]) {
65     unsigned char *key = (unsigned char *)"0123456789qwerty";
66     unsigned char *iv = (unsigned char *)"0123456789qwerty";
67     unsigned char *plaintext = (unsigned char *)"20211115fanyiqing";
68     int plaintext_len = strlen((char *)plaintext);
69     unsigned char ciphertext[128];
70     unsigned char decryptedtext[128];
71     int decryptedtext_len, ciphertext_len;
72 
73     OpenSSL_add_all_algorithms();
74     ERR_load_CRYPTO_strings;
75 
76     ciphertext_len = encrypt(plaintext, plaintext_len, key, iv, ciphertext);
77     printf("Ciphertext is:\n");
78     BIO_dump_fp(stdout, (const char *)ciphertext, ciphertext_len);
79 
80     decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);
81     decryptedtext[decryptedtext_len] = '\0';
82     printf("Decrypted text is:\n");
83     printf("%s\n", decryptedtext);
84 
85     return 0;
86 }

用此代码对不同明文加密,重复测试了3次,测试截图如下:

测试2-1:明文为20211115fanyiqing

 测试2-2:明文为20211115

测试2-3:明文为20211115asdfgh