更改AES加密中的共享密钥

时间:2013-04-05 06:48:38

标签: .net encryption

我使用高级加密标准(AES)在将数据存储到数据库之前对其进行加密。 据我了解,如果我更改算法的“共享密钥”部分,我必须相应地更新所有存储的数据。 有没有其他方法可以让我的管理员用户有机会更新密钥,而无需在执行此操作时更新大量的存储数据?

以下是我用于加密的代码:

public static string EncryptStringAES(string plainText, string sharedSecret)
    {
        if (string.IsNullOrEmpty(plainText))
            throw new ArgumentNullException("plainText");
        if (string.IsNullOrEmpty(sharedSecret))
            throw new ArgumentNullException("sharedSecret");

        string outStr = null;                       // Encrypted string to return
        RijndaelManaged aesAlg = null;              // RijndaelManaged object used to encrypt the data.

        try
        {
            // generate the key from the shared secret and the salt
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);

            // Create a RijndaelManaged object
            aesAlg = new RijndaelManaged();
            aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);

            // Create a decryptor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                // prepend the IV
                msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
                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);
                    }
                }
                outStr = Convert.ToBase64String(msEncrypt.ToArray());
            }
        }
        finally
        {
            // Clear the RijndaelManaged object.
            if (aesAlg != null)
                aesAlg.Clear();
        }

        // Return the encrypted bytes from the memory stream.
        return outStr;
    }

2 个答案:

答案 0 :(得分:1)

如果您的数据是使用一个密钥(“密钥A”)加密的,那么更改密钥(以便可以使用例如“密钥B”解密)的唯一方法是解密 all 使用“密钥A”的数据,然后使用“密钥B”重新加密。

我认为用于避免此问题的一般技术是使用强大的“主密钥”加密数据,然后使用用户密钥加密主密钥。因此,更改密码只需要使用旧密钥和新密钥(分别)解密和重新加密主密钥,数据本身保持不变。

您没有提到您实际使用的数据库,但可能值得注意的是,许多数据库服务器都支持自动数据加密,因此存储在磁盘上的数据采用加密格式,无法解密,除非您是授权用户,否则请访问。

如果您使用的数据库服务器支持此功能,则值得进行调查。透明,您将不再需要担心在代码中手动加密/解密它,它可能也已经支持密钥更改,以及数据恢复功能(如果您的用户忘记了密码等)。

答案 1 :(得分:1)

我移植了Keyczar framework to C#,其中包含两个密钥集,允许您轮换密钥(仅使用新密钥加密,仍旧解密)和password encryption密钥集。

创建密钥集:

:> KeyczarTool.exe create --location="path_to_keyset" --purpose="crypt"
:> KeyczarTool.exe addkey --location="path_to_keyset" --status="primary" --size="256" --password
Please enter password:
Please re-enter password:

加密:

Func<string> passwordCallback = ()=> Console.ReadLine(); //whatever you need to prompt

using(var keySet = new KeySet("path_to_keyset"))
using(var pbeKeySet = new PbeKeySet(keySet, passwordCallback))
using(var encrypter = new Encrypter(pbeKeySet)){
{
     return encrypter.Encrypt(plaintext);
}

解密:

using(var keySet = new KeySet("path_to_keyset"))
using(var pbeKeySet = new PbeKeySet(keySet, passwordCallback))
using(var crypter = new Crypter(pbeKeySet)){
{
     return crypter.Encrypt(plaintext);
}

当然,如果您需要非交互式加密和解密,您应该使用keyczar的regular usage,或者如果您需要额外的分离级别,您甚至可以拥有key set to encrypt another key set