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

📄 roil.c

📁 使用OpenSSL协议对网络传输文件加密并解密。是编写网络安全工具的首选。
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  $Id: roil.c,v 1.1 2002/04/11 04:42:06 route Exp $ * *  Building Open Source Network Security Tools *  roil.c - openssl example code * *  Copyright (c) 2002 Mike D. Schiffman <mike@infonexus.com> *  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include "./roil.h"intmain(int argc, char **argv){    int c;    u_char flags;    char *md;    char *ea;    FILE *filename;    char errbuf[256];    struct roil_pack *rp;    printf("Roil 1.0 [little encryption tool]\n");    flags = 0;    md = NULL;    ea = NULL;    filename = NULL;    while ((c = getopt(argc, argv, "de:hm:")) != EOF)    {        switch (c)        {            case 'd':                flags |= DECRYPT;                break;            case 'e':                ea = optarg;                flags |= ENCRYPT;                break;            case 'h':                usage(argv[0]);                exit(EXIT_SUCCESS);                break;            case 'm':                md = optarg;                flags |= MD;                break;            default:                usage(argv[0]);                exit(EXIT_FAILURE);        }    }    if (flags == 0 || (flags & ENCRYPT && flags & DECRYPT))    {        usage(argv[0]);        exit(EXIT_FAILURE);    }    if (argc - optind != 1)    {        usage(argv[0]);        exit(EXIT_FAILURE);    }    rp = roil_init(argv[optind], flags, md, ea, errbuf);    if (rp == NULL)    {        fprintf(stderr, "roil_init(): %s\n", errbuf);        exit(EXIT_FAILURE);    }    roil(rp);    roil_destroy(rp);    return (EXIT_SUCCESS);}struct roil_pack *roil_init(char *filename, u_char flags, char *md, char *ea, char *errbuf){    struct roil_pack *rp;    /* grab memory for our monolithic structure */    rp = malloc(sizeof (struct roil_pack));    if (rp == NULL)    {        sprintf(errbuf, strerror(errno));        return (NULL);    }    /* open the input file */    rp->fd_in = open(filename, O_RDWR);    if (rp->fd_in == -1)    {        sprintf(errbuf, "can't open input file \"%s\" %s",                filename, strerror(errno));        roil_destroy(rp);        return (NULL);    }    /* save the filename */    strncpy(rp->fn_in, filename, sizeof (rp->fn_in) - 1);    rp->flags = flags;    /* copy over the message digest name */    if (md)    {        strncpy(rp->md, md, 10);    }    /* copy over the message digest name */    if (ea)    {        strncpy(rp->ea, ea, 10);    }    return (rp);}voidroil_destroy(struct roil_pack *rp){    if (rp)    {        if (rp->fd_in)        {            close (rp->fd_in);        }        if (rp->fd_out)        {            close (rp->fd_out);        }        free(rp);        EVP_cleanup();    }}intopen_outputfile(struct roil_pack *rp){    int n;    n = strlen(rp->fn_in);    strcpy(rp->fn_out, rp->fn_in);    if (rp->flags & ENCRYPT)    {        if (!(n + 4 < 100))        {            /* filename too long */            sprintf(rp->errbuf, "open_outputfile(): filename too long\n");            return (-1);        }        strcpy(rp->fn_out + n, ".roil");    }    else    {        if (n < 4)        {            /* filename too short */            sprintf(rp->errbuf,                    "open_outputfile(): filename too short\n");            return (-1);        }        if (strncmp(&rp->fn_out[n - 5], ".roil", 5) == 0)        {            /* cut ".roil" from filename */            rp->fn_out[n - 5] = 0;        }        else        {            /* unknown suffix / filename */            sprintf(rp->errbuf, "open_outputfile(): unknown suffix\n");            return (-1);        }    }    /* open the file */    rp->fd_out = open(rp->fn_out, O_CREAT | O_WRONLY);    if (rp->fd_out == -1)    {        sprintf(rp->errbuf, "open_outputfile(): %s\n", strerror(errno));        return (-1);    }    /* set a umask of 600 */    if (fchmod(rp->fd_out, 0600) == -1)    {        sprintf(rp->errbuf, "open_outputfile(): %s\n", strerror(errno));        return (-1);    }    return (1);}voidroil(struct roil_pack *rp){    int n, len;    u_char *p;    if (rp->flags & MD)    {        /*         *  We're going to be digesting a file here.  The other case         *  when we would be digesting a user's passphrase to create a         *  sufficiently long key for encryption or decryption comes         *  into play from within roil_cipher() and never here.         */        rp->flags |= MD_FROMFILE;        /*         *  Digest the file contained in rp.  Upon success, the function         *  will return a pointer to a static buffer containing the hash         *  and the length will be written to len.  Upon failure p will         *  point to a NULL buffer and rp->errbuf will contain the         *  reason.         */        p = roil_digest(rp, &len);        if (p == NULL)        {            fprintf(stderr, "roil_digest(): %s", rp->errbuf);            return;        }        printf("%s message digest of %s: ", rp->md, rp->fn_in);        for (n = 0; n < len; n++)        {            printf("%02x", p[n]);        }        printf("\n");    }    else if ((rp->flags & ENCRYPT) || (rp->flags & DECRYPT))    {        /*         *  Encrypt or decrypt the file contained in rp.  Upon succes, the          *  function will return 1; upon failure the function will return          *  -1 and rp->errbuf will contain the reason.         */        if (roil_cipher(rp) == -1)        {            fprintf(stderr, "roil_cipher(): %s", rp->errbuf);            return;        }    }}u_char *roil_digest(struct roil_pack *rp, int *digest_len){    int n;    const EVP_MD *md;    u_char buf[BUF_SIZE];    EVP_MD_CTX md_context;    static u_char digest[EVP_MAX_MD_SIZE];    /* add all available digest algorithms to the hash table */    OpenSSL_add_all_digests();    /* load and verify the digest specified at the command line */    md = EVP_get_digestbyname(rp->md);    if (md == NULL)    {        snprintf(rp->errbuf, ERRBUF_SIZE, "unknown digest %s\n", rp->md);        goto bad;    }    /*     *  Initialize the md context.  Really all this does is zero out the     *  structure.     */    EVP_MD_CTX_init(&md_context);    /* initialize the md algorithm */    if (EVP_DigestInit(&md_context, md) == 0)    {        snprintf(rp->errbuf, ERRBUF_SIZE, "EVP_DigestInit() failed\n");        goto bad;    }    memset (digest, 0, sizeof (digest));    if (rp->flags & MD_FROMFILE)    {        /*         *  Digest the file.  Read in a block of data into buf and         *  process it with the md algorithm.         */        while ((n = read(rp->fd_in, buf, sizeof (buf))) > 0)        {            if (EVP_DigestUpdate(&md_context, buf, n) == 0)            {                snprintf(rp->errbuf, ERRBUF_SIZE,                        "EVP_DigestUpdate() failed\n");                goto bad;            }        }        /* retrieve the digest value and length from the md context */        if (EVP_DigestFinal(&md_context, digest, digest_len) == 0)        {            snprintf(rp->errbuf, ERRBUF_SIZE,                    "EVP_DigestFinal() failed\n");            goto bad;        }    }    else    {        /*         *  Digest a user's passphrase.  Since we know this no more         *  than KEY_LENGTH bytes, we can do it all in one chunk.         */        if (EVP_DigestUpdate(&md_context, rp->passphrase,                strlen(rp->passphrase)) == 0)        {            snprintf(rp->errbuf, ERRBUF_SIZE,                    "EVP_DigestUpdate() failed\n");            goto bad;        }        if (EVP_DigestFinal(&md_context, digest, digest_len) == 0)        {            snprintf(rp->errbuf, ERRBUF_SIZE,                    "EVP_DigestFinal() failed\n");            goto bad;        }    }    return (digest);bad:    *digest_len = 0;    return (NULL);

⌨️ 快捷键说明

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