📄 crypt.c
字号:
/*************************************************************************** crypt.c - description ------------------- begin : Mon Jul 7 14:16:22 EEST 2003 copyright : (C) 2003 by Petri Turunen email : petri.turunen@pete.fi.eu.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#include <stdio.h>#include <string.h>#include <evp.h> //openssl#include <err.h> //openssl#include <stdarg.h> //va_end#include <fcntl.h>#include <stdio.h>#include <sys/stat.h>#include <sys/types.h>#include "linux_mond.h"extern unsigned char CRYPTIV[]; /* T鋒鋘 encrypt aliohjelman kutsu */// outlen = encrypt_stuff(key, &outbuf, "%s", intext);// outlen = decrypt_stuff(key, &outbuf2, outbuf, outlen); // free(outbuf);// free(outbuf2);int encrypt_stuff(char *key, unsigned char **outbuf, char *fmt, ...){ //unsigned char outbuf[1024]; int outlen, tmplen; unsigned long error; char message[1024]; va_list ap; EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init(&ctx); ERR_load_crypto_strings(); va_start (ap, fmt); vsnprintf(message, 1024, fmt, ap); EVP_EncryptInit(&ctx, EVP_bf_cfb (), key, CRYPTIV); EVP_CIPHER_CTX_set_key_length(&ctx,strlen(key)); outlen = strlen(message); message[outlen]='\0'; if((*outbuf=(unsigned char*)malloc(outlen + EVP_CIPHER_CTX_block_size(&ctx) + 1))==NULL){ slog(1,"crypt.c:encrypt: Out of memory."); //DEBUG return 0; } memset(*outbuf,'0',outlen); if(!EVP_EncryptUpdate(&ctx, *outbuf, &outlen, message, strlen(message))) { error = ERR_get_error(); slog(1,"crypt.c: %s:%s\n",ERR_func_error_string(error),ERR_reason_error_string(error)); return 0; } /* Buffer passed to EVP_EncryptFinal() must be after data just * encrypted to avoid overwriting it. */ if(!EVP_EncryptFinal(&ctx, *outbuf + outlen, &tmplen)) { error = ERR_get_error(); slog(2,"crypt.c: %s:%s\n",ERR_func_error_string(error),ERR_reason_error_string(error)); return 0; } outlen += tmplen; EVP_CIPHER_CTX_cleanup(&ctx); /* Need binary mode for fopen because encrypted data is * binary data. Also cannot use strlen() on it because * it wont be null terminated and may contain embedded * nulls. */ va_end(ap); ERR_free_strings(); return outlen;}int decrypt_stuff(char *key, unsigned char **outbuf, unsigned char *inbuf, int inlen){ int outlen, tmplen; unsigned long error; EVP_CIPHER_CTX ctx2; EVP_CIPHER_CTX_init(&ctx2); ERR_load_crypto_strings(); EVP_DecryptInit(&ctx2, EVP_bf_cfb (), key, CRYPTIV); EVP_CIPHER_CTX_set_key_length(&ctx2,strlen(key)); if((*outbuf=(unsigned char*)malloc(inlen + EVP_CIPHER_CTX_block_size(&ctx2) + 1))==NULL){ slog(2,"crypt.c:decrypt: Out of memory."); //DEBUG return 0; } if(!EVP_DecryptUpdate(&ctx2, *outbuf, &outlen, inbuf, inlen)) { error = ERR_get_error(); slog(1,"crypt.c: %s:%s\n",ERR_func_error_string(error),ERR_reason_error_string(error)); return 0; } tmplen=0; if(!EVP_DecryptFinal(&ctx2, *outbuf+outlen, &tmplen)) { error = ERR_get_error(); slog(1,"crypt.c: %s:%s\n",ERR_func_error_string(error),ERR_reason_error_string(error)); //Fix for bug in openssl for(;;) { if(isalnum(*(*outbuf+outlen))==0) { outlen--; } else { outlen++; *(*outbuf+outlen)='\0'; break; } } return 0; } outlen += tmplen; EVP_CIPHER_CTX_cleanup(&ctx2); ERR_free_strings(); *(*outbuf+outlen)='\0'; return outlen;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -