• 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 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <gmssl/sm2.h>
14 #include <gmssl/error.h>
15 
16 
main(void)17 int main(void)
18 {
19 	SM2_KEY sm2_key;
20 	SM2_KEY pub_key;
21 	unsigned char plaintext[SM2_MAX_PLAINTEXT_SIZE];
22 	unsigned char ciphertext[SM2_MAX_CIPHERTEXT_SIZE];
23 	size_t len;
24 
25 	sm2_key_generate(&sm2_key);
26 	memcpy(&pub_key, &sm2_key, sizeof(SM2_POINT));
27 
28 	sm2_encrypt(&pub_key, (uint8_t *)"hello world", strlen("hello world"), ciphertext, &len);
29 	format_bytes(stdout, 0, 0, "ciphertext", ciphertext, len);
30 
31 	if (sm2_decrypt(&sm2_key, ciphertext, len, plaintext, &len) != 1) {
32 		fprintf(stderr, "error\n");
33 		return 1;
34 	}
35 	plaintext[len] = 0;
36 	printf("plaintext: %s\n", plaintext);
37 
38 	return 0;
39 }
40