question

JohnCTX-6479 avatar image
0 Votes"
JohnCTX-6479 asked YujianYao-MSFT answered

Creating an AES 256 block cipher

My NAS Cloud drive in amid of being reformatted. What I want to perform, afterwards, is to create an AES-256 block cipher.

It would be either a folder or an icon, which enables to prompt me to sign in my own created credentials using AES-256 encryption standard.

How would I perform this properly by using the right algorithms?

Regards,

JohnCTX

c++
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JohnCTX-6479,

Could you please tell me what type of project you created?

0 Votes 0 ·

I am in the middle of creating an AES-256 folder structure using C++.
It would then be used within my now formatted NAS cloud drive.

Its purpose is to restrict any outside users other than me to access my content.

This may be a legal alternative to a Virtual Private Network or VPN service.

0 Votes 0 ·

1 Answer

YujianYao-MSFT avatar image
0 Votes"
YujianYao-MSFT answered

Hi @JohnCTX-6479,

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

144955-sec.png

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.





sec.png (4.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.