分类: LINUX
2008-12-17 16:05:10
先man encrypt,看一看:
#define _XOPEN_SOURCE
#include
void
setkey(const char *key);
#define _XOPEN_SOURCE
#include
void
encrypt(char block[64], int edflag);
These functions encrypt and decrypt 64-bit messages. The setkey()
function sets the key used by encrypt(). The key parameter used here
is an array of 64 bytes, each of which has numerical value 1 or 0. The
bytes key[n] where n=8*i-1 are ignored, so that the effective key
length is 56 bits.
The encrypt() function modifies the passed buffer, encoding if edflag
is 0, and decoding if 1 is being passed. Like the key parameter also
block is a bit vector representation of the actual value that is
encoded. The result is returned in that same vector.
可以看出,这里涉及到了两个函数setkey和encrypt。其中,setkey用来设置密钥,而encrypt当然就是来加密(edflag==0)或者解密(edflag==1)的了。但是这里有一个需要注意的地方,也就是上面用红色标出的地方,setkey的参数key和encrypt的参数block都是64位的位字符,也就是说这两个参数是一个长度为64的字符串,而字符串中每一个字符的内容不能为别的,只能为'0'或者'1'。
比如要加密a这个字符,我们知道a的ASCALL码为0x97,二进制的表示形式为10010111。那么,我们首先得把a转化成一个字符串char * asc="10010111",然后再把asc当作参数传入encrypt就行。
#define _XOPEN_SOURCE
#include
#include
#include
#include
void stringtobyte(const char *sptr,int slen,char *bptr)
int i,j,op,tmp;
if(tmp == 0)
bptr[i*8+j] = 0x0;
bptr[i*8+j] = 0x1;
void bytetostring(const char* bptr,int blen,char* sptr)
int j,op;
if (j%8 == 7)
char* pad(char* src,int slen)
char* dest;
int padlen=0,i;
if(slen%8 != 0)
padlen = 8-slen%8;
dest = (char*)malloc((slen+padlen+1)*sizeof(char));
return dest;
int unpad(char *string, int slen)
int tlen=slen;
else
return tlen;
void DEScrypt(char* string,int slen,const char* key,int flag)
char* ptr,*buffer,bytekey[64];
int i,buflen;
int main(void)
char key[8];
char txt[100];
int i,len,ulen;
char *string;
key[i] = 'a';
key[i] = 'b';
txt[i] = 'A';
txt[i] = 'B';
DEScrypt(string,len,key,0);
DEScrypt(string,len,key,1);
xulei@xulei-desktop:~$ ./crypt
ABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBABBA$$$$