续上述博客-RC4算法移植与源码
前文中,从OpenSSL库中分离了RC4算法,用起来也十分简明清晰,但是,每次都要声明Key,Set_key,还是有些麻烦,于是,我在前文算法基础上,对RC4算法再封装一层,用一个函数完成秘钥传递和加解密过程。
修改rc4.h文件在rc4.h中,增加函数声明:
int RC4Free(unsigned char* InBuf, unsigned long InLen, unsigned char** OutBuf, unsigned long* OutLen, unsigned char* Key, unsigned long KeyLen);修改rc4_enc.c文件
在rc4_enc.c文件末尾,增加函数:
int RC4Free(unsigned char* InBuf, unsigned long InLen, unsigned char** OutBuf, unsigned long* OutLen, unsigned char* Key, unsigned long KeyLen) { RC4_KEY Rc4Key; RC4_set_key(&Rc4Key, KeyLen, Key); *OutBuf = (unsigned char*)malloc(InLen); *OutLen = InLen; RC4(&Rc4Key, InLen, InBuf, *OutBuf); return 0; }修改rc4_test.cpp文件
修改main函数内容如下:
int OriDataLen = 64; const char* Key = "12345678"; unsigned char* OriData = (unsigned char*)malloc(OriDataLen); for (int i = 0; i < OriDataLen; i++) { OriData[i] = i; } unsigned char* EncryBuf = nullptr; unsigned long EncryLen = 0; RC4Free(OriData, OriDataLen, &EncryBuf, &EncryLen, (unsigned char*)Key, strlen(Key)); unsigned char* DecryBuf = nullptr; unsigned long DecryLen = 0; RC4Free(EncryBuf, EncryLen, &DecryBuf, &DecryLen, (unsigned char*)Key, strlen(Key)); if (EncryBuf) { free(EncryBuf); } if (DecryBuf) { free(DecryBuf); }编译运行
观察OriData、EncryBuf、DecryBuf的内存数据。
欢迎大家关注、留言讨论、可分享源码
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)