Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:12:12+00:00 2026-06-07T00:12:12+00:00

I don’t know what I’m doing wrong but I’ve been trying to get this

  • 0

I don’t know what I’m doing wrong but I’ve been trying to get this thing working for about 4 hours now and I just can’t get it to work… this just gives me the error: “Please suppy a correct password” when I try to decrypt.
Encryption seems to work fine though.

Any suggestions? :<

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Security;
using AesApp.Rijndael;
using System.Linq;

    internal class FileEncryption
        {
            private static string password = pw;

            internal static void Encrypt(string inputfile, string outputfile)
            {
                byte[] encryptedPassword;

                // Create a new instance of the RijndaelManaged
                // class.  This generates a new key and initialization
                // vector (IV).
                using (var algorithm = new RijndaelManaged())
                {
                    algorithm.KeySize = 256;
                    algorithm.BlockSize = 128;

                    // Encrypt the string to an array of bytes.
                    encryptedPassword = Cryptology.EncryptStringToBytes(
                        password, algorithm.Key, algorithm.IV);
                }

                string chars = encryptedPassword.Aggregate(string.Empty, (current, b) => current + b.ToString());
                Cryptology.EncryptFile(@inputfile, @outputfile, chars);
            }

            internal static void Decrypt(string @inputfile, string @outputfile)
            {
                byte[] encryptedPassword;

                // Create a new instance of the RijndaelManaged
                // class.  This generates a new key and initialization
                // vector (IV).
                using (var algorithm = new RijndaelManaged())
                {
                    algorithm.KeySize = 256;
                    algorithm.BlockSize = 128;

                    // Encrypt the string to an array of bytes.
                    encryptedPassword = Cryptology.EncryptStringToBytes(
                        password, algorithm.Key, algorithm.IV);
                }

                string chars = encryptedPassword.Aggregate(string.Empty, (current, b) => current + b.ToString());
                Cryptology.DecryptFile(@inputfile, @outputfile, chars);
            }
        }

Reindael.cs

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);
                    }
                }
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:12:13+00:00Added an answer on June 7, 2026 at 12:12 am

    Not entirely sure, but I think I remember a time when 2 successive calls to encryption gave 2 different results.
    Therefore, your two successive calls to EncryptStringToBytes may give 2 different passwords : one for encryption and one for decryption… and this cause the failure.

    I am not sure these encryptions are necessary… if you have a password hardcoded, it is always possible to anyone to generate other strings that depend on nothing else. You should use this password directly, instead of crypting it a first time:

     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);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know if the title describes anything about what I'm trying to say but
Don't know how to explain it better but i'm trying to get a response
I don't know if this has been asked before, but what i'd like to
Don't need to do this right now but thinking about the future... What would
I don't know why, but this code worked for me a month ago... maybe
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
I don't know if this question is trivial or not. But after a couple
Don't know if this is the right place to ask this, but I will
Don't know why this is happening, but after submitting a form via JS (using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.