• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 // sm4 demo1: encrypt and decrypt a block of message (16 bytes)
11 
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <gmssl/sm4.h>
16 
17 
main(void)18 int main(void)
19 {
20 	SM4_KEY sm4_key;
21 	unsigned char key[16] = {
22 		0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
23 		0x01,0xf2,0x03,0x04,0x05,0x06,0x07,0x08,
24 	};
25 	unsigned char mbuf[16] = {
26 		0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
27 		0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
28 	};
29 	unsigned char cbuf[16];
30 	unsigned char pbuf[16];
31 	int i;
32 
33 	printf("key: ");
34 	for (i = 0; i < sizeof(key); i++) {
35 		printf("%02X", key[i]);
36 	}
37 	printf("\n");
38 
39 	printf("plaintext: ");
40 	for (i = 0; i < sizeof(mbuf); i++) {
41 		printf("%02X", mbuf[i]);
42 	}
43 	printf("\n");
44 
45 	sm4_set_encrypt_key(&sm4_key, key);
46 	sm4_encrypt(&sm4_key, mbuf, cbuf);
47 
48 	printf("ciphertext: ");
49 	for (i = 0; i < sizeof(cbuf); i++) {
50 		printf("%02X", cbuf[i]);
51 	}
52 	printf("\n");
53 
54 	sm4_set_decrypt_key(&sm4_key, key);
55 	sm4_decrypt(&sm4_key, cbuf, pbuf);
56 
57 	printf("decrypted: ");
58 	for (i = 0; i < sizeof(pbuf); i++) {
59 		printf("%02X", pbuf[i]);
60 	}
61 	printf("\n");
62 
63 	return 0;
64 }
65