LINUX.ORG.RU

помогите переписать с des на aes


0

0

class crypt_handler : public flow_handler {
    des_cblock key;

    des_key_schedule in1_sched;
    des_key_schedule in2_sched;
    des_key_schedule in3_sched;
    des_cblock in_ivec;
    int in_num;
    des_key_schedule out1_sched;
    des_key_schedule out2_sched;
    des_key_schedule out3_sched;
    des_cblock out_ivec;
    int out_num;

public:
    crypt_handler(init_context& ctxt) : flow_handler(ctxt) {
	memset(&key, 0, sizeof key);
	des_set_odd_parity(&key);
	memcpy(&in_ivec, &key, sizeof in_ivec);
	memcpy(&out_ivec, &key, sizeof out_ivec);
	des_set_key(&in_ivec, in1_sched);
	des_set_key(&in_ivec, in2_sched);
	des_set_key(&in_ivec, in3_sched);
	des_set_key(&out_ivec, out1_sched);
	des_set_key(&out_ivec, out2_sched);
	des_set_key(&out_ivec, out3_sched);

	in_num = out_num = 0;
    }

    virtual bool avail(flow_handler *from, data d) {
	static char tmpbuf[65536*16];
	assert(d.length() <= sizeof tmpbuf);

	des_ede3_cfb64_encrypt((unsigned char *)d.bits(), (unsigned char *)tmpbuf, d.length(),
			       in1_sched, in2_sched, in3_sched, &in_ivec, &in_num, DES_DECRYPT);

	return get_upstream().avail(this, data(tmpbuf, d.length()));
    }

    virtual int write(data d) {
	static char tmpbuf[65536*16];
	assert(d.length() <= sizeof tmpbuf);

	des_ede3_cfb64_encrypt((unsigned char *)d.bits(), (unsigned char *)tmpbuf, d.length(),
			       out1_sched, out2_sched, out3_sched, &out_ivec, &out_num, DES_ENCRYPT);

	return downstream[0]->write(data(tmpbuf, d.length()));
    }

    DECLARE_HANDLER;
};
anonymous

class crypt_handler : public flow_handler {
    aes_cblock key;

    aes_key_schedule in1_sched;
    aes_key_schedule in2_sched;
    aes_key_schedule in3_sched;
    aes_cblock in_ivec;
    int in_num;
    aes_key_schedule out1_sched;
    aes_key_schedule out2_sched;
    aes_key_schedule out3_sched;
    aes_cblock out_ivec;
    int out_num;

public:
    crypt_handler(init_context& ctxt) : flow_handler(ctxt) {
	memset(&key, 0, sizeof key);
	aes_set_odd_parity(&key);
	memcpy(&in_ivec, &key, sizeof in_ivec);
	memcpy(&out_ivec, &key, sizeof out_ivec);
	aes_set_key(&in_ivec, in1_sched);
	aes_set_key(&in_ivec, in2_sched);
	aes_set_key(&in_ivec, in3_sched);
	aes_set_key(&out_ivec, out1_sched);
	aes_set_key(&out_ivec, out2_sched);
	aes_set_key(&out_ivec, out3_sched);

	in_num = out_num = 0;
    }

    virtual bool avail(flow_handler *from, data d) {
	static char tmpbuf[65536*16];
	assert(d.length() <= sizeof tmpbuf);

	aes_ede3_cfb256_encrypt((unsigned char *)d.bits(), (unsigned char *)tmpbuf, d.length(),
			       in1_sched, in2_sched, in3_sched, &in_ivec, &in_num, aes_DECRYPT);

	return get_upstream().avail(this, data(tmpbuf, d.length()));
    }

    virtual int write(data d) {
	static char tmpbuf[65536*16];
	assert(d.length() <= sizeof tmpbuf);

	aes_ede3_cfb256_encrypt((unsigned char *)d.bits(), (unsigned char *)tmpbuf, d.length(),
			       out1_sched, out2_sched, out3_sched, &out_ivec, &out_num, aes_ENCRYPT);

	return downstream[0]->write(data(tmpbuf, d.length()));
    }

    DECLARE_HANDLER;
};


P.S. какой вопрос, такой и ответ ;)

fmj
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.