当前位置: 代码迷 >> 综合 >> Windows+VS2019环境各密码算法库(openssl\GMP\ZLib)的环境配置及用法总结
  详细解决方案

Windows+VS2019环境各密码算法库(openssl\GMP\ZLib)的环境配置及用法总结

热度:0   发布时间:2024-01-31 15:31:59.0

文章目录

    • GMP
    • ZLIB
    • OPENSSL

GMP

要点:首先VS中只能使用.lib文件作静态库,网上用.a的纯属**

安装:教程+编译完成的文件

环境

首先将gmp.h及libgmp-10.lib放到同一文件夹下,记为GMPLib

  1. 工程右键→属性→c/c++→常规→附加包含目录:GMPLib(gmp.h所在目录)
  2. 工程右键→属性→链接器→常规→附加库目录:GMPLib(libgmp-10.lib所在目录)
  3. 工程右键→属性→链接器→输入→附加依赖项:libgmp-10.lib

使用

#include <stdio.h>
#include <gmp.h>
const int MAXN = 1e5 + 5;
typedef mpz_t bint;char sa[2020], sb[2020];int main()
{mpz_t a,b,t;	//mpz_t 为GMP内置大数类型scanf("%s", sa);scanf("%s", sb);mpz_init_set_str(a, sa, 10); //大数a初始化为字符串sa代表的数字mpz_init_set_str(b, sb, 10);mpz_init(t);    //大数t使用前要进行初始化,以便动态分配空间//mpz_ui_pow_ui(t, 2, 100); //GMP所有函数基本都是以mpz打头int i;for(i=1;i<=100;i++)mpz_mul(t, a, b);gmp_printf("a * b = %Zd\n", t);   //输出大数,大数的格式化标志为%Zdmpz_clears(a,b,t,NULL); //清除,最后以NULL结尾return 0;
}

ZLIB

要点:此处使用单一静态库,环境配置中与上述有所不同,需要添加3个宏定义

安装:教程

环境

  1. 包含头文件

    属性–C/C+±-常规–附加包含目录(添加包含.h文件的目录)

  2. 定义宏

    属性–C/C+±-预处理器–预处理器定义:添加3个宏: ZLIB_WINAPI;ASMV;ASMINF

  3. 项目包含.lib文件所在的目录:

    属性–链接器–常规–附加库目录

  4. 项目包含.lib文件

    属性–链接器–输入–附加依赖项(libstat.lib)

使用

#include <stdlib.h>
#include <stdio.h>
#include <zlib.h>int main(int argc, char* argv[])
{FILE* file;uLong flen;unsigned char* fbuf = NULL;uLong clen;unsigned char* cbuf = NULL;/* 通过命令行参数将srcfile文件的数据压缩后存放到dstfile文件中 */if (argc < 3){printf("Usage: zcdemo srcfile dstfile\n");return -1;}if ((file = fopen(argv[1], "rb")) == NULL){printf("Can\'t open %s!\n", argv[1]);return -1;}/* 装载源文件数据到缓冲区 */fseek(file, 0L, SEEK_END); /* 跳到文件末尾 */flen = ftell(file);  /* 获取文件长度 */fseek(file, 0L, SEEK_SET);if ((fbuf = (unsigned char*)malloc(sizeof(unsigned char) * flen)) == NULL){printf("No enough memory!\n");fclose(file);return -1;}fread(fbuf, sizeof(unsigned char), flen, file);/* 压缩数据 */clen = compressBound(flen);if ((cbuf = (unsigned char*)malloc(sizeof(unsigned char) * clen)) == NULL){printf("No enough memory!\n");fclose(file);return -1;}if (compress(cbuf, &clen, fbuf, flen) != Z_OK){printf("Compress %s failed!\n", argv[1]);return -1;}fclose(file);if ((file = fopen(argv[2], "wb")) == NULL){printf("Can\'t create %s!\n", argv[2]);return -1;}/* 保存压缩后的数据到目标文件 */fwrite(&flen, sizeof(uLong), 1, file); /* 写入源文件长度 */fwrite(&clen, sizeof(uLong), 1, file); /* 写入目标数据长度 */fwrite(cbuf, sizeof(unsigned char), clen, file);fclose(file);free(fbuf);free(cbuf);return 0;
}

OPENSSL

要点:安装比较简单

安装:

  1. 到官网下载一个安装包,例如Win32 OpenSSL v1.1.1g.exe
  2. 无脑安装,中间有一步选择lib复制路径,要改一下,改成复制到openssl安装路径

环境:
为方便统一使用,此处我将主要文件集中在一起,记为Envior文件夹

  1. 将openssl安装路径(C:\Program Files (x86)\OpenSSL-Win32)中的include\openssl文件夹整个复制到Envior

  2. 将openssl安装路径中的lib文件夹下的libssl_static.lib及libcrypto_static.lib复制到Envior

  3. 包含头文件

    属性–C/C+±-常规–附加包含目录(添加目录Envior)

  4. 项目包含.lib文件所在的目录:

    属性–链接器–常规–附加库目录(添加目录Envior)

  5. 项目包含.lib文件

    属性–链接器–输入–附加依赖项(libssl_static.lib;libcrypto_static.lib)

使用:

#include <openssl/md5.h>
#include <string.h>
#include <stdio.h>void MD5(char* input,unsigned char* output) //MD5求摘要
{MD5_CTX ctx;memset(output, 0, sizeof(output));MD5_Init(&ctx);MD5_Update(&ctx, input, strlen(input) );MD5_Final(output, &ctx);/*int i = 0;for (i = 0; i < 16; i < i++) //输出{printf("%02X", output[i]);}printf("\n");*/
}#include <openssl/aes.h> //AES-CBC加密
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
unsigned char* str2hex(char* str) {//注释1unsigned char* ret = NULL;int str_len = strlen(str);int i = 0;assert((str_len % 2) == 0);ret = (unsigned char*)malloc(str_len / 2);for (i = 0; i < str_len; i = i + 2) {sscanf(str + i, "%2hhx", &ret[i / 2]);}return ret;
}
unsigned char* padding_buf(unsigned char* buf, int size, int* final_size) {//注释2unsigned char* ret = NULL;int pidding_size = AES_BLOCK_SIZE - (size % AES_BLOCK_SIZE);int i;*final_size = size + pidding_size;ret = (unsigned char*)malloc(size + pidding_size);memcpy(ret, buf, size);if (pidding_size != 0) {for (i = size; i < (size + pidding_size); i++) {ret[i] = 0;}}return ret;
}
void printf_buff(unsigned char* buff, int size) {int i = 0;for (i = 0; i < size; i++) {printf("%02X ", buff[i]);if ((i + 1) % 8 == 0) {printf("\n");}}printf("\n\n\n\n");
}
char s[300];
void encrpyt_buf(unsigned char* raw_buf, unsigned  char** encrpy_buf, int len) {AES_KEY aes;strcpy(s, "8cc72b05705d5c46f412af8cbed55aad");s[32] = '\0';unsigned char* key = str2hex(s);strcpy(s, "667b02a85c61c786def4521b060265e8");unsigned char* iv = str2hex(s);AES_set_encrypt_key(key, 128, &aes);//注释3AES_cbc_encrypt(raw_buf, *encrpy_buf, len, &aes, iv, AES_ENCRYPT);free(key);free(iv);
}
void decrpyt_buf(unsigned char* raw_buf, unsigned char** encrpy_buf, int len) {AES_KEY aes;strcpy(s, "8cc72b05705d5c46f412af8cbed55aad");unsigned char* key = str2hex(s);strcpy(s, "667b02a85c61c786def4521b060265e8");unsigned char* iv = str2hex(s);AES_set_decrypt_key(key, 128, &aes);AES_cbc_encrypt(raw_buf, *encrpy_buf, len, &aes, iv, AES_DECRYPT);free(key);free(iv);
}
int main(int argn, char* argv[]) {unsigned char* raw_buf = NULL;unsigned char* after_padding_buf = NULL;int padding_size = 0;unsigned char* encrypt_buf = NULL;unsigned char* decrypt_buf = NULL;// 1raw_buf = (unsigned char*)malloc(17);memcpy(raw_buf, "life's a struggle", 17);printf("------------------raw_buf\n");printf_buff(raw_buf, 17);// 2after_padding_buf = padding_buf(raw_buf, 17, &padding_size);printf("------------------after_padding_buf\n");printf_buff(after_padding_buf, padding_size);// 3encrypt_buf = (unsigned char*)malloc(padding_size);encrpyt_buf(after_padding_buf, &encrypt_buf, padding_size);printf("------------------encrypt_buf\n");printf_buff(encrypt_buf, padding_size);// 4decrypt_buf = (unsigned char*)malloc(padding_size);decrpyt_buf(encrypt_buf, &decrypt_buf, padding_size);printf("------------------decrypt_buf\n");printf_buff(decrypt_buf, padding_size);free(raw_buf);free(after_padding_buf);free(encrypt_buf);free(decrypt_buf);return 0;
}
  相关解决方案