Creating an AES 256 block cipher

JohnCTX 636 Reputation points
2021-10-28T01:11:28.73+00:00

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++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,544 questions
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,281 Reputation points Microsoft Vendor
    2021-10-29T09:08:59.58+00:00

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

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful