Want to Join Us ?

you'll be able to discuss, share and send private messages.

Simple File Encrypter

Discussion in 'C++' started by storm shadow, Feb 21, 2013.

Share This Page

  1. storm shadow

    Techbliss Owner Admin Ida Pro Expert Developer

    Code (C):
    #include <iostream>
        #include <stdlib.h>
        #include <fstream>
        #include <string>
        #include <cstdio>
     
        using namespace std;
     
        int encryptchar(char ech);
        char decryptchar(int dch);
        void Encryption();
        void Decryption();
     
     
     
        int main()
        {
            cout << "File Encryption" << endl << endl;
            cout << "1. Encrypt file" << endl;
            cout << "2. Decrypt file" << endl << endl;
            cout << "Command:";
            int command;
            cin >> command;
     
            switch(command)
            {
                case 1:
                Encryption();
                case 2:
                Decryption();
                default:
                exit(1);
            }
     
            return 0;
        }
     
        void Encryption()
        {
            cout << endl << "File to encrypt:";
     
            string eFilepath;
            cin >> eFilepath;
     
            string eFileoutput;
            cout << "Fileoutput:";
            cin >> eFileoutput;
     
            try
            {
                FILE *eFile;
                FILE *eOutput;
     
                    eFile = fopen(eFilepath.c_str(), "r");
                    eOutput = fopen(eFileoutput.c_str(), "w");
                    while(!(feof(eFile)))
                    {
                        char ch;
                        ch = fgetc(eFile);
     
                        if(feof(eFile))
                            break;
     
                        cout << encryptchar(ch) << endl;
                        fprintf(eOutput, "%d", encryptchar(ch));
                        fprintf(eOutput, "\n");
                    }
     
                    fclose(eFile);
                    fclose(eOutput);
            }
            catch(exception e)
            {
                cout << endl << "ERROR!" << endl;
            }
     
            cout << endl << "File successfully encrypted." << endl;
            cin.ignore().get();
            exit(1);
        }
     
        void Decryption()
        {
            cout << endl << "File to decrypt:";
     
            string dFilepath;
            cin >> dFilepath;
            cout << "Fileoutput:";
            string dOutputpath;
            cin >> dOutputpath;
     
            try
            {
                ifstream dFile;
                FILE *dOutput;
     
                dFile.open(dFilepath.c_str());
                dOutput = fopen(dOutputpath.c_str(), "w");
     
                while(!(dFile.eof()))
                {
                    string strLine;
                    getline(dFile, strLine);
     
                    if(dFile.eof())
                            break;
     
                    int intLine = atoi(strLine.c_str());
     
                    cout << decryptchar(intLine);
                    fprintf(dOutput, "%c",decryptchar(intLine));
                }
     
                dFile.close();
                fclose(dOutput);
     
            }
            catch(exception e)
            {
                cout << endl << "ERROR!" << endl;
            }
     
            cout << endl << "File successfully decrypted." << endl;
            cin.ignore().get();
            exit(1);
        }
     
        /*
                Functions and Encryptchar Decryptchar can be changed.
                For example, passing an integer as a password, each individual number
                different effect. pw = 1234, for example: +1 * 2/3 + 4
        */

        int encryptchar(char ech)
        {
            int returnInteger = (int)ech;
     
            returnInteger += 23;
            returnInteger *= 2;
            returnInteger -= 17;
            returnInteger *= 5;
            returnInteger -= 35;
     
            return returnInteger;
        }
     
        char decryptchar(int dch)
        {
            dch += 35;
            dch /= 5;
            dch += 17;
            dch /= 2;
            dch -= 23;
     
            char returnChar = (char)dch;
            return returnChar;
        }
     
     
     

    Attached Files:

    Rip Cord likes this.
  2. dila

    Member

    • dila
    • Sep 17, 2015
    • 10
    • 12
    I noticed that because each character is enciphered separately you can generate a 256-byte lookup table and avoid the cost of the operations for each byte:

    Code (Text):
    #include <stdint.h> // for uint8_t type
    uint8_t table[256];
    for (int i = 0; i < 256; ++i) {
        table[i] = encryptchar(i);
    }
    And then encryption becomes:

    Code (Text):
    uint8_t encrypt(uint8_t byte) {
        return table[byte];
    }
     
    storm shadow likes this.
Top