我需要一些帮助来解密我的文本文件,并使其能够读入我的程序..
到目前为止我编写的内容是读取加密文件,创建新文件,解密并读取新创建的文件。
我需要解密加密文件,而不必创建一个读取解密文本的新文件..
好吧,让我告诉你我的代码:
P.S大部分内容都不需要,我已经知道了
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
};
}
你有我的解密代码和我想要使用的代码..
我已经上传了您在计算机上使用的代码,因为我很难解释..
我希望能够在我的程序中读取加密文件,而无需编写新文件来解密它。
我希望有人能帮助我
解密&amp;加密的.txt文件包含(和图像)
- &GT;计划.rar pack link&lt; -
使用Visual Studio 2010构建它
答案 0 :(得分: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