Cool Piece Of Code In C++
Results 1 to 2 of 2

Thread: Cool Piece Of Code In C++

  1. #1
    Banned
    Join Date
    Oct 2003
    Posts
    19

    File Encryption in C++

    [gloworange]************THIS C++ CODE BELONGS TO Th3 SpIdER********************** [/gloworange]

    Heres a simple method to make a basic encryption program in C++ for simple file encryption. any suggestions.......
    i have include the compiled program also as encrypt.zip

    #include<fstream.h>
    #include<string.h>
    #include<conio.h>
    void printerror(int);
    enum{infileerr,outfileerr,passworderr};
    int main(int argc, char* argv[])


    {

    clrscr();
    int fileno=1;
    char infilename[255];
    char outfilename[255];
    char password[255];
    char ch;
    cout<<"\t\t\t------------------------------\n";
    cout<<"\t\t\t Spider File Encrytion System\n";
    cout<<"\t\t\tWritten by Faizan Bashir - HbK\n";
    cout<<"\t\t\t------------------------------\n\n";
    do //Start for here


    {

    //--------------------------------------
    //
    // -------------------------------------
    // ---
    //
    //Take care of infile
    ++fileno;
    cout<<"Enter path of file to be encrypted/decrypted: ";
    if (argc<2)


    {
    cin.getline(infilename,255);
    }
    else


    {
    strcpy(infilename,argv[fileno-1]);
    cout<<infilename<<"\n";
    }
    ifstream infile(infilename,ios::binary); //prepare infile.
    if (!infile)


    {
    printerror(infileerr);
    return -1;
    }
    cout<<"\nEnter Name of destination file: ";
    cin.getline(outfilename,255);
    ofstream outfile(outfilename,ios::binary); //prepare outfile.
    if (!outfile)


    {
    printerror(outfileerr);
    return -1;
    }
    //--------------------------------------
    //
    // -------------------------------------
    // ---
    //
    //Take care of password
    cout<<"\n\nPassword: ";
    for (int i2=0;i2<255;i2++)password[i2]='';
    cin.getline(password,255);
    int endofstr;
    char passwordcopy[255];
    for (int i1=0;i1<255;i1++)


    {
    if(password[i1]=='\0')break;
    endofstr=i1;
    }
    strcpy(passwordcopy,password);
    for (int i=endofstr;i>=0;i--)


    {
    password[endofstr-i]=password[endofstr-i]&(~passwordcopy[i]);
    }
    //--------------------------------------
    //
    // -------------------------------------
    // ---
    //
    //Encrpyt infile
    int index=0;
    while((infile.read((char*)&ch,sizeof(ch))))


    {
    if (password[index]=='\0') index=0;
    ch=(password[index])^ch; //xor it
    outfile.write((char*)&ch,sizeof(ch));
    index+=1;
    }
    //--------------------------------------
    //
    // -------------------------------------
    // ---
    //
    //Final messages
    infile.close();
    outfile.close();
    cout<<"\n|The Encryption/Decryption operation has been successfully completed|\n\n\n";

    } //End for here
    while(fileno<argc);
    while ((ch=cin.get())!=ch){cout<<"";}
    return 0;
    }
    void printerror(int error)


    {
    char ch;
    switch(error)


    {


    case infileerr:{
    cout<<"\nError: File not found\n";


    break;
    }


    case outfileerr:{
    cout<<"\nError: Unable to create destination file\n";

    break;
    }


    case passworderr:{
    cout<<"\nError: Invalid password\n";

    break;
    }
    }
    while ((ch=cin.get())!=ch){cout<<"";}
    }


    _________________________________
    any suggestion??????


  2. #2
    Senior Member
    Join Date
    Sep 2003
    Posts
    179
    I downnloaded your compiled version and the file your encryption program created was exactly the same as the source file.

    Here is the data I used:

    1. Opened up notepad and created a txt file with the words "I love" as the data and saved it as: 'C:\ray.txt'
    2. Ran your program.
    3. Entered in 'C:\ray.txt' when prompted for a file.
    4. Entered in 'C:\love.txt' as my ouput file.
    5. Entered 'big' as my password.


    love.txt reads "I love" the same as the source file.

    Sorry man, I'll read your code later to see If I can find out whats up. Sure someone else will be able to help also. Time for Class.

    DeafLamb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •