【问题标题】:How to generate Key and Key IV aes in C#如何在 C# 中生成 Key 和 Key IV aes
【发布时间】:2019-10-20 21:26:56
【问题描述】:

如何生成密钥和密钥IV而不显式编写?

public sealed class MyCryptoClass
{
    protected RijndaelManaged myRijndael;

    private static string encryptionKey = "142eb4a7ab52dbfb971e18daed7056488446b4b2167cf61187f4bbc60fc9d96d";

    private static string initialisationVector ="26744a68b53dd87bb395584c00f7290a";

我尝试生成 encryptionKey 和 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;
        }
    }

【问题讨论】:

  • 不清楚你的问题是什么。 不明确写出来是什么意思?明确写什么?
  • 我想生成它们,而不是提供声明 private static string encryptionKey = "142eb4a7ab52dbfb971e18daed7056488446b4b2167cf61187f4bbc60fc9d96d"; private static string initialisationVector ="26744a68b53dd87bb395584c00f7290a"; 的密钥
  • 不想使用 Rijndael 算法的生成方法?
  • 在这里生成一个密钥using (RijndaelManaged rijAlg = new RijndaelManaged()) { rijAlg.Key = Key; rijAlg.IV = IV; ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); 但我需要随机加密密钥和初始化向量我是 AES 新手

标签: c# key aes


【解决方案1】:

让我们一步一步来,让事情变得简单。

您需要两种方法来实现您的目标。我将从加密方法开始:

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

那么我们随后需要一个 Decrypt 方法:

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

注意:最好将EncryptDecrypt 方法包装在一个类中,然后使用它们。

您可以调用如下方法:

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

【讨论】:

    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 2022-01-26
    相关资源
    最近更新 更多