请看下面的代码
void InformationWriter::writeContacts(System::String ^phone, System::String ^email)
{
try
{
//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("contacts.dat",FileMode::OpenOrCreate,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");
}
catch (IOException ^e)
{
System::Windows::Forms::MessageBox::Show(e->Message);
}
}
执行以下部分时,出现错误
FileStream ^input = gcnew FileStream("contacts.dat",FileMode::Open); //Open the file to be encrypted
以下是我得到的错误
这是我第一次使用CryptoStream
,我也是 C++/CLI 的新手。