I have a randomly generated string of about 256 characters long. This string is hashed to for a key and initialization vector for a program running on the .NET framework 4.8. Is this method secure from a professional point of view?

  • 1
    $\begingroup$ What is the size of the key, how it is generated? Not exactly a clear question and AES-256 has 256 bits of key not 256 characters. $\endgroup$
    – kelalaka
    Apr 5, 2021 at 22:12
  • $\begingroup$ The key size will become 256bit by hashing the random string into SHA256 byte array and MD5 for The initialization vector. All I Need To Know Is It Safe To Say That The string used to generate the keys will be hard to crack $\endgroup$
    – DDX5 Media
    Apr 5, 2021 at 22:15
  • $\begingroup$ Say that you have an alphabet of 64 characters (all upper and lowercase characters, digits and 2 special characters, not that many) Then each character contains 6 bits of entropy as 2^6 = 64. So your key is build from 256 * 6 = 1536 bits of entropy, which is more than enough. Then again, your key and IV are static, so if you want to encrypt multiple messages then you're better off prefixing a (usually 16 byte) random IV to the ciphertext so it can be used to initialize the cipher during decryption. $\endgroup$
    – Maarten Bodewes
    Apr 5, 2021 at 22:21
  • 1
    $\begingroup$ Please don't use MD5 ever. It's insecure and better attacks may be found. It would act as a red flag to any serious auditor of the protocol or code. Instead you might want to read up on KDF's such as HKDF and authenticated encryption. For transport mode security: use TLS. $\endgroup$
    – Maarten Bodewes
    Apr 5, 2021 at 22:23
  • $\begingroup$ Just for clarification a static IV could be used as a substitute for the MD5 hash instead, If not how would i go about generating an IV that is 128bit $\endgroup$
    – DDX5 Media
    Apr 5, 2021 at 22:36

1 Answer 1


The Following Code Is Now Using The Password Derive Bytes Function, All Values Will Be Changed Accordingly

    string Password = "Example";
    byte[] IV = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    byte[] Salt = Encoding.ASCII.getBytes("Salt Example");
    PasswordDeriveBytes pdb = new PasswordDerivedBytes(Password,Salt);
    Aes Alg = Aes.Create();
    Alg.Key = pdb.CryptDeriveKey("Aes","SHA256",256,IV);
    Alg.IV = IV;

The Code Above Is C#

  • 1
    $\begingroup$ That is not a safe way to derive a key from a password. A special "Password Hashing Function" like Argon2id is needed, not a general hash like SHA256. Also the IV is incorrect and will negate the security since it's repeated. $\endgroup$ Apr 6, 2021 at 3:34
  • $\begingroup$ So Something Like Password derive bytes in C# Would be a better alternative $\endgroup$
    – DDX5 Media
    Apr 6, 2021 at 19:57
  • $\begingroup$ Yes, though I also don't see what mode AES is being used in (AES isn't secure without a mode of operation, preferably an AEAD mode like GCM), and again you've got a fixed IV which means there's no practical security. The IV must never repeat for any given key (password) in most modes. $\endgroup$ Apr 6, 2021 at 20:13
  • $\begingroup$ I Have Not Tried Using A Random IV Before I Know AES Has A Method For That, However Does The IV Have To Be The Same For Both Encryption and Decryption $\endgroup$
    – DDX5 Media
    Apr 6, 2021 at 20:21
  • $\begingroup$ Yes, the IV must be the same for both encryption and decryption. It's not secret, and is usually sent just before the ciphertext. $\endgroup$ Apr 6, 2021 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.