Thread: Reading encrypted text file in program!

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    Reading encrypted text file in program!

    Hello peeps!


    I need some help to decrypt my text file and make it able to read in my program..

    What I have programmed so far is to read the encrypted file, create a new file, decrypt it and read the newly created file..

    I need to decrypt the encrypted file without having to create a new file that reads the decrypted text..

    Well, Let me show you my code:

    P.S Most of the include is not needed and I already know that

    Visual studio 2010 Windows Form Application CLR

    Code:
    #pragma once
    
    
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    #include <stdio.h>
    #include <iomanip>
    
    
    namespace EncryptandDecryptfiletest {
    
    
        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;
        using namespace System::IO;
        using namespace std;
    
    
        private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
    
    
                     /*Decryption --- When program loads*/
    
    
                     char ch,mod;
                     char key = 97;
                     char name[100] = "Encrypted.txt";
                     char target[100] = "TTO.txt";
                     ifstream fin("Encrypted.txt", ios::binary); // Reading file
                     if(!fin) //open the encrypted file in a binary mode
                     {
                         MessageBox::Show("Encrypted.txt did not open"); //If file does not exist
                     } //or any kind of error
                     
                     ofstream fout;
                     fout.open(target,ios::binary); //Opens outputfile
                     if(!fout)
                     { //Show error if any error occurs in opening new file
                         MessageBox::Show("TTO.txt did not open");
                     }
                     while(fin.get(ch))
                     { // opens the Encrypted file
                         if(ch==EOF)break;
                         mod = ch + key;
                         if (mod > 255 ) mod -= 255;
                         fout << mod; //Writes decrypted text to TTO.txt
                     }
                     fin.close(); //Close the encrypted file
                     fout.close(); // Close the decrypted file
                 }
        private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
    
    
                     label1->Text = comboBox1->Text;
                     pictureBox1->Load("Images\\" + comboBox1->SelectedItem->ToString() + ".png");
    
    
                     //String^ openFileTest = "Encrypted.txt"; // Opens the encrypted .txt file
                     String^ openFileTest = "TTO.txt"; //Opens the newly created file that is decrypted
    
    
                     try  //Reading the .txt file
                     {
                         StreamReader^ DataIn = File::OpenText(openFileTest);
                         String^ DataStr;
                         int count = 0;
                         array<String^>^ result;
                         array<Char>^ separ = gcnew array<Char>{'"'}; //After each Quote gets a new value of result[x]
    
    
                         while((DataStr = DataIn->ReadLine()) != nullptr)
                         {
                             count++;
                             result = DataStr->Split(separ);
                             if(comboBox1->Text == result[0]) // result[0] = Name
                             {
                                 textBox1->Text = result[1]; //reads first word in txt file
                                 textBox2->Text = result[2]; //second word in txt file
                                 textBox3->Text = result[3]; //third word in txt file
                             }
                         } // ends while()
                     } // ends try
                     catch (Exception^ e)
                     {
                         if(dynamic_cast<FileNotFoundException^>(e))
                             MessageBox::Show("File " + openFileTest + " not found");
                     }
                 } // Ends comboBox1_SelectedIndexChanged void
    };
    }
    You have my decryption code and the code I want to use..

    I have uploaded the code for you to use on your computer because it is fairly hard for me to explain..

    I want to be able to read the encrypted file in my program, without having to writing a new file to decrypt it..

    I hope anyone is able to help me

    Decrypted & Encrypted .txt file Included (And images)

    --> Program .rar pack link <--

    Build it with Visual Studio 2010

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    You do know that you can solve this problem using standard C++, right?

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    By the way, this is why "C++" sucks, especially as a first language. In a few years (maybe 1) we're going to have a great number of people who cannot code their way out of a bag made of oxygen.

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    2
    Quote Originally Posted by SirPrattlepod View Post
    You do know that you can solve this problem using standard C++, right?
    I do know that, but I can't figure it out.. that is why I need help with it.

    and yes everything is already coded with c++...

    What I can't figure out is how to read the encrypted file, and use the encrypted file to write out the stuff i want to write out in the program..

    All I have figured out so far is how to decrypt the encrypted file, create a new file with the decrypted text and open that file to let the program read..

    Ofcourse, you can already see that in the code

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by xakzyy View Post
    and yes everything is already coded with c++...
    What I can't figure out is how to read the encrypted file, and use the encrypted file to write out the stuff i want to write out in the program..
    It's not standard C++, though, so not as many people will be familiar with it. You're mixing languages. You use the standard ifstream class to read the file in one part, then you use StreamReader in another.

    However, it seems that you simply need to read the file into a string, perhaps by writing it to a stringstream.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    This was already asked by the OP in another thread, If anything stick to using the forms stuff for your GUI only, ie the front end, and use standard C++ for the data handling, backend work.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. an encrypted program
    By makonikor in forum C Programming
    Replies: 13
    Last Post: 07-14-2010, 01:35 PM
  2. Writing encrypted value to file.
    By mkthnx001 in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 12:42 PM
  3. Reading encrypted data from file
    By cctoombs in forum C Programming
    Replies: 2
    Last Post: 02-13-2005, 02:59 AM
  4. reading text from a file into your program
    By deltabird in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2003, 10:34 AM