安装及说明,请参考
https://blog.csdn.net/jenie/article/details/106741002
base64 编解码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <string.h> #include <openssl/evp.h> int main() { unsigned char in[100], base64[150], decode[100]; EVP_ENCODE_CTX ectx; EVP_EncodeInit(&ectx); int i = 0; for (i = 0;i < 100;i ++) { in[i] = i; } int outl, inl = 100; EVP_EncodeUpdate(&ectx, base64, &outl, in, inl); int total = outl; EVP_EncodeFinal(&ectx, base64+total, &outl); printf("%s\n", base64); } |
socket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <stdio.h> #include <openssl/bio.h> #define OUT_LENGTH 128 int main() { int sock = BIO_get_accept_socket("8899", 0); BIO *b = BIO_new_socket(sock, BIO_NOCLOSE); char *addr = NULL; int ret = BIO_accept(sock, &addr); BIO_set_fd(b, ret, BIO_NOCLOSE); while (1) { char out[OUT_LENGTH] = {0}; BIO_read(b, out, OUT_LENGTH); if (out[0] = 'q') break; printf("%s\n", out); } BIO_free(b); return 0; } |
file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <stdio.h> #include <openssl/bio.h> int main() { BIO *b = NULL; int len = 0, outlen = 0; char *out = NULL; b = BIO_new_file("bf.txt", "w"); len = BIO_write(b, "OpenSSL", 4); len = BIO_printf(b, "%s", "zcp"); BIO_free(b); b = BIO_new_file("bf.txt", "r"); len = BIO_pending(b); len = 50; out = (char*)OPENSSL_malloc(len); len = 1; while (len > 0) { len = BIO_read(b, out+outlen, 1); outlen += len; } printf("out: %s\n", out); BIO_free(b); free(out); return 0; } |
rsa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <openssl/rsa.h> int main() { RSA *r; int bits = 512, ret; unsigned long e = RSA_3; BIGNUM *bne; r = RSA_generate_key(bits, e, NULL, NULL); RSA_print_fp(stdout, r, 11); RSA_free(r); //printf("\n\n----------------------------------\n\n"); bne = BN_new(); ret = BN_set_word(bne, e); //printf("\n\n----------------------------------\n\n"); r = RSA_new(); RSA_generate_key_ex(r, bits, bne, NULL); if (ret != 1) { printf("RSA_generate_key_ex err!\n"); return -1; } RSA_free(r); return 0; } |
md5 sha1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <stdio.h> #include <string.h> #include <openssl/objects.h> #include <openssl/rsa.h> int main() { unsigned char in[] = "EVP_ENCODE_CTX ectx, dctx; \ unsigned char in[100], base64[150], decode[100]; \ EVP_EncodeInit(&ectx); \ int i = 0; \ for (i = 0;i < 100;i ++) { \ in[i] = i;\ } "; unsigned char out[128] = {0}; int n = strlen(in); int i = 0; MD4(in, n, out); printf("MD4 result: \n"); // 16 byte for (i = 0;i < 16;i ++) { printf("%x", out[i]); } printf("\n"); MD5(in, n, out); printf("MD4 result: \n"); // 16 byte for (i = 0;i < 16;i ++) { printf("%x", out[i]); } printf("\n"); SHA(in, n, out); printf("MD4 result: \n"); // 20 byte for (i = 0;i < 16;i ++) { printf("%x", out[i]); } printf("\n"); SHA1(in, n, out); printf("MD4 result: \n"); // 20 byte for (i = 0;i < 20;i ++) { printf("%x", out[i]); } printf("\n"); SHA256(in, n, out); printf("MD4 result: \n"); // 32 byte for (i = 0;i < 32;i ++) { printf("%x", out[i]); } printf("\n"); SHA512(in, n, out); printf("MD4 result: \n"); // 64 byte for (i = 0;i < 64;i ++) { printf("%x", out[i]); } printf("\n"); } |
mem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> #include <openssl/bio.h> int main() { BIO *b = BIO_new(BIO_s_mem()); int len = BIO_write(b, "OpenSSL", 4); len = BIO_printf(b, "%s", "zcp"); printf("len: %d\n", len); len = BIO_ctrl_pending(b); printf("len: %d\n", len); char *out = OPENSSL_malloc(len); len = BIO_read(b, out, len); printf("len: %d\n", len); OPENSSL_free(out); BIO_free(b); return 0; } |
hash
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <stdio.h> #include <openssl/lhash.h> #define NAME_LENGTH 32 typedef struct _Person { char name[NAME_LENGTH]; int high; char otherInfo[NAME_LENGTH]; } Person; static int person_cmp(const void *a, const void *b) { char *namea = ((Person*)a)->name; char *nameb = ((Person*)b)->name; return strcmp(namea, nameb); } void print_value(void *a) { Person *p = (Person*)a; printf("name: %s\n", p->name); printf("high: %d\n", p->high); printf("other info : %s\n", p->otherInfo); } int main() { _LHASH *h = lh_new(NULL, person_cmp); if (h == NULL) { printf("err.\n"); return -1; } Person p1 = {"King", 170, "xxxx"}; Person p2 = {"Darren", 175, "xxxx"}; Person p3 = {"Mark", 170, "xxxx"}; Person p4 = {"Milo", 170, "xxxx"}; lh_insert(h, &p1); lh_insert(h, &p2); lh_insert(h, &p3); lh_insert(h, &p4); lh_doall(h, print_value); printf("\n\n\n------------------------------\n\n\n"); void *data = lh_retrieve(h, (const char *)"King"); if (data == NULL) { return -1; } print_value(data); lh_free(h); return 0; } |
random
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd=0; char *buff=NULL; unsigned long ulTest = 0; fd = open("/dev/random",O_RDONLY); read(fd,&ulTest,sizeof(ulTest)); printf("%lu\n",ulTest); close(fd); } |