Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 此代码容易受到oracle攻击吗?_C#_Encryption_Aes_Padding_Padding Oracle Attack - Fatal编程技术网

C# 此代码容易受到oracle攻击吗?

C# 此代码容易受到oracle攻击吗?,c#,encryption,aes,padding,padding-oracle-attack,C#,Encryption,Aes,Padding,Padding Oracle Attack,以下代码是否容易受到填充oracle攻击,因为如果填充有效或无效,它将返回(CBC、PKCS#7) 该代码直接取自微软的网页,可以在dotnetfiddle.net等在线编译器上轻松运行 using System; using System.IO; using System.Security.Cryptography; namespace RijndaelManaged_Example { class RijndaelExample { public stati

以下代码是否容易受到填充oracle攻击,因为如果填充有效或无效,它将返回(CBC、PKCS#7)

该代码直接取自微软的网页,可以在dotnetfiddle.net等在线编译器上轻松运行

using System;
using System.IO;
using System.Security.Cryptography;

namespace RijndaelManaged_Example
{
    class RijndaelExample
    {
        public static void Main()
        {
            try
            {

                string original = "Here is some data to encrypt!";

                // Create a new instance of the RijndaelManaged
                // class.  This generates a new key and initialization 
                // vector (IV).
                using (RijndaelManaged myRijndael = new RijndaelManaged())
                {

                    myRijndael.GenerateKey();
                    myRijndael.GenerateIV();
                    // Encrypt the string to an array of bytes.
                    byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

                    // Decrypt the bytes to a string.
                    string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

                    //Display the original data and the decrypted data.
                    Console.WriteLine("Original:   {0}", original);
                    Console.WriteLine("Round Trip: {0}", roundtrip);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }
        static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");
            byte[] encrypted;
            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                // Create an encryptor to perform the stream transform.
                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    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 the encrypted bytes from the memory stream.
            return encrypted;

        }

        static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

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

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    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;

        }
    }
}
使用系统;
使用System.IO;
使用System.Security.Cryptography;
名称空间RijndaelManaged_示例
{
类RijndaelexSample
{
公共静态void Main()
{
尝试
{
string original=“这里有一些要加密的数据!”;
//创建RijndaelManaged的新实例
//这将生成一个新的密钥和初始化
//载体(IV)。
使用(RijndaelManaged myRijndael=new RijndaelManaged())
{
myRijndael.GenerateKey();
myRijndael.GenerateIV();
//将字符串加密为字节数组。
byte[]encrypted=EncryptStringToBytes(原始,myRijndael.Key,myRijndael.IV);
//将字节解密为字符串。
字符串往返=DecryptStringFromBytes(加密,myRijndael.Key,myRijndael.IV);
//显示原始数据和解密数据。
WriteLine(“原件:{0}”,原件);
WriteLine(“往返:{0}”,往返);
}
}
捕获(例外e)
{
WriteLine(“错误:{0}”,e.Message);
}
}
静态字节[]加密StringToBytes(字符串明文,字节[]密钥,字节[]IV)
{
//检查参数。

如果(plainText==null | | | plainText.LengthYes,则代码易受攻击,因为.NET默认为CBC,默认情况下带有PKCS#7兼容的填充。您可以通过更改密文(密文的最后16个字节)轻松测试这一点并检查是否引发异常。请注意,填充预言器工作不需要错误条件,时间差可能已经泄漏了足够的信息

当然,这并不一定意味着使用该代码的系统易受攻击。如果该代码用于对静止数据执行加密(例如,文件加密),则很可能无法构造填充oracle,并且攻击的必要条件未得到满足

请注意,填充oracle攻击是一种特定类型的明文oracle攻击。即使使用了不同的分组密码模式,也可能会发生其他攻击。通常,您需要经过身份验证的加密以确保明文oracle不可能发生:仅在验证消息完整性和真实性后才采取行动


如图所示的代码对于传输模式安全性而言并不安全。当然,为了获得安全的传输安全性,CBC填充oracle可能只是众多漏洞中的一个;可以说,代码片段根本没有演示传输安全性。

欢迎使用密码学。您可以看看我删除了最新的add-on例如,攻击的问题代码,因为这显然与StackOverflow无关。注释不用于扩展讨论;此对话已完成。oracle是否必须访问此会话的私钥才能工作?