Hi @JohnCTX ,
I created a C++/CLR project, it uses AES 256, can encrypt and decrypt strings, you could use it to encrypt file names, the following is my code, you could refer to it.
private: array<unsigned char>^ AES_Encrypt(array<unsigned char>^ bytesToBeEncrypted, array<unsigned char>^ passwordBytes) {
array<unsigned char>^ encryptedBytes = nullptr;
array<unsigned char>^ saltBytes = gcnew array<unsigned char>(8) { 1, 2, 3, 4, 5, 6, 7, 8 };
MemoryStream^ ms = gcnew MemoryStream();
RijndaelManaged^ AES = gcnew RijndaelManaged();
AES->KeySize = 256;
AES->BlockSize = 128;
AES->Mode = CipherMode::CBC;
AES->Padding = System::Security::Cryptography::PaddingMode::Zeros;
auto key = gcnew Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES->Key = key->GetBytes(AES->KeySize / 8);
AES->IV = key->GetBytes(AES->BlockSize / 8);
auto cs = gcnew CryptoStream(ms, AES->CreateEncryptor(), CryptoStreamMode::Write);
try {
try {
cs->Write(bytesToBeEncrypted, 0, bytesToBeEncrypted->Length);
cs->Close();
}
finally {
if (cs != nullptr) delete cs;
}
encryptedBytes = ms->ToArray();
}
finally {
if (ms != nullptr) delete ms;
}
return encryptedBytes;
}
private: array<unsigned char>^ AES_Decrypt(array<unsigned char>^ bytesToBeDecrypted, array<unsigned char>^ passwordBytes) {
array<unsigned char>^ decryptedBytes = nullptr;
array<unsigned char>^ saltBytes = gcnew array<unsigned char>(8) { 1, 2, 3, 4, 5, 6, 7, 8 };
MemoryStream^ ms = gcnew MemoryStream();
RijndaelManaged^ AES = gcnew RijndaelManaged();
AES->KeySize = 256;
AES->BlockSize = 128;
AES->Mode = CipherMode::CBC;
AES->Padding = System::Security::Cryptography::PaddingMode::Zeros;
auto key = gcnew Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES->Key = key->GetBytes(AES->KeySize / 8);
AES->IV = key->GetBytes(AES->BlockSize / 8);
auto cs = gcnew CryptoStream(ms, AES->CreateDecryptor(), CryptoStreamMode::Write);
try {
try {
cs->Write(bytesToBeDecrypted, 0, bytesToBeDecrypted->Length);
cs->Close();
}
finally {
if (cs != nullptr) delete cs;
}
decryptedBytes = ms->ToArray();
}
finally {
if (ms != nullptr) delete ms;
}
return decryptedBytes;
}
//Encrypt String
private: System::String^ EncryptText(System::String^ input, System::String^ password) {
array<unsigned char>^ bytesToBeEncrypted = System::Text::Encoding::UTF8->GetBytes(input);
array<unsigned char>^ passwordBytes = System::Text::Encoding::UTF8->GetBytes(password);
passwordBytes = SHA256::Create()->ComputeHash(passwordBytes);
array<unsigned char>^ bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
System::String^ result = Convert::ToBase64String(bytesEncrypted);
return result;
}
//Decrypt String
private: System::String^ DecryptText(System::String^ input, System::String^ password) {
array<unsigned char>^ bytesToBeDecrypted = Convert::FromBase64String(input);
array<unsigned char>^ passwordBytes = System::Text::Encoding::Encoding::UTF8->GetBytes(password);
passwordBytes = SHA256::Create()->ComputeHash(passwordBytes);
array<unsigned char>^ bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
System::String^ result = System::Text::Encoding::Encoding::UTF8->GetString(bytesDecrypted);
return result;
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
System::String^ temp = EncryptText(this->textBox1->Text, "batman");
this->label1->Text = temp;
this->label2->Text = DecryptText(temp, "batman");
}
Best regards,
Elya
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.