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

  • Home
  • SEARCH
  • 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 6121407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:46:49+00:00 2026-05-23T15:46:49+00:00

public static string GenerateKey() { AesCryptoServiceProvider aesCrypto = (AesCryptoServiceProvider)AesCryptoServiceProvider.Create(); // Use the Automatically generated

  • 0
   public static string GenerateKey()
   {
        AesCryptoServiceProvider aesCrypto = (AesCryptoServiceProvider)AesCryptoServiceProvider.Create();

        // Use the Automatically generated key for Encryption. 
        return ASCIIEncoding.ASCII.GetString(aesCrypto.Key);
    }

    static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
    {
        FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);

        FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
        AesCryptoServiceProvider AES = new AesCryptoServiceProvider();
        // Sets the appropriate block size for the AES Encryption Method
        AES.BlockSize = 128;
        AES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        AES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        ICryptoTransform aesencrypt = AES.CreateEncryptor();
        CryptoStream cryptostream = new CryptoStream(fsEncrypted, aesencrypt, CryptoStreamMode.Write);

        byte[] bytearrayinput = new byte[fsInput.Length];
        fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
        cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
        cryptostream.Close();
        fsInput.Close();
        fsEncrypted.Close();
    }

    static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)
    {
        AesCryptoServiceProvider AES = new AesCryptoServiceProvider();
        //A 64 bit key and IV is required for this provider.
        //Set secret key For DES algorithm.
        AES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        //Set initialization vector.
        AES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

        //Create a file stream to read the encrypted file back.
        FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
        //Create a DES decryptor from the DES instance.
        ICryptoTransform aesdecrypt = AES.CreateDecryptor();
        //Create crypto stream set to read and do a 
        //DES decryption transform on incoming bytes.
        CryptoStream cryptostreamDecr = new CryptoStream(fsread, aesdecrypt, CryptoStreamMode.Read);
        //Print the contents of the decrypted file.
        StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
        fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
        fsDecrypted.Flush();
        fsDecrypted.Close();
    }

I’m getting the exception listed in the title at this line in the EncryptFile method.

AES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

I set the BlockSize directly before that line, and I’m sure AES is suppose to use a 16 byte block size, so what do I do to get this working? I’m unsure as to why the code is still having problems with the block size.

Note: I’m simply trying some examples I found online, this isn’t meant to be a lock-tight AES implementation, just something I’d like to get working so I can continue to learn about the algorithm.

Thanks for any help.

  • 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-05-23T15:46:50+00:00Added an answer on May 23, 2026 at 3:46 pm

    The IV must be exactly the same size as the block size, which in the case of AES is 16 bytes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating an encoded key like this: public static String generateKey(String username) { return
I am trying to generate a key for encryption: public static final String ENCYT_ALGORITHM
I'm trying to use public static string TryGetRequestValue(this HttpRequest stringArg, int maxLengthArg) { return
public static string RatingCalculator(int input) { if (input < 10) { return string.Empty; }
public static string GetCurrentEmployeeName() { return db.Employees.SingleOrDefault( c => c.NTUserName == CurrentUsername() ).Last_First; }
Code Behind: [WebMethod] public static string emp() { return BlaBla; } Aspx Page: $(document).ready(function()
public static string filename2_1, filename2_2, filename2_3; filename2_1=xyz.jpg; filename2_2=abc.png; filename2_3=qwe.jpg; ..... ... for (int key
public static string BoldStartTag { get { return <B>; } } VS public static
public static string MakeWebSafe(this string x) { const string RegexRemove = @(<\s*script[^>]*>)|(<\s*/\s*script[^>]*>); return Regex.Replace(x,
public static string ToTrimmedString(this DataRow row, string columnName) { return row[columnName].ToString().Trim(); } Compiles fine

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.