/*IDEA Algorithm functions */
void en_key_idea(word16 userkey[8],IDEAkey Z);
void de_key_idea(IDEAkey Z, IDEAkey DK);
void cipher_idea(word16 in[4],word16 out[4],IDEAkey Z);
uint16 inv(uint16 x);
uint16 mul(uint16 a,uint16 b);
/*file handling functions*/
char read_char_from_file(FILE *fp);
word16 read_word16_from_file(FILE *fp);
void write_char_to_file(char data,FILE *fp);
void write_word16_to_file(word16 data,FILE *fp);
void cipher_file(FILE *in,FILE *out,word16 *key);
void decipher_file(FILE *in,FILE *out,word16 *key);
void swap_files_and_clean_up(char *file);
void GetChoice(int &choice , char szFileName[100] ,char szKey[50]);
#endif
3.3 cpp文件部分
/*idea.cpp*/
#include "idea.h"
/*实现把加密密钥转换为解密密钥, 加密密钥与解密密钥的模216+1乘 等于 1 MOD(216+1)*/
static uint16 inv(uint16 x)
{
uint16 t0,t1;
uint16 q,y;
if (x<=1)
return x;
t1=(uint16)(0x10001l/x);
y=(uint16)(0x10001l%x);
if (y==1)
return low16(1-t1);
t0=1;
do
{
q=x/y;
x=x%y; t0+=q*t1;
|