c#中的AES128解密

问题描述 投票:0回答:1

我只收到一个密钥和一个使用 AES 加密的值。我现在需要解密它并从中检索值。然而,我在网上找到的所有样本都使用其他属性来解密(盐,IV,...)

我在网上看到了这个网站http://aes.online-domain-tools.com/并且我能够将其“解密”为二进制文件。

但是在 C# 中这样做似乎对我不起作用。

任何人都可以为我提供一种在 C# 中仅使用加密值和密钥解密 AES_128 的简单方法吗?

编辑

  1. 从钥匙中检索 IV:

    Key = System.Text.Encoding.ASCII.GetBytes(DecryptionConstants.AES_KEY);
    using (Aes aesAlg = Aes.Create())
    {
        // Convert Key to bytes and pass into AES
        aesAlg.Key = Convert.FromBase64String(DecryptionConstants.AES_KEY);
        IV = new byte[aesAlg.BlockSize / 8];
    
        var hexKey = BitConverter.ToString(IV);
        IV = StringToByteArray("a2 26 cb 78 e2 cb 26 cb e7 c7 f0 bc c7 7b bd 9d");
    
    }
    
  2. 解密

    byte[] EncryptedBytes = Convert.FromBase64String(text);
    
    //Setup the AES provider for decrypting.            
    AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider
    {
        Key = Key,
        IV = this.IV,
        BlockSize = 128,
        KeySize = 256,
        Padding = PaddingMode.None,
        Mode = CipherMode.CBC,                
    };
    
    var cryptoTransform = aesProvider.CreateDecryptor(aesProvider.Key, aesProvider.IV);
    
    var DecryptedBytes = cryptoTransform.TransformFinalBlock(EncryptedBytes, 8, EncryptedBytes.Length-8);
    

这会产生一个字节数组,它无法返回到我放入的字符串。

c# encryption aes
1个回答
0
投票

根据您的数据,您应该猜测 IV,可能是 16 个零或数据的前 16 个字节,以及 CipherMode、ECB、CBC 等。

这是我用来加密/解密的一个小函数:

    public static byte[] EncryptAES(byte[] message, byte[] key, bool doEncryption)
    {
        try
        {
            Aes aes = new AesManaged();
            aes.Key = key;
            aes.IV = new byte[16];
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.None;

            ICryptoTransform cipher;

            if (doEncryption == true)
                cipher = aes.CreateEncryptor();
            else
                cipher = aes.CreateDecryptor();

            return cipher.TransformFinalBlock(message, 0, message.Length);
        }
        catch(Exception)
        {
            return null;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.