如何解密加密文本?

时间:2014-05-18 17:47:20

标签: c++ visual-studio-2010 c++-cli

我找到了允许我加密和解密文本的代码:

cipherData = textBox2->Text;
plainbytes = Encoding::ASCII->GetBytes(cipherData);

plainKey = Encoding::ASCII->GetBytes("0123456789abcdef");

desObj->Key = plainKey;

desObj->Mode = CipherMode::CBC;

desObj->Padding = PaddingMode::PKCS7;

MemoryStream^ ms = gcnew MemoryStream();
CryptoStream^ cs = gcnew CryptoStream(ms,desObj->CreateEncryptor(),CryptoStreamMode::Write);

cs->Write(plainbytes,0,plainbytes->Length);
cs->Close();

chipherbytes = ms->ToArray();
ms->Close();

textBox3->Text = Encoding::ASCII->GetString(chipherbytes);

//decripto

MemoryStream^ ms1 = gcnew MemoryStream(chipherbytes);
CryptoStream^ cs1 = gcnew CryptoStream(ms1,desObj->CreateDecryptor(),CryptoStreamMode::Read);

cs1->Read(chipherbytes,0,chipherbytes->Length);
plainbytes2 = ms1->ToArray();
cs1->Close();
ms1->Close();

textBox4->Text = Encoding::ASCII->GetString(plainbytes2);

它很完美,效果很好。问题是我希望解密以前的加密文本,从ASCII开始而不是从MemoryStream开始。

我用这种方式尝试了代码:

cipherData = textBox2->Text;
plainbytes = Encoding::ASCII->GetBytes(cipherData);

plainKey = Encoding::ASCII->GetBytes("0123456789abcdef");

desObj->Key = plainKey;

desObj->Mode = CipherMode::CBC;

desObj->Padding = PaddingMode::PKCS7;

MemoryStream^ ms = gcnew MemoryStream();
CryptoStream^ cs = gcnew CryptoStream(ms,desObj->CreateEncryptor(),CryptoStreamMode::Write);

cs->Write(plainbytes,0,plainbytes->Length);
cs->Close();

chipherbytes = ms->ToArray();
ms->Close();

textBox3->Text = Encoding::ASCII->GetString(chipherbytes);

//DECRYPTION CODE


cipherData = textBox3->Text;
chipherbytes = Encoding::ASCII->GetBytes(cipherData);

MemoryStream^ ms1 = gcnew MemoryStream(chipherbytes);
CryptoStream^ cs1 = gcnew CryptoStream(ms1,desObj->CreateDecryptor(),CryptoStreamMode::Read);

cs1->Read(chipherbytes,0,chipherbytes->Length);
plainbytes2 = ms1->ToArray();
cs1->Close();
ms1->Close();

textBox4->Text = Encoding::ASCII->GetString(plainbytes2);

但是当我尝试代码时,我收到了这个错误:

  

未处理的类型异常   发生'System.Security.Cryptography.CryptographicException'   mscorlib.dll

     

附加信息:填充无效,无法删除。

编辑1:

我关闭了wordwrap属性,现在我收到一个新错误:

  

> mscorlib.dll中的'eccezione non gestita di tipo'System.Security.Cryptography.CryptographicException'   Informazioni aggiuntive:Lunghezza dei dati da decrittografare non valida。

     

(mscorlib.dll中发生未处理的“System.Security.Cryptography.CryptographicException”类型的例外情况>   附加信息:要解密的数据长度无效。)

1 个答案:

答案 0 :(得分:1)

解决

我使用System :: Convert :: ToBase64String将加密文本正确写入文本框,避免数据丢失;然后我使用System :: Convert :: FromBase64String

从文本框加载文本
         //ENCRYPTION CODE

         cipherData = textBox2->Text;
         plainbytes = Encoding::Unicode->GetBytes(cipherData);

         plainKey = Encoding::Unicode->GetBytes("0123456789abcdef");

         desObj->Key = plainKey;

         desObj->Mode = CipherMode::CBC;

         desObj->Padding = PaddingMode::PKCS7;

         MemoryStream^ ms = gcnew MemoryStream();
         CryptoStream^ cs = gcnew CryptoStream(ms,desObj->CreateEncryptor(),CryptoStreamMode::Write);

         cs->Write(plainbytes,0,plainbytes->Length);
         cs->Close();

         chipherbytes = ms->ToArray();
         ms->Close();

         textBox3->Text = System::Convert::ToBase64String(chipherbytes);

         //DECRYPTION CODE


         chipherbytes = System::Convert::FromBase64String(textBox3->Text);

         MemoryStream^ ms1 = gcnew MemoryStream(chipherbytes);
         CryptoStream^ cs1 = gcnew CryptoStream(ms1,desObj->CreateDecryptor(),CryptoStreamMode::Read);

         cs1->Read(chipherbytes,0,chipherbytes->Length);
         plainbytes2 = ms1->ToArray();
         cs1->Close();
         ms1->Close();

         textBox4->Text = Encoding::Unicode->GetString(plainbytes2);