gpt4 book ai didi

c++ - 如何解密加密文本?

转载 作者:行者123 更新时间:2023-11-28 06:52:17 30 4
gpt4 key购买 nike

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

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);

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

An unhandled exception of type'System.Security.Cryptography.CryptographicException' occurred inmscorlib.dll

Additional Information: The padding is invalid and can not be removed.

编辑 1:

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

Eccezione non gestita di tipo 'System.Security.Cryptography.CryptographicException' in >mscorlib.dllInformazioni aggiuntive: Lunghezza dei dati da decrittografare non valida.

(An unhandled exception of type 'System.Security.Cryptography.CryptographicException' >occurred in mscorlib.dllAdditional information: Length of the data to decrypt is invalid.)

最佳答案

已解决

我使用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);

关于c++ - 如何解密加密文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23725023/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com