在C#中使用AES进行确定性加密

问题描述 投票:-2回答:1

我需要一个确定性加密算法,我可以使用社会安全号码,我将加密存储在MongoDB中,并需要能够搜索它们。

我想出的是使用AES,但有目的地使用不变的IV进行gimp。我只是想确保我没有完全破坏加密,只做我需要做的事情来启用搜索。

总而言之,如果您使用如下加密的SSN转储数据库,您是否能够在不知道IV和密钥的情况下通过某种攻击获取明文?如果我通过IV(而不是硬编码)这​​将作为第二个键,它会有帮助吗?

这是我的other question的后续行动

public class DeterministicAes
{
    //random 16 byte iv
    private static readonly string DeterministicIv = "YO9FhYEIpGd28mJNupCjvg==";

    public static string SimpleEncryptWithPassword(string secretMessage, string password)
    {
        if (String.IsNullOrEmpty(secretMessage))
            throw new ArgumentException("Secret Message Required!", "secretMessage");
        var key = Convert.FromBase64String(password);
        var iv = Convert.FromBase64String(DeterministicIv);
        var cipherText = EncryptStringToBytes_Aes(secretMessage, key, iv);
        return Convert.ToBase64String(cipherText);
    }

    public static string SimpleDecryptWithPassword(string cipherText, string password)
    {
        if (String.IsNullOrEmpty(cipherText))
            throw new ArgumentException("Secret Message Required!", "cipherText");
        var cipherTextBytes = Convert.FromBase64String(cipherText);
        var key = Convert.FromBase64String(password);
        var iv = Convert.FromBase64String(DeterministicIv);
        return DecryptStringFromBytes_Aes(cipherTextBytes, key, iv);
    }

    //Credit: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=netframework-4.7.2#examples
    static byte[] EncryptStringToBytes_Aes(string plainText, byte[] key, byte[] iv) {}

    static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV) {}
}
c# encryption aes
1个回答
0
投票

按照建议与AES-SIV一起去,并在Crypto sub-site上进行了善意的解释

© www.soinside.com 2019 - 2024. All rights reserved.