帮忙看一个加密函数

taft 2004-08-22 03:26:05
void encrypt (
unsigned char* instr,
int instrlen,
unsigned char* key,
unsigned char* outstr,
int* outstrlen_prt)
{
unsigned char
plain[8], // plain text buffer
plain_pre_8[8], // plain text buffer, previous 8 bytes
* crypted, // crypted text
* crypted_pre_8, // crypted test, previous 8 bytes
* inp; // current position in instr
int
pos_in_byte = 1, // loop in the byte
is_header=1, // header is one byte
count=0, // number of bytes being crypted
padding = 0; // number of padding stuff

int rand(void) { // it can be the real random seed function
return 0xdead; } // override with number, convenient for debug

/*** we encrypt every eight byte ***/
void encrypt_every_8_byte (void) {
for(pos_in_byte=0; pos_in_byte<8; pos_in_byte++) {
if(is_header) { plain[pos_in_byte] ^= plain_pre_8[pos_in_byte]; }
else { plain[pos_in_byte] ^= crypted_pre_8[pos_in_byte]; }
} // prepare plain text
encipher( (unsigned long *) plain,
(unsigned long *) key,
(unsigned long *) crypted); // encrypt it

for(pos_in_byte=0; pos_in_byte<8; pos_in_byte++) {
crypted[pos_in_byte] ^= plain_pre_8[pos_in_byte];
}
memcpy(plain_pre_8, plain, 8); // prepare next

crypted_pre_8 = crypted; // store position of previous 8 byte
crypted += 8; // prepare next output
count += 8; // outstrlen increase by 8
pos_in_byte = 0; // back to start
is_header = 0; // and exit header
}// encrypt_every_8_byte

pos_in_byte = (instrlen + 0x0a) % 8; // header padding decided by instrlen
if (pos_in_byte) {
pos_in_byte = 8 - pos_in_byte;
}
plain[0] = (rand() & 0xf8) | pos_in_byte;

memset(plain+1, rand()&0xff, pos_in_byte++);
memset(plain_pre_8, 0x00, sizeof(plain_pre_8));

crypted = crypted_pre_8 = outstr;

padding = 1; // pad some stuff in header
while (padding <= 2) { // at most two byte
if(pos_in_byte < 8) { plain[pos_in_byte++] = rand() & 0xff; padding ++; }
if(pos_in_byte == 8){ encrypt_every_8_byte(); }
}

inp = instr;
while (instrlen > 0) {
if (pos_in_byte < 8) { plain[pos_in_byte++] = *(inp++); instrlen --; }
if (pos_in_byte == 8){ encrypt_every_8_byte(); }
}

padding = 1; // pad some stuff in tailer
while (padding <= 7) { // at most sever byte
if (pos_in_byte < 8) { plain[pos_in_byte++] = 0x00; padding ++; }
if (pos_in_byte == 8){ encrypt_every_8_byte(); }
}

*outstrlen_prt = count;
}

怎么这个加密函数内包含了2个函数定义,我linux下编译成功,但在vc下就是不行。程序的流程是什么?我该怎么在外部特别是实现第2个嵌套的函数encrypt_every_8_byte (),里面的变量太麻烦了。
...全文
184 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
taft 2004-08-22
  • 打赏
  • 举报
回复
谢谢 pomelowu(羽战士) 大哥, 问题快解决了,我又加深了指针的应用
pomelowu 2004-08-22
  • 打赏
  • 举报
回复
encipher是定义在其他地方的吧?不需要做什么处理,关键是把数组plain和指针key、crypted当作encrypt_every_8_byte的参数传进去。
encrypt_every_8_byte(unsigned char plain[], unsigned char * key, unsigned char * crypted)

上面只是一个例子,你看还需不需要传其他参数进去,修改一下参数表吧。
taft 2004-08-22
  • 打赏
  • 举报
回复
to pomelowu

encipher( (unsigned long *) plain,
(unsigned long *) key,
(unsigned long *) crypted);

这个我不知道怎么处理
pomelowu 2004-08-22
  • 打赏
  • 举报
回复
当然,其中的参数表你需要修改一下,看哪些数值是需要输入输出的,当作encrypt_every_8_byte ()的参数,不需要输入输出的局部变量就在encrypt_every_8_byte ()中重新声明一下。
pomelowu 2004-08-22
  • 打赏
  • 举报
回复
把嵌套函数声明放到函数体外面。

int rand(void) { // it can be the real random seed function
return 0xdead; } // override with number, convenient for debug

/*** we encrypt every eight byte ***/
void encrypt_every_8_byte (void) {
for(pos_in_byte=0; pos_in_byte<8; pos_in_byte++) {
if(is_header) { plain[pos_in_byte] ^= plain_pre_8[pos_in_byte]; }
else { plain[pos_in_byte] ^= crypted_pre_8[pos_in_byte]; }
} // prepare plain text
encipher( (unsigned long *) plain,
(unsigned long *) key,
(unsigned long *) crypted); // encrypt it

for(pos_in_byte=0; pos_in_byte<8; pos_in_byte++) {
crypted[pos_in_byte] ^= plain_pre_8[pos_in_byte];
}
memcpy(plain_pre_8, plain, 8); // prepare next

crypted_pre_8 = crypted; // store position of previous 8 byte
crypted += 8; // prepare next output
count += 8; // outstrlen increase by 8
pos_in_byte = 0; // back to start
is_header = 0; // and exit header
}// encrypt_every_8_byte

void encrypt (
unsigned char* instr,
int instrlen,
unsigned char* key,
unsigned char* outstr,
int* outstrlen_prt)
{
unsigned char
plain[8], // plain text buffer
plain_pre_8[8], // plain text buffer, previous 8 bytes
* crypted, // crypted text
* crypted_pre_8, // crypted test, previous 8 bytes
* inp; // current position in instr
int
pos_in_byte = 1, // loop in the byte
is_header=1, // header is one byte
count=0, // number of bytes being crypted
padding = 0; // number of padding stuff

pos_in_byte = (instrlen + 0x0a) % 8; // header padding decided by instrlen
if (pos_in_byte) {
pos_in_byte = 8 - pos_in_byte;
}
plain[0] = (rand() & 0xf8) | pos_in_byte;

memset(plain+1, rand()&0xff, pos_in_byte++);
memset(plain_pre_8, 0x00, sizeof(plain_pre_8));

crypted = crypted_pre_8 = outstr;

padding = 1; // pad some stuff in header
while (padding <= 2) { // at most two byte
if(pos_in_byte < 8) { plain[pos_in_byte++] = rand() & 0xff; padding ++; }
if(pos_in_byte == 8){ encrypt_every_8_byte(); }
}

inp = instr;
while (instrlen > 0) {
if (pos_in_byte < 8) { plain[pos_in_byte++] = *(inp++); instrlen --; }
if (pos_in_byte == 8){ encrypt_every_8_byte(); }
}

padding = 1; // pad some stuff in tailer
while (padding <= 7) { // at most sever byte
if (pos_in_byte < 8) { plain[pos_in_byte++] = 0x00; padding ++; }
if (pos_in_byte == 8){ encrypt_every_8_byte(); }
}

*outstrlen_prt = count;
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧