Last active
July 14, 2017 00:13
-
-
Save wilbeibi/024cd3e2a77a7693d0818e8808a88134 to your computer and use it in GitHub Desktop.
Chinese encryption SM4 cbc demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <openssl/evp.h> | |
int main(int argc, const char* argv[]) | |
{ | |
if (argc <= 1) { | |
fprintf(stderr, "Usage: %s <data>\n", argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
const char *key, *iv, *data; | |
int key_length, iv_length, data_length; | |
key = "1111222233334444"; | |
key_length = strlen(key); | |
iv = "aaaabbbbccccdddd"; | |
iv_length = strlen(iv); | |
data = argv[1]; | |
data_length = strlen(data); | |
const EVP_CIPHER *cipher; | |
int cipher_key_length, cipher_iv_length; | |
//cipher = EVP_aes_256_gcm(); | |
//cipher = EVP_aes_256_cbc(); | |
cipher = EVP_sms4_cbc(); | |
cipher_key_length = EVP_CIPHER_key_length(cipher); | |
cipher_iv_length = EVP_CIPHER_iv_length(cipher); | |
if (key_length != cipher_key_length) { | |
fprintf(stderr, "Error: key length must be %d\n", cipher_key_length); | |
exit(EXIT_FAILURE); | |
} | |
if (iv_length != cipher_iv_length) { | |
fprintf(stderr, "Error: iv length must be %d\n", cipher_iv_length); | |
exit(EXIT_FAILURE); | |
} | |
const char *p; | |
char *datax; | |
int i, datax_length; | |
datax = (char *)malloc(data_length); | |
for (p = data, i = 0; *p != '\0'; p += 2, i++) { | |
char buf[3]; | |
buf[0] = *p; | |
buf[1] = *(p+1); | |
buf[2] = '\0'; | |
datax[i] = strtol(buf, NULL, 16); | |
} | |
datax_length = i; | |
EVP_CIPHER_CTX *ctx; | |
//EVP_CIPHER_CTX_init(ctx); | |
ctx = EVP_CIPHER_CTX_new(); | |
if (ctx == NULL) { | |
printf("ctx failed to create.\n"); | |
} | |
EVP_DecryptInit_ex(ctx, cipher, NULL, (unsigned char *)key, (unsigned char *)iv); | |
int plain_length, final_length; | |
unsigned char *plaintext; | |
plain_length = datax_length; | |
plaintext = (unsigned char *)malloc(plain_length + 1); | |
EVP_DecryptUpdate(ctx, plaintext, &plain_length, (unsigned char *)datax, datax_length); | |
EVP_DecryptFinal_ex(ctx, plaintext + plain_length, &final_length); | |
plaintext[plain_length + final_length] = '\0'; | |
printf("Plaintext: %s\n", plaintext); | |
free(plaintext); | |
free(datax); | |
//EVP_CIPHER_CTX_cleanup(&ctx); | |
EVP_CIPHER_CTX_free(ctx); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <openssl/evp.h> | |
int main(int argc, const char* argv[]) | |
{ | |
if (argc <= 1) { | |
fprintf(stderr, "Usage: %s <data>\n", argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
const char *key, *iv, *data; | |
int key_length, iv_length, data_length; | |
key = "1111222233334444"; | |
key_length = strlen(key); | |
iv = "aaaabbbbccccdddd"; | |
iv_length = strlen(iv); | |
data = argv[1]; | |
data_length = strlen(data); | |
const EVP_CIPHER *cipher; | |
int cipher_key_length, cipher_iv_length; | |
//cipher = EVP_aes_256_cbc(); | |
//cipher = EVP_aes_256_gcm(); | |
cipher = EVP_sms4_cbc(); | |
cipher_key_length = EVP_CIPHER_key_length(cipher); | |
cipher_iv_length = EVP_CIPHER_iv_length(cipher); | |
if (key_length != cipher_key_length) { | |
fprintf(stderr, "Error: key length must be %d\n", cipher_key_length); | |
exit(EXIT_FAILURE); | |
} | |
if (iv_length != cipher_iv_length) { | |
fprintf(stderr, "Error: iv length must be %d\n", cipher_iv_length); | |
exit(EXIT_FAILURE); | |
} | |
EVP_CIPHER_CTX *ctx; | |
//EVP_MD_CTX *ctx; | |
ctx = EVP_CIPHER_CTX_new(); | |
if (ctx == NULL) { | |
printf("ctx failed to create.\n"); | |
} | |
int i, cipher_length, final_length; | |
unsigned char *ciphertext; | |
//EVP_CIPHER_CTX_init(&ctx); | |
EVP_EncryptInit_ex(ctx, cipher, NULL, (unsigned char *)key, (unsigned char *)iv); | |
cipher_length = data_length + EVP_MAX_BLOCK_LENGTH; | |
ciphertext = (unsigned char *)malloc(cipher_length); | |
EVP_EncryptUpdate(ctx, ciphertext, &cipher_length, (unsigned char *)data, data_length); | |
EVP_EncryptFinal_ex(ctx, ciphertext + cipher_length, &final_length); | |
for (i = 0; i < cipher_length + final_length; i++) | |
printf("%02x", ciphertext[i]); | |
printf("\n"); | |
free(ciphertext); | |
//EVP_CIPHER_CTX_cleanup(&ctx); | |
//EVP_MD_CTX_free(ctx); | |
EVP_CIPHER_CTX_free(ctx); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CFLAGS=-g -std=gnu99 -Wall -pedantic-errors | |
all: encrypt decrypt | |
encrypt: encrypt.o | |
gcc -o encrypt encrypt.o ./GmSSL/libcrypto.a -lpthread -ldl | |
encrypt.o: encrypt.c | |
gcc -c -o encrypt.o encrypt.c -I/usr/local/include | |
decrypt: decrypt.o | |
gcc -o decrypt decrypt.o ./GmSSL/libcrypto.a -lpthread -ldl | |
decrypt.o: decrypt.c | |
gcc -c -o decrypt.o decrypt.c -I/usr/local/include | |
clean: | |
rm -rf *.o encrypt decrypt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment