⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fes.h

📁 一款文件加密的小软件
💻 H
📖 第 1 页 / 共 3 页
字号:
            }
        }
        done = findnext(&fileInfo);
    }
    return done;
}

/**
 *  int ViewFiles(FILEATTRIB *fnAttrib, int fileNums);
 *  - 列出当前页文件,并返回选择值
 *  - 如果取消选择则返回-1,否则返回所选项的id值
 *  - 本函数中使用全局变量
 *    - message(type: WINDOWHANDLE)
 *    - fileList(type: WINDOWHANDLE)
 *    - key(type: KEYINFO)
 *    - pathSrc(type: char[])
 **/

int
ViewFiles(FILEATTRIB *fnAttrib, int fileNums)
{
    int i, j;
    WORD extScanKey;
    char disStr[16];

    if (ENCRYPT == key.flag)
    {
        strcpy(fileList.title, "[ENCRYPT: files list]");
    }
    else if (DECRYPT == key.flag)
    {
        strcpy(fileList.title, "[DECRYPT: files list]");
    }

    if (FALSE == Window(&fileList))
    {
        FreeWindow(&fileList);
        return -1;
    }

    /* 显示当前路径名 */
    textbackground(fileList.fillColor);
    textcolor(fileList.fontColor);
    gotoxy(fileList.left + 1, fileList.top + 1);
    fnsplit(pathSrc, drive, dir, file, ext);
    cputs(drive);
    cputs(dir);
    cputs(file);
    cputs(tmpext);

    /* 打印分隔行 */
    gotoxy(fileList.left + 1, fileList.top + 2);
    for (i=0; i<MAXDIR; i++)
    {
        putch('=');
    }

    /* 显示当前页的所有文件项 */
    textbackground(fileList.fillColor);
    textcolor(fileList.fontColor);
    for (i=0; i<fileNums; i++)
    {
        gotoxy(fileList.left + (i%4)*16 +2,
               fileList.top  +  i/4     +3);
        strcpy(disStr, fnAttrib[i].fnStr);
        if (ISDIR == fnAttrib[i].flag)
        {
            strcat(disStr, "\\");
        }
        cputs(disStr);
    }

    /* 刚开始时高亮显示第一条 */
    textbackground(fileList.fontColor);
    textcolor(fileList.fillColor);
    gotoxy(fileList.left + 2,
           fileList.top + 3);
    strcpy(disStr, fnAttrib[0].fnStr);
    if (ISDIR == fnAttrib[0].flag)
    {
        strcat(disStr, "\\");
    }
    cputs(disStr);
    gotoxy(fileList.left + 2,
           fileList.top + 3);

    i = j = 0;
    while (1)                               /* 选择某个文件 */
    {
        extScanKey = GetKey();

        textbackground(fileList.fillColor); /* 去掉当前项的高亮显示 */
        textcolor(fileList.fontColor);
        gotoxy(fileList.left + i*16 + 2,
               fileList.top  + j + 3);
        strcpy(disStr, fnAttrib[j*4+i].fnStr);
        if (ISDIR == fnAttrib[j*4+i].flag)
        {
            strcat(disStr, "\\");
        }
        cputs(disStr);
        gotoxy(fileList.left + i*16 + 2,
               fileList.top  + j + 3);

        switch (extScanKey)                 /* 按键判断 */
            {
        case KEY_UP:    j--;
                        if (j <= -1) j++;
                        break;
        case KEY_DOWN:  j++;
                        if (j >= 8 || (j*4+i) >= fileNums) j--;
                        break;
        case KEY_LEFT:  i--;
                        if (i <= -1) i++;
                        break;
        case KEY_RIGHT: i++;
                        if (i >= 4 || (j*4+i) >= fileNums) i--;
                        break;
        case KEY_ENTER: FreeWindow(&fileList);
                        return (j*4+i);
                        break;
        case KEY_ESC:   FreeWindow(&fileList);
                        return -1;
                        break;
        default:        break;
            }

        textbackground(fileList.fontColor); /* 高亮显示当前项 */
        textcolor(fileList.fillColor);
        gotoxy(fileList.left + i*16 + 2,
               fileList.top  + j + 3);
        strcpy(disStr, fnAttrib[j*4+i].fnStr);
        if (ISDIR == fnAttrib[j*4+i].flag)
        {
            strcat(disStr, "\\");
        }
        cputs(disStr);
        gotoxy(fileList.left + i*16 + 2,
               fileList.top  + j + 3);
    }
}


/**
 *  BOOL GetFileHandle(FILEHANDLE *srcFileHandle,
 *                     FILEHANDLE *dstFileHandle);
 *  - 读入源文件和创建目标文件,返回它们的文件句柄号
 *  - 成功返回TRUE,同时返回文件句柄号;反之返回FALSE
 *  - 本函数中使用全局变量
 *    - message(type: WINDOWHANDLE)
 *    - key(type: KEYINFO)
 *    - pathSrc(type: char[])
 *    - pathDst(type: char[])
 *    - drive(type: char[])
 *    - dir(type: char[])
 *    - file(type: char[])
 *    - ext(type: char[])
 *    - srcFileInfo(type: struct ffblk)
 *    - dstFileInfo(type: struct ffblk)
 *    - fnChosen(type: char[])
 *    - filetime(type: struct ftime)
 **/

BOOL
GetFileHandle(FILEHANDLE *srcFileHandle,
              FILEHANDLE *dstFileHandle)
{
    char ch;
    long fileSize;
    char fileMark[4];
    BYTE checkCode55[16],
         checkCodeAA[16];
    int  i;

    /* 加解密确认 */
    if (ENCRYPT == key.flag)
    {
        strcpy(message.displayStr, "[!]\n\n Sure to ENCRYPT this file:");
    }
    else if (DECRYPT == key.flag)
    {
        strcpy(message.displayStr, "[!]\n\n Sure to DECRYPT this file:");
    }
    strcat(message.displayStr, fnChosen);
    strcat(message.displayStr, "?(y/n) \n");
    Message(&message);

    while (1)
    {
        ch = getch();
        if ('y' == ch || 'Y' == ch)
        {
            FreeMessage(&message);
            break;
        }
        else if ('n' == ch || 'N' == ch)
        {
            FreeMessage(&message);
            return FALSE;
        }
    }

    /* 合并出源文件全路径名(源文件可能是明
     * 文文件也可能是密文文件)           */
    fnsplit(pathSrc, drive, dir, file, ext);
    strcpy (pathSrc, drive);
    strcat (pathSrc, dir);
    strcat (pathSrc, fnChosen);


    /* 获取源文件相关属性。由于源文件是确定存在后才
     * 执行到这一步,因此此处对函数findfirst(...)
     * 返回结果不加判断,即返回肯定为成功        */
    findfirst(pathSrc, &srcFileInfo, ALLFILES);

    /* 修改文件读写属性为可读可写 */
    if (-1 == chmod(pathSrc, S_IREAD|S_IWRITE))
    {
        strcpy(message.displayStr, "[!]\n\n Unable to change r/w mode \n");
        Message(&message);
        getch();
        FreeMessage(&message);
        return FALSE;
    }

    /* 然后将源文件以可读可写方式打开(获取文件handle号) */
    if (-1 ==
        (*srcFileHandle = open(pathSrc, O_RDWR|O_BINARY)))
    {
        _chmod(pathSrc, 1, srcFileInfo.ff_attrib);
        strcpy(message.displayStr, "[!]\n\n Unable to get the source file handle \n");
        Message(&message);
        getch();
        FreeMessage(&message);
        return FALSE;
    }

    if (ENCRYPT == key.flag)
    {
        /* 根据源文件名生成目标文件名,
         * 后缀名更改为.sea(或.aes) */
        strcpy (pathDst, pathSrc);
        fnsplit(pathDst, drive, dir, file, ext);
        /* 如果被加密文件后缀名是.sea
         * 则目标文件的后缀名为.aes      */
        ToLower(ext);
        if (0 == strcmp(ext, ".sea"))
        {
            strcpy(ext, ".aes");
        }
        else
        {
            strcpy(ext, ".sea");
        }
        fnmerge(pathDst, drive, dir, file, ext);

        /* 检验当前目录下是否已存在目标文件 */
        if (TRUE == CheckExist(pathDst))
        {
            _close(*srcFileHandle);
            /* 在退出之前,恢复源文件的文件属性(下同) */
            _chmod(pathSrc, 1, srcFileInfo.ff_attrib);
            /* 从安全角度考虑,系统不会覆盖已存盘的同名文件 */
            strcpy(message.displayStr, "[!]\n\n For safety, System WON'T OVERWRITE the EXISTED file: ");
            strcat(message.displayStr, file);
            strcat(message.displayStr, ext);
            strcat(message.displayStr, " \n");
            Message(&message);
            getch();
            FreeMessage(&message);
            return FALSE;
        }

        /* 如果不存在目标文件则创建新文件并以只写方式打开 */
        if (-1 ==
            (*dstFileHandle = creat(pathDst, S_IWRITE)))
        {
            _close(*srcFileHandle);
            _chmod(pathSrc, 1, srcFileInfo.ff_attrib);
            strcpy(message.displayStr, "[!]\n\n  Unable to build destination file  \n");
            Message(&message);
            getch();
            FreeMessage(&message);
            return FALSE;
        }

        /* 在密文文件头上添加密文标记"SEA" */
        strcpy(fileMark, "SEA");
        _write(*dstFileHandle, fileMark, 4);

        /* 添加密码校验码(两个16字节) */
        for (i=0; i<16; i++)
        {
            checkCode55[i] = 0x55;
            checkCodeAA[i] = 0xaa;
        }
        SEA(checkCode55, &key);
        _write(*dstFileHandle, checkCode55, 16);
        SEA(checkCodeAA, &key);
        _write(*dstFileHandle, checkCodeAA, 16);

        /* the next sizeof(struct ffblk) bytes area are used
         * to store the information of source file.        */
        _write(*dstFileHandle, &srcFileInfo, sizeof(struct ffblk));

        /* then store the original time and date */
        getftime(*srcFileHandle, &fileTime);
        _write(*dstFileHandle, &fileTime, sizeof(struct ftime));

        /* Get the file size */
        fileSize = filelength(*srcFileHandle);

        /* And the following 4 bytes save
         * the size of source file      */
        _write(*dstFileHandle, &fileSize, 4);
    }
    else if (DECRYPT == key.flag)
    {
        /* 判断密文文件是否合法,即在文
         * 件头上是否含有"SEA"标记   */
        _read(*srcFileHandle, fileMark, 4);
        if (0 != strcmp(fileMark, "SEA"))
        {
            _close(*srcFileHandle);
            _chmod(pathSrc, 1, srcFileInfo.ff_attrib);
            strcpy(message.displayStr, "[!]\n\n Invalid cipher file for SEA \n");
            Message(&message);
            getch();
            FreeMessage(&message);
            return FALSE;
        }

        /* 进行密钥校验 */
        _read(*srcFileHandle, checkCode55, 16);
        SEA(checkCode55, &key);
        _read(*srcFileHandle, checkCodeAA, 16);
        SEA(checkCodeAA, &key);
        for (i=0; i<16; i++)
        {
            if (0x55 != checkCode55[i]
                || 0xaa != checkCodeAA[i])
            {
                _close(*srcFileHandle);
                _chmod(pathSrc, 1, srcFileInfo.ff_attrib);
                strcpy(message.displayStr, "[!]\n\n  WRONG KEY for this CIPHER file  \n");
                Message(&message);
                getch();
                FreeMessage(&message);
                return FALSE;
            }
        }

        /* 合并出目标文件全路径名(新文件在源密文文件所在目录下生成) */
        _read(*srcFileHandle, &dstFileInfo, sizeof(struct ffblk));
        strcpy (pathDst, pathSrc);
        fnsplit(pathDst, drive, dir, file, ext);
        strcpy (pathDst, drive);
        strcat (pathDst, dir);
        strcat (pathDst, dstFileInfo.ff_name);

        /* 检验当前目录下是否已存在目标文件 */
        if (TRUE == CheckExist(pathDst))
        {
            _close(*srcFileHandle);
            _chmod(pathSrc, 1, srcFileInfo.ff_attrib);
            strcpy(message.displayStr, "[!]\n\n For safety, System WON'T OVERWRITE the EXISTED file: ");
            strcat(message.displayStr, dstFileInfo.ff_name);
            strcat(message.displayStr, " \n");
            Message(&message);
            getch();
            FreeMessage(&message);
            return FALSE;
        }

        /* 然后创建新文件并以只写方式打开 */
	if (-1 ==
            (*dstFileHandle = creat(pathDst, S_IWRITE)))
        {
            _close(*srcFileHandle);
            _chmod(pathSrc, 1, srcFileInfo.ff_attrib);
            strcpy(message.displayStr, "[!]\n\n  Unable to build destination file  \n");
            Message(&message);
            getch();
            FreeMessage(&message);
            return FALSE;
        }

        /* 读取原明文文件的创建时间 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -