Search code examples
c#encryptionkeyaes

AES Key generation (C#)


i'm working on this project that uses AES encryption. The encryption / decryption part is all done, but I can't get the random AES key generator to work.. I tried multiple things, but I can't get them to work.. Help would be appreciated

Regards, CmplDev


Solution

  • Generate a random string or numbers. Following function modified from MSDN return a random key in a specific length:

    private static string CreateSalt(int size)
    {
        //Generate a cryptographic random number.
        using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
        {
            byte[] buff = new byte[size];
            rng.GetBytes(buff);
    
            // Return a Base64 string representation of the random number.
            return Convert.ToBase64String(buff);
        }
    }