歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> Linux程式設計入門-crypt

Linux程式設計入門-crypt

日期:2017/2/27 14:28:00   编辑:更多Linux
  crypt是個密碼加密函數,它是基於Data Encryption Standard(DES)演算法。   crypt基本上是One way encryption,因此它只適用於密碼的使用,不適合於資   料加密。     char *crypt(const char *key, const char *salt);     key是使用者的密碼。salt是兩個字,每個字可從[a-zA-Z0-9./]中選出來,因   此同一密碼增加了4096種可能性。透過使用key中每個字的低七位元,取得   56-bit關鍵字,這56-bit關鍵字被用來加密成一組字,這組字有13個可顯示的   ASCII字,包含開頭兩個salt。   crypt在您有自行管理使用者的場合時使用,例如會員網站、BBS等等。       范例一: crypt_Word.c     #include   #include   #include     void main(int argc,char **argv)   {   if (argc!=3) exit(0);   printf("%s\\n",crypt(argv[1],argv[2]));   }     編譯     gcc -o crypt_word crypt.c -lcrypt     檢驗     請先看您的/etc/passwd,找一個您自己的帳號,看前面兩個字,那是您自己的   salt。接下來輸入:     ./crypt_word your_password salt     看看它們是否相同(應該要相同,除非您加了crypt plugin或使用不同的crypt   function,例如shadow、pam,這種狀況下,加密字是不同的),另外檢驗看看   他們是否為13個字。     您也可以利用Apache上所附的htpasswd來產生加密字做為驗證。     范例二: verify_passwd.c     注意,這個范例讀取/etc/passwd的資料,不適用於使用shadow或已經使用pam   的系統(例如slackware,RedHat及Debian在不外加crypt plugin的狀況下,應   當相同)。此范例僅供三考,做為了解crypt函數運作的情形,真正撰寫程式   時,應該避免類似的寫法。     #include   #include   #include     typedef strUCt {   char username[64];   char passwd[16];   int uid;   int gid;   char name[256];   char root[256];   char shell[256];   } account;     /* 注意! 以下的寫法,真實世界的軟體開發狀況下,千萬不要用! */   int acc_info(char *info,account *user)   {   char * start = info;   char * now = info;     /* username */   while (*now&&*now!=\':\') now++; /* 這是超級安全大漏洞 */   if (!*now) return 0;   *now = 0; now++;   strcpy(user->username,start); /* 這會導致buffer overflow */   start = now;     /* passwd */   while (*now&&*now!=\':\') now++; /* 這是超級大安全漏洞 */   if (!*now) return 0;   *now = 0; now++;   strcpy(user->passwd,start); /* 這會導致buffer overflow */   start = now;     /* uid */   while (*now&&*now!=\':\') now++;   if (!*now) return 0;   *now = 0; now++;   user->uid = atoi(start);   start = now;     /* gid */   while (*now&&*now!=\':\') now++;   if (!*now) return 0;   *now = 0; now++;   user->gid = atoi(start);   start = now;     /* name */   while (*now&&*now!=\':\') now++; /* 這是超級安大全漏洞 */   if (!*now) return 0;   *now = 0; now++;   strcpy(user->name,start); /* 這會導致buffer overflow */   start = now;     /* root */   while (*now&&*now!=\':\') now++; /* 這是超級大安全漏洞 */   if (!*now) return 0;   *now = 0; now++;   strcpy(user->root,start); /* 這會導致buffer overflow */   start = now;     /* shell */   while (*now&&*now!=\':\') now++; /* 這是超級大安全漏洞 */   *now = 0; now++;   strcpy(user->shell,start); /* 這會導致buffer overflow */   start = now;   return 1;   }     int read_password(char *filename,account *users)   {   FILE *fp;   char buf[1024];   int n;     n = 0;   fp = fopen(filename,"rt");   while (fgets(buf,1024,fp)!=NULL) {   if (acc_info(buf,&users[n])) n++;   }   fclose(fp);   return n;   }     void main(int argc,char **argv)   {   int n,i,done;   account ACC[128];   char username[256];   char password[256];   char * passwd;   char salt[4];     if (argc



Copyright © Linux教程網 All Rights Reserved