Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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# Rijndael加密问题_C#_Encryption_Rijndael - Fatal编程技术网

C# Rijndael加密问题

C# Rijndael加密问题,c#,encryption,rijndael,C#,Encryption,Rijndael,我不知道我做错了什么,但我已经试着让它工作了大约4个小时了,我就是不能让它工作。。。这只是给了我一个错误:“请提供一个正确的密码”当我试图解密。 不过,加密似乎工作正常 有什么建议吗 不完全确定,但我想我记得有一段时间,两次连续的加密调用给出了两个不同的结果。 因此,连续两次调用EncryptStringToBytes可能会给出两个不同的密码:一个用于加密,一个用于解密。。。这就是失败的原因 我不确定这些加密是否必要。。。如果您有一个硬编码的密码,任何人都可以生成其他不依赖任何其他内容的字符串。

我不知道我做错了什么,但我已经试着让它工作了大约4个小时了,我就是不能让它工作。。。这只是给了我一个错误:“请提供一个正确的密码”当我试图解密。 不过,加密似乎工作正常


有什么建议吗 不完全确定,但我想我记得有一段时间,两次连续的加密调用给出了两个不同的结果。 因此,连续两次调用EncryptStringToBytes可能会给出两个不同的密码:一个用于加密,一个用于解密。。。这就是失败的原因

我不确定这些加密是否必要。。。如果您有一个硬编码的密码,任何人都可以生成其他不依赖任何其他内容的字符串。您应该直接使用此密码,而不是第一次对其进行加密:

 internal static void Encrypt(string inputfile, string outputfile)
 {
     Cryptology.EncryptFile(inputfile, outputfile, password);
 }

 internal static void Decrypt(string inputfile, string outputfile)
 {
     Cryptology.DecryptFile(inputfile, outputfile, password);
 }
  • 您的加密函数似乎与解密函数完全相同
  • 另外,为什么要将所有字节转换为字符串并连接它们?这种转变是不可逆的

  • Aggregate()函数是原因。每次运行应用程序时,它都会创建不同的值。

    您好。。。只是想问一下《时代》:你哪里会出错?什么有效,什么无效?文件加密?字节加密?这么多的代码很难阅读,请缩小您的请求范围。很抱歉,没有提到,加密似乎可以正常工作,生成外观合适的文件,但在解密时我遇到了这个错误为什么您要在
    inputfile
    outputfile
    前面加上
    @
    这样的前缀?因此,给定密钥和iv,调用DecryptStringFromBytes(EncryptStringToBytes(“foo”,key,iv),key,iv)失败?这很有趣-我们实际上正在尝试修复从internet抓取的一段代码,而不可能理解OP中正在发生的事情。。。移动到Close好的,它与此配合使用^^谢谢!很抱歉我不能理解,我已经很久没睡觉了,而且我对c很陌生#
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    
        namespace AesApp.Rijndael
        {
            internal sealed class Cryptology
            {
                private const string Salt = "d5fg4df5sg4ds5fg45sdfg4";
                private const int SizeOfBuffer = 1024 * 8;
    
                internal 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("key");
                    }
    
                    byte[] encrypted;
                    // Create an RijndaelManaged object
                    // with the specified key and IV.
                    using (var rijAlg = new RijndaelManaged())
                    {
                        rijAlg.Key = key;
                        rijAlg.IV = iv;
    
                        // Create a decrytor to perform the stream transform.
                        ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
    
                        // Create the streams used for encryption.
                        using (var msEncrypt = new MemoryStream())
                        {
                            using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                            {
                                using (var 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;
    
                }
    
                internal 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("key");
    
                    // Declare the string used to hold
                    // the decrypted text.
                    string plaintext;
    
                    // Create an RijndaelManaged object
                    // with the specified key and IV.
                    using (var rijAlg = new RijndaelManaged())
                    {
                        rijAlg.Key = key;
                        rijAlg.IV = iv;
    
                        // Create a decrytor to perform the stream transform.
                        ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
    
                        // Create the streams used for decryption.
                        using (var msDecrypt = new MemoryStream(cipherText))
                        {
                            using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                            {
                                using (var srDecrypt = new StreamReader(csDecrypt))
                                {
                                    // Read the decrypted bytes from the decrypting stream
                                    // and place them in a string.
                                    plaintext = srDecrypt.ReadToEnd();
                                }
                            }
                        }
    
                    }
                    return plaintext;
                }
    
                internal static void EncryptFile(string inputPath, string outputPath, string password)
                {
                    var input = new FileStream(inputPath, FileMode.Open, FileAccess.Read);
                    var output = new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write);
    
                    // Essentially, if you want to use RijndaelManaged as AES you need to make sure that:
                    // 1.The block size is set to 128 bits
                    // 2.You are not using CFB mode, or if you are the feedback size is also 128 bits
    
                    var algorithm = new RijndaelManaged { KeySize = 256, BlockSize = 128 };
                    var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(Salt));
    
                    algorithm.Key = key.GetBytes(algorithm.KeySize / 8);
                    algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);
    
                    using (var encryptedStream = new CryptoStream(output, algorithm.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        CopyStream(input, encryptedStream);
                    }
                }
    
                internal static void DecryptFile(string inputPath, string outputPath, string password)
                {
                    var input = new FileStream(inputPath, FileMode.Open, FileAccess.Read);
                    var output = new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write);
    
                    // Essentially, if you want to use RijndaelManaged as AES you need to make sure that:
                    // 1.The block size is set to 128 bits
                    // 2.You are not using CFB mode, or if you are the feedback size is also 128 bits
                    var algorithm = new RijndaelManaged { KeySize = 256, BlockSize = 128 };
                    var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(Salt));
    
                    algorithm.Key = key.GetBytes(algorithm.KeySize / 8);
                    algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);
    
                    try
                    {
                        using (var decryptedStream = new CryptoStream(output, algorithm.CreateDecryptor(), CryptoStreamMode.Write))
                        {
                            CopyStream(input, decryptedStream);
                        }
                    }
                    catch (CryptographicException)
                    {
                        throw new InvalidDataException("Please suppy a correct password");
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
    
                private static void CopyStream(Stream input, Stream output)
                {
                    using (output)
                    using (input)
                    {
                        byte[] buffer = new byte[SizeOfBuffer];
                        int read;
                        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            output.Write(buffer, 0, read);
                        }
                    }
                }
            }
        }
    
     internal static void Encrypt(string inputfile, string outputfile)
     {
         Cryptology.EncryptFile(inputfile, outputfile, password);
     }
    
     internal static void Decrypt(string inputfile, string outputfile)
     {
         Cryptology.DecryptFile(inputfile, outputfile, password);
     }
    
    string chars = encryptedPassword.Aggregate(string.Empty, (current, b) => current + b.ToString());
                Cryptology.EncryptFile(@inputfile, @outputfile, chars);