0

我需要一些帮助来解密我的文本文件并使其能够在我的程序中读取..

到目前为止我编程的是读取加密文件,创建一个新文件,解密它并读取新创建的文件..

我需要解密加密文件,而不必创建一个读取解密文本的新文件..

好吧,让我向您展示我的代码:

PS 大部分包含是不需要的,我已经知道了

Visual Studio 2010 Windows 窗体应用程序 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
 };
}

你有我的解密代码和我想使用的代码..

我已经上传了代码供您在您的计算机上使用,因为我很难解释..

我希望能够在我的程序中读取加密文件,而不必编写新文件来解密它..

我希望有人能帮助我

包括解密和加密的 .txt 文件(和图像)

--> 程序.rar包链接<--

使用 Visual Studio 2010 构建它

4

1 回答 1

0

只需将输出文件流替换为MemoryStream.

首先要做的是分解成函数。由于您逐字节读取流,因此您不需要阅读器,这很好。

void Decrypt(Stream^ input, Stream^ output)
{
    char key=97;
    int byteRead;
    while((byteRead=input->ReadByte()) >= 0)
    {
        char ch = (char)byteRead;
        char mod = (char)(ch+key)
        output->WriteByte(mod);
    }
}

//When loading
MemoryStream^ ms = gcnew MemoryStream();
{
    FileStream^ fs = File::OpenRead(L"encrypted.txt");
    Decrypt(fr, ms);
    delete fs;
}

ms.Seek(0, SeekOrigin::Begin);

//Later, read from the memory stream
于 2013-10-03T14:24:17.150 回答