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

#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

Recommended Answers

All 3 Replies

Solution seems simple enough -- put the lines in a vector of strings instead of writing them to a file. Build a string by adding characters one as a time as they are read and decrypt. When '\n' is reached then you know that's the end of the line and add the entire string to a vector.

char name[100] = "Encrypted.txt";

Why use so much space in that array? All you need is this: char name[] = "Encrypted.txt"; or even better like this: const char* name = "Encrypted.txt";

I do know I used a lot of space in the array haha but even thought you gave me a helpful tip, I have 0 idea on how to use it..

Even thought I have gotten this far, I am not sure how to continue.. I hoped for a start of coding or something similiar to push me to somewhere of the right direction..

I really appriciate your help / tip, but it just doesn't say me much..

I am a kind of a coder who knows how to code, but doesn't know what the different stuff means.. sorry, I know it sounds stupid but that is how it always has worked for me :p

line 49: Useless line because the loop on line 47 stops when EOF is reached.

line 52 is where you would build a string.

Something like this:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>

void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

            vector<string> Lines; // make this a member of the Form1 class so that it can be used in other functions

             /*Decryption --- When program loads*/

            string line;
             char ch,mod;
             char key = 97;
             const char *name = "Encrypted.txt";
             ifstream fin(name, 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

             while(fin.get(ch))
             { // opens the Encrypted file
                 mod = ch + key;
                 if (mod > 255 ) mod -= 255;
            //     fout << mod; //Writes decrypted text to TTO.txt
                 line += mod;
                 if(mod == '\n')
                 {
                     Lines.push_back(line);
                     line = "";
                 }
             }
             fin.close(); //Cose the encrypted file
         }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.