Search This Blog

Simplicity is the ultimate sophistication.” — Leonardo da Vinci
Contact me: sreramk360@gmail.com

Tuesday, 20 January 2015

A small encryption program



A small encryption program
 

The following program is a small example of a password protector (or an encryption program). You drag the file to be encrypted and place it on the compiled binary release of the following code; the file gets opened by that application and the entire information, binary or text, is taken into the random access memory and there, the encryption takes place. Once the data gets encrypted, its written back to the file. The data present in the file starts to make no sense at all until it gets decrypted using the correct password. This is usually done to protect a confidential file from being viewed by a third person.

The following code is not actually suited for real time protection, but is the best example for beginners in cryptography. It has many loop-holes that could be exploited.
/*
* password protector
* By K Sreram
*/
#include <iostream>
#include <fstream>
#include <list>
#include <string>

const char encryptionPin[] = { "password" };

int main(int argc, char **argv)
{
    std::string fileName;
    if (!(argc >= 2))
    {
        std::cout << " enter file name:";
        std::getline(std::cin, fileName);
    }
    else
        fileName = argv[1];


    char data;  size_t i;
   
    std::fstream file(fileName, std::ios::in|std::ios::binary);
    std::list<char> filesData;
    std::list<char>::iterator index;
    std::string password;
    std::cout << "\n\n enter the password\n";
    std::cin >> password; // get the password (does not include space)
   
    for (i = 0; i < 8; i++)        filesData.push_back(encryptionPin[i]);
   
    while (!file.eof())
    {
        file.read(&data, sizeof(char)/*always 1*/);
        filesData.push_back(data); /// get the file's data in a list
    }
    file.clear();
    file.close();
    file.open(fileName , std::ios::out | std::ios::binary); /// prepare the file for writing
    const size_t passwordLength = password.size();

    for (index = filesData.begin(), i = 0; index != filesData.end(); index++, i++)
    {
        (*index) = (*index) + (char)i + password.c_str()[i%passwordLength];
        data = (*index);
        file.write(&data, sizeof(char));
    }
    std::cout << "file encryption completed";
    file.clear();
    file.close();
    system("pause");
   return 0;
}

About my blog

2 comments:

  1. This encryption algorithm does not guaranty any real-time protection. This is just an example that can only used for learning propose. To have a more sophisticated encryption algorithm, you would need to have a more stronger key generator unlike the one above.

    ReplyDelete

Featured post

Why complicating matters is not good

“ Simplicity is the ultimate sophistication.” — Leonardo da Vinci Why is complicating things wrong ? - K Sr...