#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Help for executing this program code without error and get proper output


    #include <stdio.h>
    #include <stdlib.h>

    #include <openssl/pem.h>
    #include <openssl/conf.h>
    #include <openssl/x509v3.h>
    #include <openssl/pkcs12.h>
    #include <openssl/evp.h>
    #include <openssl/rsa.h>
    #include <openssl/md5.h>
    #include <openssl/rc4.h>
    #include <iostream>

    using namespace std;
    #define MAX_PATH 512

    #define BUFLEN 2048
    void DoEncrypt(const char* srcfile, const char* enc_file)
    {
    char mykey[EVP_MAX_KEY_LENGTH] = "this's my key";
    char iv[EVP_MAX_IV_LENGTH] = "my iv";
    char ciphertext[BUFLEN*2] = {0};
    char plaintext[BUFLEN] = {0};
    FILE* fpread = fopen(srcfile, "rb");
    int iReadLen = 0;
    if (NULL == fpread)
    {
    cout<<"do encrypt read src file fail" <<endl;
    return;
    }
    FILE* fpwrite = fopen(enc_file, "w+");
    if (NULL == fpwrite)
    {
    cout<<"enc_file to create fail" <<endl;
    fclose(fpread);
    return;
    }
    const EVP_CIPHER* cipherType = EVP_des_ede3_ecb();
    EVP_CIPHER_CTX ctx;
    EVP_CIPHER_CTX_init(&ctx);

    int out_len;
    EVP_EncryptInit(&ctx, cipherType, (const unsigned char*)mykey, (const unsigned char*)iv);
    while ( (iReadLen = fread(plaintext, 1, BUFLEN, fpread)) > 0 )
    {
    memset(ciphertext, 0x00, sizeof(ciphertext));
    EVP_EncryptUpdate(&ctx, (unsigned char*)ciphertext, &out_len, (const unsigned char*)plaintext, iReadLen);
    fwrite(ciphertext, 1, out_len, fpwrite);
    memset(plaintext, 0x00, sizeof(plaintext));
    }
    EVP_EncryptFinal(&ctx, (unsigned char*)ciphertext, &out_len);
    fwrite(ciphertext, 1, out_len, fpwrite);
    EVP_CIPHER_CTX_cleanup(&ctx);

    fclose(fpread);
    fclose(fpwrite);
    cout<< "DoEncrypt finish" <<endl;
    }
    void DoDecrypt(const char* enc_file, const char* dec_file)
    {
    char mykey[EVP_MAX_KEY_LENGTH] = "this's my key";
    char iv[EVP_MAX_IV_LENGTH] = "my iv";
    char ciphertext[BUFLEN*2] = {0};
    char plaintext[BUFLEN] = {0};
    FILE* fpread = fopen(enc_file, "rb");
    if (NULL == fpread)
    {
    cout<<"DoDecrypt enc file open fail" <<endl;
    return;
    }
    FILE* fpwrite = fopen(dec_file, "w+");
    if (NULL == fpwrite)
    {
    cout<< "DoDecrypt open dec file create fail" <<endl;
    fclose(fpread);
    return;
    }

    const EVP_CIPHER* cipherType = EVP_des_ede3_ecb();
    EVP_CIPHER_CTX ctx;
    EVP_CIPHER_CTX_init(&ctx);
    EVP_DecryptInit(&ctx, (const EVP_CIPHER*)cipherType, (const unsigned char*)mykey, (const unsigned char*)iv);
    int iReadLen, out_len;
    while ( (iReadLen = fread(plaintext, 1, BUFLEN, fpread)) > 0 )
    {
    memset(ciphertext, 0x00, sizeof(ciphertext));
    EVP_DecryptUpdate(&ctx, (unsigned char*)ciphertext, &out_len, (const unsigned char*)plaintext, iReadLen);
    fwrite(ciphertext, 1, out_len, fpwrite);
    memset(plaintext, 0x00, sizeof(plaintext));
    }
    EVP_DecryptFinal(&ctx, (unsigned char*)ciphertext, &out_len);
    fwrite(ciphertext, 1, out_len, fpwrite);
    EVP_CIPHER_CTX_cleanup(&ctx);

    fclose(fpread);
    fclose(fpwrite);
    cout<< "DoDecrypt finished" <<endl;
    }

    int main(void)
    {
    const char* srcfile = "abc.txt";
    const char* enc_file = "abc.txt.enc";
    const char* dec_file = "abc.txt.dec";
    DoEncrypt(srcfile, enc_file);
    DoDecrypt(enc_file, dec_file);
    return 0;
    }
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    And your question is?
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    how shall i execute that program code without errors please help
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Just do it correctly.

    Why? Do you think that you're getting errors? What makes you think that?

    Please be specific, so that you can give us a specific question instead of a useless general question. Only then can you ever hope to to get a specific answer that actually addresses the problem that you imagine that you are having.

    Or you could continue to ask useless general questions, in which case you will continue to receive useless general answers.

    And please use code tags, because nobody will try to read that horrible mess that you posted. Readable code gets read; unreadable code gets ignored.

    You need to help us to help you.
    Last edited by dwise1_aol; April 12th, 2013 at 12:06 AM.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    aksara, this is what your code looks like when code tags are used.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <openssl/pem.h>
    #include <openssl/conf.h>
    #include <openssl/x509v3.h>
    #include <openssl/pkcs12.h>
    #include <openssl/evp.h>
    #include <openssl/rsa.h>
    #include <openssl/md5.h>
    #include <openssl/rc4.h>
    #include <iostream>
    
    using namespace std;
    #define MAX_PATH 512
    
    #define BUFLEN  2048
    void DoEncrypt(const char* srcfile, const char* enc_file)
    {
        char mykey[EVP_MAX_KEY_LENGTH] = "this's my key";
        char iv[EVP_MAX_IV_LENGTH] = "my iv"; 
        char ciphertext[BUFLEN*2] = {0};
        char plaintext[BUFLEN] = {0};
        FILE* fpread = fopen(srcfile, "rb");
        int iReadLen = 0;
        if (NULL == fpread)
        {
            cout<<"do encrypt read src file fail" <<endl;
            return;
        }
        FILE* fpwrite = fopen(enc_file, "w+");
        if (NULL == fpwrite)
        {
            cout<<"enc_file to create fail" <<endl;
            fclose(fpread);
            return;
        }
        const EVP_CIPHER* cipherType = EVP_des_ede3_ecb();
        EVP_CIPHER_CTX ctx;
        EVP_CIPHER_CTX_init(&ctx);
    
        int out_len;
        EVP_EncryptInit(&ctx, cipherType, (const unsigned char*)mykey, (const unsigned char*)iv);
        while ( (iReadLen = fread(plaintext, 1, BUFLEN, fpread)) > 0 )
        {
            memset(ciphertext, 0x00, sizeof(ciphertext));
            EVP_EncryptUpdate(&ctx, (unsigned char*)ciphertext, &out_len, (const unsigned char*)plaintext, iReadLen);
            fwrite(ciphertext, 1, out_len, fpwrite);
            memset(plaintext, 0x00, sizeof(plaintext));
        }
        EVP_EncryptFinal(&ctx, (unsigned char*)ciphertext, &out_len);
        fwrite(ciphertext, 1, out_len, fpwrite);
        EVP_CIPHER_CTX_cleanup(&ctx);
    
        fclose(fpread);
        fclose(fpwrite);
        cout<< "DoEncrypt finish" <<endl;
    }
    void DoDecrypt(const char* enc_file, const char* dec_file)
    {
        char mykey[EVP_MAX_KEY_LENGTH] = "this's my key";
        char iv[EVP_MAX_IV_LENGTH] = "my iv"; 
        char ciphertext[BUFLEN*2] = {0};
        char plaintext[BUFLEN] = {0};
        FILE* fpread = fopen(enc_file, "rb");
        if (NULL == fpread)
        {
            cout<<"DoDecrypt enc file open fail" <<endl;
            return;
        }
        FILE* fpwrite = fopen(dec_file, "w+");
        if (NULL == fpwrite)
        {
            cout<< "DoDecrypt open dec file create fail" <<endl;
            fclose(fpread);
            return;
        }
    
        const EVP_CIPHER* cipherType = EVP_des_ede3_ecb();
        EVP_CIPHER_CTX ctx;
        EVP_CIPHER_CTX_init(&ctx);
        EVP_DecryptInit(&ctx, (const EVP_CIPHER*)cipherType, (const unsigned char*)mykey, (const unsigned char*)iv);
        int iReadLen, out_len;
        while ( (iReadLen = fread(plaintext, 1, BUFLEN, fpread)) > 0 )
        {
            memset(ciphertext, 0x00, sizeof(ciphertext));
            EVP_DecryptUpdate(&ctx, (unsigned char*)ciphertext, &out_len, (const unsigned char*)plaintext, iReadLen);
            fwrite(ciphertext, 1, out_len, fpwrite);
            memset(plaintext, 0x00, sizeof(plaintext));
        }
        EVP_DecryptFinal(&ctx, (unsigned char*)ciphertext, &out_len);
        fwrite(ciphertext, 1, out_len, fpwrite);
        EVP_CIPHER_CTX_cleanup(&ctx);
    
        fclose(fpread);
        fclose(fpwrite);
        cout<< "DoDecrypt finished" <<endl;
    }
    
    int main(void) 
    {
        const char* srcfile = "abc.txt";
        const char* enc_file = "abc.txt.enc";
        const char* dec_file = "abc.txt.dec";
        DoEncrypt(srcfile, enc_file);
        DoDecrypt(enc_file, dec_file);
        return 0;
    }
    I had obtained your original formatting via the Reply button. You can use the Reply button on this message to see how to use code tags.

    You actually did a fairly nice job of formatting.
  10. #6
  11. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Actually, the code is just copied/pasted from either this two-year-old SO post or its cross-post on LinuxQuestions, and this "question" was also in turn cross-posted here. No credit for anything is due.

    The only assumption we can make regarding this code (due to the lack of a cogent question) is that the OP has no idea how to compile it, perhaps because they don't have the OpenSSL development kit installed, or they don't know how to link external libraries.

    Comments on this post

    • dwise1_aol agrees : Hence the quality of the formatting.
    Last edited by ptr2void; April 12th, 2013 at 10:46 AM.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!

IMN logo majestic logo threadwatch logo seochat tools logo