关于 openssl的几种重要并常用的用法,用c语言进行实践(base64 ,md5,rsa,ssh_hash,file,socket,mem,random )

安装及说明,请参考

https://blog.csdn.net/jenie/article/details/106741002

 

base64 编解码:

#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

#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

#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

#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

#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

#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

#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 

#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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用openssl库在Windows下用C语言生成RSA密钥对并进行加解密的源代码。需要注意的是,以下代码需要在包含openssl库的环境下编译运行。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/rsa.h> #include <openssl/pem.h> #include <openssl/err.h> #define KEY_LENGTH 2048 #define PUB_EXP 3 #define MESSAGE_SIZE 1024 int main() { RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL); unsigned char plaintext[MESSAGE_SIZE] = "This is a message to be encrypted"; unsigned char ciphertext[MESSAGE_SIZE]; unsigned char decryptedtext[MESSAGE_SIZE]; int ciphertext_len, decryptedtext_len; // 加密过程 ciphertext_len = RSA_public_encrypt(strlen(plaintext)+1, plaintext, ciphertext, keypair, RSA_PKCS1_PADDING); if(ciphertext_len == -1) { printf("加密失败: %s\n", ERR_error_string(ERR_get_error(), NULL)); exit(EXIT_FAILURE); } // 打印加密结果 printf("加密后的密文:\n"); for(int i = 0; i < ciphertext_len; i++) { printf("%02x ", ciphertext[i]); } printf("\n"); // 解密过程 decryptedtext_len = RSA_private_decrypt(ciphertext_len, ciphertext, decryptedtext, keypair, RSA_PKCS1_PADDING); if(decryptedtext_len == -1) { printf("解密失败: %s\n", ERR_error_string(ERR_get_error(), NULL)); exit(EXIT_FAILURE); } // 打印解密结果 printf("解密后的明文:%s\n", decryptedtext); RSA_free(keypair); return 0; } ``` 以上代码使用openssl库生成了一个2048位的RSA密钥对,并使用公钥加密了一段文本,再使用私钥进行解密。您可以根据需要进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值