R
Ralph Moritz
Hi group,
I'm trying to encrypt & then decrypt a string using symmetric
encryption. The encryption seems to work fine, but when trying to
decrypt, I get a System.IndexOutOfRangeException. Please would someone
be kind enough to point out my mistake? Please see my code (C++/CLI)
below.
TIA,
Ralph
System::String^ Quantum::Util::EncryptString(System::String^ str)
{
using namespace System::IO;
using namespace System::Security::Cryptography;
MemoryStream^ ms = gcnew MemoryStream;
RijndaelManaged^ rm = gcnew RijndaelManaged;
CryptoStream^ cs = gcnew CryptoStream(ms, rm->CreateEncryptor(m_key, m_iv),
CryptoStreamMode::Write);
StreamWriter^ sw = gcnew StreamWriter(cs);
try {
sw->Write(str);
} finally {
sw->Close();
cs->Close();
ms->Close();
}
return System::Convert::ToBase64String(ms->ToArray());
}
System::String^ Quantum::Util:ecryptString(System::String^ str)
{
using namespace System::IO;
using namespace System::Security::Cryptography;
MemoryStream^ ms = gcnew MemoryStream;
RijndaelManaged^ rm = gcnew RijndaelManaged;
CryptoStream^ cs = gcnew CryptoStream(ms, rm->CreateDecryptor(m_key, m_iv),
CryptoStreamMode::Read);
StreamWriter^ sw = gcnew StreamWriter(ms);
StreamReader^ sr = gcnew StreamReader(cs);
System::String^ cleartext;
try {
array<System::Byte>^ bytes = System::Convert::FromBase64String(str);
sw->Write(bytes);
cleartext = sr->ReadToEnd();
} finally {
sw->Close();
sr->Close();
cs->Close();
ms->Close();
}
return cleartext;
}
I'm trying to encrypt & then decrypt a string using symmetric
encryption. The encryption seems to work fine, but when trying to
decrypt, I get a System.IndexOutOfRangeException. Please would someone
be kind enough to point out my mistake? Please see my code (C++/CLI)
below.
TIA,
Ralph
System::String^ Quantum::Util::EncryptString(System::String^ str)
{
using namespace System::IO;
using namespace System::Security::Cryptography;
MemoryStream^ ms = gcnew MemoryStream;
RijndaelManaged^ rm = gcnew RijndaelManaged;
CryptoStream^ cs = gcnew CryptoStream(ms, rm->CreateEncryptor(m_key, m_iv),
CryptoStreamMode::Write);
StreamWriter^ sw = gcnew StreamWriter(cs);
try {
sw->Write(str);
} finally {
sw->Close();
cs->Close();
ms->Close();
}
return System::Convert::ToBase64String(ms->ToArray());
}
System::String^ Quantum::Util:ecryptString(System::String^ str)
{
using namespace System::IO;
using namespace System::Security::Cryptography;
MemoryStream^ ms = gcnew MemoryStream;
RijndaelManaged^ rm = gcnew RijndaelManaged;
CryptoStream^ cs = gcnew CryptoStream(ms, rm->CreateDecryptor(m_key, m_iv),
CryptoStreamMode::Read);
StreamWriter^ sw = gcnew StreamWriter(ms);
StreamReader^ sr = gcnew StreamReader(cs);
System::String^ cleartext;
try {
array<System::Byte>^ bytes = System::Convert::FromBase64String(str);
sw->Write(bytes);
cleartext = sr->ReadToEnd();
} finally {
sw->Close();
sr->Close();
cs->Close();
ms->Close();
}
return cleartext;
}