aescrypt.c

来自「linux下完成任意文件的AES加密. 口令可变,加密稳定迅速.」· C语言 代码 · 共 1,135 行 · 第 1/3 页

C
1,135
字号
        memcpy(buffer2, tail, 16);        tail += 16;        if (tail == (buffer + 64))        {            tail = buffer;        }        memcpy(buffer2+16, tail, 16);    }    else    {        memcpy(buffer2, tail+1, 15);        tail += 16;        if (tail == (buffer + 64))        {            tail = buffer;        }        memcpy(buffer2+15, tail, 16);        tail += 16;        if (tail == (buffer + 64))        {            tail = buffer;        }        memcpy(buffer2+31, tail, 1);    }    if (memcmp(digest, buffer2, 32))    {        if (aeshdr.version == 0x00)        {            fprintf(stderr, "Error: Message has been altered or password is incorrect\n");        }        else        {            fprintf(stderr, "Error: Message has been altered and should not be trusted\n");        }        return -1;    }    return 0;}/* *  usage * *  Displays the program usage to the user. */void usage(const char *progname){    const char* progname_real; //contains the real name of the program (without path)    progname_real = rindex(progname, '/');    if (progname_real == NULL) //no path in progname: use progname    {        progname_real = progname;    }    else    {        progname_real++;    }    fprintf(stderr, "\nusage: %s {-e|-d} [-p <password>] { [-o <output filename>] <file> | <file> [<file> ...] }\n\n",            progname_real);}/* *  version * *  Displays the program version to the user. */void version(const char *progname){    const char* progname_real; //contains the real name of the program (without path)    progname_real = rindex(progname, '/');    if (progname_real == NULL) //no path in progname: use progname    {        progname_real = progname;    }    else    {        progname_real++;    }    fprintf(stderr, "\n%s version %s (%s)\n\n",            progname_real, PROG_VERSION, PROG_DATE);}/* *  cleanup * *  Removes output files that are not fully and properly created. */void cleanup(const char *outfile){    if (strcmp(outfile,"-") && outfile[0] != '\0')    {        unlink(outfile);    }}int main(int argc, char *argv[]){    int rc=0, passlen=0;    FILE *infp = NULL;    FILE *outfp = NULL;    encryptmode_t mode=UNINIT;    char *infile = NULL,         pass_input[MAX_PASSWD_LEN+1],         pass[MAX_PASSWD_LEN+1];    int file_count = 0;    char outfile[1024];    iconv_t condesc;    // Initialize the output filename    outfile[0] = '\0';        while ((rc = getopt(argc, argv, "vhdep:o:")) != -1)    {        switch (rc)        {            case 'h':                usage(argv[0]);                return 0;            case 'v':                version(argv[0]);                return 0;            case 'd':                if (mode != UNINIT)                {                    fprintf(stderr, "Error: only specify one of -d or -e\n");                    cleanup(outfile);                    return -1;                }                mode = DEC;                break;            case 'e':                if (mode != UNINIT)                {                    fprintf(stderr, "Error: only specify one of -d or -e\n");                    cleanup(outfile);                    return -1;                }                mode = ENC;                break;            case 'p':                if (optarg != 0)                {                    passlen = passwd_to_utf16(  optarg,                                                strlen(optarg),                                                MAX_PASSWD_LEN+1,                                                pass);                    if (passlen < 0)                    {                        cleanup(outfile);                        return -1;                    }                }                break;            case 'o':                // outfile argument                if (!strncmp("-", optarg, 2))                {                    // if '-' is outfile name then out to stdout                    outfp = stdout;                }                else if ((outfp = fopen(optarg, "w")) == NULL)                {                    fprintf(stderr, "Error opening output file %s:", optarg);                    perror("");                    cleanup(outfile);                    return  -1;                }                strncpy(outfile, optarg, 1024);                outfile[1023] = '\0';                break;            default:                fprintf(stderr, "Error: Unknown option '%c'\n", rc);        }    }        if (optind >= argc)    {        fprintf(stderr, "Error: No file argument specified\n");        usage(argv[0]);        cleanup(outfile);        return -1;    }    if (mode == UNINIT)    {        fprintf(stderr, "Error: -e or -d not specified\n");        usage(argv[0]);        cleanup(outfile);        return -1;    }    // Prompt for password if not provided on the command line    if (passlen == 0)    {        passlen = read_password(pass_input, mode);        switch (passlen)        {            case 0: //no password in input                fprintf(stderr, "Error: No password supplied.\n");                cleanup(outfile);                return -1;            case AESCRYPT_READPWD_FOPEN:            case AESCRYPT_READPWD_FILENO:            case AESCRYPT_READPWD_TCGETATTR:            case AESCRYPT_READPWD_TCSETATTR:            case AESCRYPT_READPWD_FGETC:            case AESCRYPT_READPWD_TOOLONG:                fprintf(stderr, "Error in read_password: %s.\n",                        read_password_error(passlen));                cleanup(outfile);                return -1;            case AESCRYPT_READPWD_NOMATCH:                fprintf(stderr, "Error: Passwords don't match.\n", passlen);                cleanup(outfile);                return -1;        }        passlen = passwd_to_utf16(  pass_input,                                    strlen(pass_input),                                    MAX_PASSWD_LEN+1,                                    pass);        if (passlen < 0)        {            cleanup(outfile);            // For security reasons, erase the password            memset(pass, 0, passlen);            return -1;        }    }    file_count = argc - optind;    if ((file_count > 1) && (outfp != NULL))    {        if (outfp != stdout)        {            fclose(outfp);        }        fprintf(stderr, "Error: A single output file may not be specified with multiple input files.\n");        usage(argv[0]);        cleanup(outfile);        // For security reasons, erase the password        memset(pass, 0, passlen);        return -1;    }    while (optind < argc)    {        infile = argv[optind++];        if ((outfp != NULL) ||            ((outfp == NULL) && (strncmp("-", infile, 2))))        {            printf("%s: %s\n",                   (mode == ENC) ? "Encrypting" : "Decrypting",                   strncmp(infile,"-",2) ? infile : "STDIN");        }        if(!strncmp("-", infile, 2))        {            if (file_count > 1)            {                if ((outfp != stdout) && (outfp != NULL))                {                    fclose(outfp);                }                fprintf(stderr, "Error: STDIN may not be specified with multiple input files.\n");                usage(argv[0]);                cleanup(outfile);                // For security reasons, erase the password                memset(pass, 0, passlen);                return -1;            }            infp = stdin;            if (outfp == NULL)            {                outfp = stdout;            }        }        else if ((infp = fopen(infile, "r")) == NULL)        {            if ((outfp != stdout) && (outfp != NULL))            {                fclose(outfp);            }            fprintf(stderr, "Error opening input file %s : ", infile);            perror("");            cleanup(outfile);            // For security reasons, erase the password            memset(pass, 0, passlen);            return  -1;        }                if (mode == ENC)        {            if (outfp == NULL)            {                snprintf(outfile, 1024, "%s.aes", infile);                if ((outfp = fopen(outfile, "w")) == NULL)                {                    if ((infp != stdin) && (infp != NULL))                    {                        fclose(infp);                    }                    fprintf(stderr, "Error opening output file %s : ", outfile);                    perror("");                    cleanup(outfile);                    // For security reasons, erase the password                    memset(pass, 0, passlen);                    return  -1;                }            }                        rc = encrypt_stream(infp, outfp, pass, passlen);        }        else if (mode == DEC)        {            if (outfp == NULL)            {                // assume .aes extension                strncpy(outfile, infile, strlen(infile)-4);                outfile[strlen(infile)-4] = '\0';                if ((outfp = fopen(outfile, "w")) == NULL)                {                    if ((infp != stdin) && (infp != NULL))                    {                        fclose(infp);                    }                    fprintf(stderr, "Error opening output file %s : ", outfile);                    perror("");                    cleanup(outfile);                    // For security reasons, erase the password                    memset(pass, 0, passlen);                    return  -1;                }            }                        // should probably test against ascii, utf-16le, and utf-16be encodings            rc = decrypt_stream(infp, outfp, pass, passlen);        }                if ((infp != stdin) && (infp != NULL))        {            fclose(infp);        }        if ((outfp != stdout) && (outfp != NULL))        {            fclose(outfp);        }        // If there was an error, remove the output file        if (rc)        {            cleanup(outfile);            // For security reasons, erase the password            memset(pass, 0, passlen);            return -1;        }        // Reset input/output file names and desriptors        outfile[0] = '\0';        infp = NULL;        outfp = NULL;    }    // For security reasons, erase the password    memset(pass, 0, passlen);        return rc;}

⌨️ 快捷键说明

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