📄 crypto.c
字号:
/* crypto.c -- Crypto interface for Intercom Copyright (C) 2001-2003 Shane Wegner This file is part of Intercom. Intercom is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. Intercom is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of version 2 of the GNU General Public License along with Intercom; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA To contact the author, please send email to shane@cm.nu. *//* $Id: crypto.c,v 1.8 2003/02/13 20:22:49 shane Exp $ */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <stdlib.h>#include <string.h>#include <openssl/des.h>#include <openssl/blowfish.h>#include <openssl/md5.h>#include <assert.h>#include "crypto.h"#include "protocol.h"#include "intercom.h"#ifdef CRYPTO_OLD_DES#define DES_key_schedule des_key_schedule#define DES_cblock des_cblock#define DES_string_to_key des_string_to_key#define DES_set_key des_set_key#define DES_ncbc_encrypt des_ncbc_encryptstatic DES_key_schedule des_sched;#elsestatic DES_key_schedule *des_sched;#endifint cr_type_fromstr(const char *s){if (!strcasecmp(s, "des"))return CALL_CRYPT_DES;else if (!strcasecmp(s, "blowfish"))return CALL_CRYPT_BLOWFISH;elsereturn -1;}static void cr_des_set_key(const char *passphrase){DES_cblock des_key;DES_string_to_key(passphrase, &des_key);DES_set_key(&des_key, des_sched);}static void cr_des_encrypt(const char *in, size_t inl, char *out, size_t *outl){size_t l;DES_cblock ivec;memset(ivec, 0, 8);l = (inl + 7) & (~7);DES_ncbc_encrypt(in, out, l, des_sched, &ivec, DES_ENCRYPT);*outl = l;}static void cr_des_decrypt(const char *in, size_t inl, char *out, size_t *outl){DES_cblock ivec;memset(ivec, 0, 8);assert(((inl + 7) & (~7)) == inl);DES_ncbc_encrypt(in, out, inl, des_sched, &ivec, DES_DECRYPT);*outl = inl;}static BF_KEY bf_key;static void cr_bf_set_key(const char *passphrase){char md5[16];MD5(passphrase, strlen(passphrase), md5);BF_set_key(&bf_key, 16, md5);}static void cr_bf_encrypt(const char *in, size_t inl, char *out, size_t *outl){size_t l;char ivec[8];memset(ivec, 0, 8);l = (inl + 7) & (~7);BF_cbc_encrypt(in, out, l, &bf_key, ivec, BF_ENCRYPT);*outl = l;}static void cr_bf_decrypt(const char *in, size_t inl, char *out, size_t *outl){char ivec[8];memset(ivec, 0, 8);assert(((inl + 7) & (~7)) == inl);BF_cbc_encrypt(in, out, inl, &bf_key, ivec, BF_DECRYPT);*outl = inl;}void cr_set_key(const char *passphrase){switch(call.cryptinfo.type) {case CALL_CRYPT_DES:cr_des_set_key(passphrase);break;case CALL_CRYPT_BLOWFISH:cr_bf_set_key(passphrase);break;default:assert(0);}}void cr_encrypt(const char *in, size_t inl, char *out, size_t *outl){switch(call.cryptinfo.type) {case CALL_CRYPT_DES:cr_des_encrypt(in, inl, out, outl);break;case CALL_CRYPT_BLOWFISH:cr_bf_encrypt(in, inl, out, outl);break;default:assert(0);}}void cr_decrypt(const char *in, size_t inl, char *out, size_t *outl){switch(call.cryptinfo.type) {case CALL_CRYPT_DES:cr_des_decrypt(in, inl, out, outl);break;case CALL_CRYPT_BLOWFISH:cr_bf_decrypt(in, inl, out, outl);break;default:assert(0);}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -