Blank lines are the output after decrypting a file
I am encrypting and decrypting a file. Below is the code.
Encryption code
void InformationWriter::writeContacts(System::String ^phone,
System::String ^email)
{
//Write the file
StreamWriter ^originalTextWriter = gcnew
StreamWriter("contacts.dat",false);
originalTextWriter->WriteLine(phone);
originalTextWriter->WriteLine(email);
originalTextWriter->Close();
//Encrypt the file
FileStream ^fileWriter = gcnew
FileStream("contacts2.dat",FileMode::Create,FileAccess::Write);
DESCryptoServiceProvider ^crypto = gcnew DESCryptoServiceProvider();
crypto->Key = ASCIIEncoding::ASCII->GetBytes("Intru235");
crypto->IV = ASCIIEncoding::ASCII->GetBytes("Intru235");
CryptoStream ^cStream = gcnew
CryptoStream(fileWriter,crypto->CreateEncryptor(),CryptoStreamMode::Write);
//array<System::Byte>^ phoneBytes =
ASCIIEncoding::ASCII->GetBytes(phone);
FileStream ^input = gcnew
FileStream("contacts.dat",FileMode::Open); //Open the file to be
encrypted
int data = 0;
while((data=input->ReadByte()!=-1))
{
cStream->WriteByte((System::Byte)data);
}
input->Close();
cStream->Close();
fileWriter->Close();
System::Windows::Forms::MessageBox::Show("Data Saved");
}
Decryption Code
void InformationReader::readInformation()
{
System::String ^password = "Intru235";
FileStream ^stream = gcnew
FileStream("contacts2.dat",FileMode::Open,FileAccess::Read);
DESCryptoServiceProvider ^crypto = gcnew DESCryptoServiceProvider();
crypto->Key = ASCIIEncoding::ASCII->GetBytes(password);
crypto->IV = ASCIIEncoding::ASCII->GetBytes(password);
CryptoStream ^crypStream = gcnew
CryptoStream(stream,crypto->CreateDecryptor(),CryptoStreamMode::Read);
StreamReader ^reader = gcnew StreamReader(crypStream);
phoneNumber = reader->ReadLine();
email = reader->ReadLine();
crypStream->Close();
reader->Close();
}
I have a very big problem here. Even though my file writing thing works as
should be, the reading one is having issues. When I read things, I only
get blank Lines! I know the program has read "something" because the lines
are blank (spaces).
What am I doing wrong in this Decrypt or thing? Please help.
No comments:
Post a Comment