nht_cntt

MaHoa_GiaiMa

Apr 15th, 2015
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX 1024
  4.  
  5. int _MaHoaFile(const char *sPathFile1, const char *sPathFile2);
  6. int _GiaiMaFile(const char *sPathFile1, const char *sPathFile2);
  7. char* _NhapPass();
  8. void _WritePass(FILE *pFile, const char *Key);// Viet mat khau vao file
  9. char* _ReadPass(FILE *pFile); // Doc mat khau tu file
  10. bool _KiemTraPass(const char *Key1, const char *Key2);
  11. inline void _GiaiMaHoa(char &x);// ham ma hoa va giai ma
  12.  
  13. int main()
  14. {
  15.     if (_MaHoaFile("Create Boot.exe", "Create Boot2.exe"))
  16.     {
  17.         printf("true\n");
  18.     }
  19.     else
  20.         printf("false\n");
  21.  
  22.     return 0;
  23. }
  24.  
  25.  
  26. int _MaHoaFile(const char *sPathFile1, const char *sPathFile2)
  27. {
  28.  
  29.     char Buff[MAX] // Bộ đệm, nơi chứa dữ liệu của file
  30.         , *Key;
  31.     int n;
  32.     FILE *pFile1, *pFile2;
  33.     Key = new char[20]; // Key nhập từ bàn phím
  34.     Key = _NhapPass(); // Nhập mật khẩu
  35.     pFile1 = fopen(sPathFile1, "rb");
  36.     if (!pFile1)
  37.     {
  38.         printf("File khong ton tai.\n");
  39.         fclose(pFile1);
  40.         return 0;
  41.     }
  42.     pFile2 = fopen(sPathFile2, "wb");
  43.     if (!pFile2)
  44.     {
  45.         printf("Khong tao duoc file.\n");
  46.         fclose(pFile2);
  47.         return 0;
  48.     }
  49.     while (!feof(pFile1))
  50.     {
  51.         n = fread(Buff, 1, MAX, pFile1); // Đọc dữ liệu vào bộ đệm Buff
  52.         for(int i = 0; i < n; ++i)
  53.                 _GiaiMaHoa(Buff[i]); // Xử lý dữ liệu
  54.         if (n > 0)
  55.         {
  56.             fwrite(Buff, 1, n, pFile2); // Đưa dữ liệu đã xử lý vào file2
  57.         }
  58.     }
  59.     _WritePass(pFile2, Key); // Ghi mật khẩu vào file2
  60.     fclose(pFile1);
  61.     fclose(pFile2);
  62.     return 1;
  63. }
  64.  
  65.  
  66. int _GiaiMaFile(const char *sPathFile1, const char *sPathFile2)
  67. {
  68.     char Buff[MAX], // Bộ đệm, nơi chứa dữ liệu của file
  69.         *Key, *KeySave;
  70.     int n;
  71.     FILE *pFile1, *pFile2;
  72.     Key = new char[20]; // Key nhập từ bàn phím
  73.     KeySave = new char[20]; // KeySave lấy từ file
  74.     Key = _NhapPass(); // Nhập mật khẩu
  75.  
  76.     pFile1 = fopen(sPathFile1, "rb");
  77.     if (!pFile1)
  78.     {
  79.         printf("File khong ton tai.\n");
  80.         fclose(pFile1);
  81.         return 0;
  82.     }
  83.  
  84.     KeySave = _ReadPass(pFile1); // Lấy KeySave
  85.     while(!_KiemTraPass(Key, KeySave))
  86.     {
  87.         printf("!Sai mat khau. Xin nhap lai.!\n");
  88.         Key = _NhapPass();
  89.     }
  90.  
  91.     pFile2 = fopen(sPathFile2, "wb");
  92.     if (!pFile2)
  93.     {
  94.         printf("Khong tao duoc file.\n");
  95.         fclose(pFile2);
  96.         return 0;
  97.     }
  98.     while (!feof(pFile1))
  99.     {
  100.         n = fread(Buff, 1, MAX, pFile1); // Đọc dữ liệu vào bộ đệm Buff
  101.         for(int i = 0; i < n; ++i)
  102.                 _GiaiMaHoa(Buff[i]); // Xử lý dữ liệu
  103.         if (n > 0)
  104.         {
  105.             fwrite(Buff, 1, n, pFile2); // Đưa dữ liệu đã xử lý vào file2
  106.         }
  107.     }
  108.     fclose(pFile1);
  109.     fclose(pFile2);
  110.     return 1;
  111.  
  112. }
  113.  
  114.  
  115. inline void _GiaiMaHoa(char &x)
  116. {
  117.     x = ~x; // Đảo bit dữ liệu
  118. }
  119.  
  120. char* _NhapPass()
  121. {
  122.     char *Key;
  123.     Key = new char[20];
  124.     printf("Nhap mat khau: ");
  125.     gets(Key);
  126.     return Key;
  127. }
  128.  
  129.  
  130. void _WritePass(FILE *pFile, const char *Key)
  131. {
  132.     fseek(pFile, 0, SEEK_END); // Đưa con trỏ tập tin về cuối tập tin.
  133.     int len = strlen(Key); // Lấy độ dài của chuỗi mật khẩu.
  134.     fwrite(Key, 1, len, pFile); // Viết từng kí tự từ bộ đệm vào file2
  135.     fwrite(&len, sizeof(int), 1, pFile); // Viết độ dài của chuỗi mật khẩu vào file2, để dùng cho việc lấy mật khẩu sau này để so sánh.
  136.     fseek(pFile, 0, SEEK_SET); // Đưa con trỏ tập tin về đầu tập tin.
  137. }
  138.  
  139. char* _ReadPass(FILE *pFile)
  140. {
  141.     int Len; // Dộ dài chuỗi mật khẩu
  142.     char *Key;
  143.     Key = new char[20]; // Khai báo bộ đệm nơi chứa mật khẩu.
  144.     fseek(pFile, -sizeof(int), SEEK_END); //Dời con trỏ tập tin từ cuối file đi ngược lên 4 byte. Để
  145.     fread(&Len, sizeof(int), 1, pFile); // lấy độ dài của chuỗi mật khẩu
  146.     Len += sizeof(int);
  147.     fseek(pFile, -Len, SEEK_END);// Dời con trỏ tập tin từ vị trí hồi nãy đi lên Len byte. Để
  148.     Len -= sizeof(int);
  149.     fread(Key, 1, Len, pFile);// lấy mật khẩu
  150.     Key[Len] = '\0';
  151.     fseek(pFile, 0, SEEK_SET); // Đưa con trỏ về đầu tập tin.
  152.     return Key;
  153.     /*
  154.     Vd:
  155.     ....Nội dung file
  156.     .......[Mật khẩu] [Độ dài mật khẩu] kết thúc file.\
  157.     */
  158. }
  159.  
  160. bool _KiemTraPass(const char *Key1, const char *Key2)
  161. {
  162.     if(!strcmp(Key1, Key2)) return true;
  163.     else return false;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment