简体   繁体   中英

How to generate Key and Key IV aes in C#

how to generate a key and key IV and not write them explicitly?

public sealed class MyCryptoClass
{
    protected RijndaelManaged myRijndael;

    private static string encryptionKey = "142eb4a7ab52dbfb971e18daed7056488446b4b2167cf61187f4bbc60fc9d96d";

    private static string initialisationVector ="26744a68b53dd87bb395584c00f7290a";

I try generate encryptionKey and initialisationVector

    protected static readonly MyCryptoClass _instance = new MyCryptoClass();
    public static MyCryptoClass Instance
    {
        get { return _instance; }
    }
        public string EncryptText(string plainText)
    {
        using (myRijndael = new RijndaelManaged())
        {

            myRijndael.Key = HexStringToByte(encryptionKey);
            myRijndael.IV = HexStringToByte(initialisationVector);
            myRijndael.Mode = CipherMode.CBC;
            myRijndael.Padding = PaddingMode.PKCS7;

            byte[] encrypted = EncryptStringToBytes(plainText, myRijndael.Key, myRijndael.IV);
            string encString = Convert.ToBase64String(encrypted);

            return encString;
        }
    }

Let's do it step by step to keep things simple.

You need two methods to achieve your goal. I'll start with encryption method:

static byte[] Encrypt(string input, byte[] Key, byte[] IV)
{
    byte[] encryptedBytes;

    using (RijndaelManaged rijndael = new RijndaelManaged())
    {
        rijndael.Key = Key;
        rijndael.IV = IV;

        ICryptoTransform encryptor = rijndael.CreateEncryptor(rijndael.Key, rijndael.IV);

        using (var memoryStream = new MemoryStream())
        {
            using (var cryptoStream = new CryptoStream(memoryStream,
                    encryptor, CryptoStreamMode.Write))
            {
                using (var streamWriter = new StreamWriter(cryptoStream))
                {
                    streamWriter.Write(input);
                }
                encryptedBytes = memoryStream.ToArray();
            }
        }
    }
    return encryptedBytes;
}

Then we need a Decrypt method subsequently:

static string Decrypt(byte[] cipher, byte[] Key, byte[] IV)
{
    string plaintext = null;

    using (RijndaelManaged rijndael = new RijndaelManaged())
    {
        rijndael.Key = Key;
        rijndael.IV = IV;

        ICryptoTransform decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV);

        using (var memoryStream = new MemoryStream(cipher))
        {
            using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
            {
                using (var streamReader = new StreamReader(cryptoStream))
                {
                    plaintext = streamReader.ReadToEnd();
                }
            }
        }

    }

    return plaintext;
}

Note: It's better to wrap Encrypt and Decrypt methods in a class and then use them.

You can call the methods like below:

string original = "This is what would be encrypted!";

using (RijndaelManaged myRijndael = new RijndaelManaged())
{

    myRijndael.GenerateKey(); // this line generates key
    myRijndael.GenerateIV(); // this line generates initialization vektor

    // This line returns encrypted text
    byte[] encryptedBytes = Encrypt(original, myRijndael.Key, myRijndael.IV);

    // You can decrypt the encrypted text like so
    string decryptedString = Decrypt(encryptedBytes, myRijndael.Key, myRijndael.IV);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM