首页 > 解决方案 > 在.Net C++中解密字符串

问题描述

我带着小问题来找你。实际上,我正在尝试制作一个能够在.NET C++ 中解密字符串的函数。这是我的函数代码:


array<Byte>^ chipherbytes;
array<Byte>^ plainbytes2;
array<Byte>^ plainkey;
SymmetricAlgorithm^ desObj;
desObj = Rijndael::Create();
chipherbytes = Encoding::ASCII->GetBytes(dataGridView1->CurrentCell->Value->ToString());
plainkey = Encoding::ASCII->GetBytes("0123456789abcdef");
desObj->Key = plainkey;
desObj->Mode = CipherMode::CBC;
desObj->Padding=PaddingMode::PKCS7;
MemoryStream^ ms1 = gcnew MemoryStream(chipherbytes);
CryptoStream^ cs1 = gcnew CryptoStream(ms1, desObj->CreateDecryptor(), CryptoStreamMode::Read);
cs1->Read(chipherbytes, 0, chipherbytes->Length); //<- in this line is problem
plainbytes2 = ms1->ToArray();
cs1->Close();
ms1->Close();
textBox1->Text = Encoding::ASCII->GetString(plainbytes2);

但是当我开始调试并且稍后我打开此功能时,程序崩溃并且正在通信“System.Security.Cryptography.CryptographicException:补码无效并且无法删除。” 它显示在标记的标签中。有人可以给我建议吗?

编辑:

这是代码的加密部分:

cipherData = textBox6->Text;
    plainbytes = Encoding::ASCII->GetBytes(cipherData);
    plainkey = Encoding::ASCII->GetBytes("0123456789abcdef");
    desObj->Key = plainkey;
    //choose other appropriate modes (CBC,CFB,CTS,ECB,OFB)
    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();
    textBox7->Text = Encoding::ASCII->GetString(chipherbytes);

顺便说一句,我想将加密数据发送到 mysql 数据库,然后从数据库下载并解密。

标签: .netencryptionframeworksc++-cli

解决方案


推荐阅读