首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正确使用AesCryptoServiceProvider

正确使用AesCryptoServiceProvider
EN

Stack Overflow用户
提问于 2019-07-02 21:22:20
回答 1查看 508关注 0票数 2

我想写一些直接的加密程序。以下是我在搜索网络的基础上得出的结论。

代码语言:javascript
复制
public string Encrypt(string plainText)
{
    byte[] encrypted;

    // Create an AesCryptoServiceProvider object
    // with the specified key and IV.
    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
    {
        // Create an encryptor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            msEncrypt.WriteByte((byte)aesAlg.Key.Length);
            msEncrypt.Write(aesAlg.Key, 0, aesAlg.Key.Length);
            msEncrypt.WriteByte((byte)aesAlg.IV.Length);
            msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);

            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {
                    //Write all data to the stream.
                    swEncrypt.Write(plainText);
                }
                encrypted = msEncrypt.ToArray();
            }
        }
    }
    return Convert.ToBase64String(encrypted);
}

public string Decrypt(string cipherText)
{
    string plaintext = null;

    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
    {
        // Create the streams used for decryption.
        using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(cipherText)))
        {
            int l = msDecrypt.ReadByte();
            byte[] key = new byte[l];
            msDecrypt.Read(key, 0, l);
            l = msDecrypt.ReadByte();
            byte[] IV = new byte[l];
            msDecrypt.Read(IV, 0, l);

            // Create a decryptor to perform the stream transform.
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(key, IV);

            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
            {

                // Read the decrypted bytes from the decrypting stream
                // and place them in a string.
                plaintext = srDecrypt.ReadToEnd();
            }
        }
    }
    return plaintext;
}

两个问题:

  • 首先,我发现的大多数示例都硬编码了KeyIV。所以我要做的是把它写成加密的字节。这将使我的加密数据更大。有更好的办法吗?
  • 而且,我不使用任何密码。会使用密码来生成自定义的Key吗?如果是这样的话,我怎么知道这把钥匙需要多长时间?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-02 21:47:33

首先,我发现的大多数例子都是硬编码密钥和IV,所以我要做的就是把它写成加密的字节。这将使我的加密数据更大。有更好的办法吗?

显然,您不应该为不受保护的流编写密钥,因为密钥需要预先共享或建立,并且保持秘密。这种秘密密钥共享可以通过多种方式实现,从密钥协议到密钥派生、棘轮等。

而且,我不使用任何密码。人们会使用密码来生成自定义密钥吗?如果是这样的话,我怎么知道这把钥匙需要多长时间?

这是可能的。但是,提醒自己密码通常不是很强,所以如果可以避免基于密码的加密(PBE),那么这样做可能是个好主意。

如果从密码派生密钥,则应使用基于密码的密钥派生函数(有时也称为密码哈希)。在C#中,有一个名为Rfc2898DeriveBytes的PBKDF2 (严重的)实现。到现在为止,这也不是最先进的技术,但是应该足够了--如果您设置了足够高的迭代计数。

当你从一个人记住的密码中得到一个密钥时,128位就足够了。找到密钥几乎不可能比用于派生密钥的密码更容易。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56860188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档