Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define MAX 1024
- int _MaHoaFile(const char *sPathFile1, const char *sPathFile2);
- int _GiaiMaFile(const char *sPathFile1, const char *sPathFile2);
- char* _NhapPass();
- void _WritePass(FILE *pFile, const char *Key);// Viet mat khau vao file
- char* _ReadPass(FILE *pFile); // Doc mat khau tu file
- bool _KiemTraPass(const char *Key1, const char *Key2);
- inline void _GiaiMaHoa(char &x);// ham ma hoa va giai ma
- int main()
- {
- if (_MaHoaFile("Create Boot.exe", "Create Boot2.exe"))
- {
- printf("true\n");
- }
- else
- printf("false\n");
- return 0;
- }
- int _MaHoaFile(const char *sPathFile1, const char *sPathFile2)
- {
- char Buff[MAX] // Bộ đệm, nơi chứa dữ liệu của file
- , *Key;
- int n;
- FILE *pFile1, *pFile2;
- Key = new char[20]; // Key nhập từ bàn phím
- Key = _NhapPass(); // Nhập mật khẩu
- pFile1 = fopen(sPathFile1, "rb");
- if (!pFile1)
- {
- printf("File khong ton tai.\n");
- fclose(pFile1);
- return 0;
- }
- pFile2 = fopen(sPathFile2, "wb");
- if (!pFile2)
- {
- printf("Khong tao duoc file.\n");
- fclose(pFile2);
- return 0;
- }
- while (!feof(pFile1))
- {
- n = fread(Buff, 1, MAX, pFile1); // Đọc dữ liệu vào bộ đệm Buff
- for(int i = 0; i < n; ++i)
- _GiaiMaHoa(Buff[i]); // Xử lý dữ liệu
- if (n > 0)
- {
- fwrite(Buff, 1, n, pFile2); // Đưa dữ liệu đã xử lý vào file2
- }
- }
- _WritePass(pFile2, Key); // Ghi mật khẩu vào file2
- fclose(pFile1);
- fclose(pFile2);
- return 1;
- }
- int _GiaiMaFile(const char *sPathFile1, const char *sPathFile2)
- {
- char Buff[MAX], // Bộ đệm, nơi chứa dữ liệu của file
- *Key, *KeySave;
- int n;
- FILE *pFile1, *pFile2;
- Key = new char[20]; // Key nhập từ bàn phím
- KeySave = new char[20]; // KeySave lấy từ file
- Key = _NhapPass(); // Nhập mật khẩu
- pFile1 = fopen(sPathFile1, "rb");
- if (!pFile1)
- {
- printf("File khong ton tai.\n");
- fclose(pFile1);
- return 0;
- }
- KeySave = _ReadPass(pFile1); // Lấy KeySave
- while(!_KiemTraPass(Key, KeySave))
- {
- printf("!Sai mat khau. Xin nhap lai.!\n");
- Key = _NhapPass();
- }
- pFile2 = fopen(sPathFile2, "wb");
- if (!pFile2)
- {
- printf("Khong tao duoc file.\n");
- fclose(pFile2);
- return 0;
- }
- while (!feof(pFile1))
- {
- n = fread(Buff, 1, MAX, pFile1); // Đọc dữ liệu vào bộ đệm Buff
- for(int i = 0; i < n; ++i)
- _GiaiMaHoa(Buff[i]); // Xử lý dữ liệu
- if (n > 0)
- {
- fwrite(Buff, 1, n, pFile2); // Đưa dữ liệu đã xử lý vào file2
- }
- }
- fclose(pFile1);
- fclose(pFile2);
- return 1;
- }
- inline void _GiaiMaHoa(char &x)
- {
- x = ~x; // Đảo bit dữ liệu
- }
- char* _NhapPass()
- {
- char *Key;
- Key = new char[20];
- printf("Nhap mat khau: ");
- gets(Key);
- return Key;
- }
- void _WritePass(FILE *pFile, const char *Key)
- {
- fseek(pFile, 0, SEEK_END); // Đưa con trỏ tập tin về cuối tập tin.
- int len = strlen(Key); // Lấy độ dài của chuỗi mật khẩu.
- fwrite(Key, 1, len, pFile); // Viết từng kí tự từ bộ đệm vào file2
- 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.
- fseek(pFile, 0, SEEK_SET); // Đưa con trỏ tập tin về đầu tập tin.
- }
- char* _ReadPass(FILE *pFile)
- {
- int Len; // Dộ dài chuỗi mật khẩu
- char *Key;
- Key = new char[20]; // Khai báo bộ đệm nơi chứa mật khẩu.
- fseek(pFile, -sizeof(int), SEEK_END); //Dời con trỏ tập tin từ cuối file đi ngược lên 4 byte. Để
- fread(&Len, sizeof(int), 1, pFile); // lấy độ dài của chuỗi mật khẩu
- Len += sizeof(int);
- fseek(pFile, -Len, SEEK_END);// Dời con trỏ tập tin từ vị trí hồi nãy đi lên Len byte. Để
- Len -= sizeof(int);
- fread(Key, 1, Len, pFile);// lấy mật khẩu
- Key[Len] = '\0';
- fseek(pFile, 0, SEEK_SET); // Đưa con trỏ về đầu tập tin.
- return Key;
- /*
- Vd:
- ....Nội dung file
- .......[Mật khẩu] [Độ dài mật khẩu] kết thúc file.\
- */
- }
- bool _KiemTraPass(const char *Key1, const char *Key2)
- {
- if(!strcmp(Key1, Key2)) return true;
- else return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment