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 4581734
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:58:16+00:00 2026-05-21T20:58:16+00:00

I am using the BouncyCastle.Crypto.dll for encrypt/decrypt a string in my app. I am

  • 0

I am using the “BouncyCastle.Crypto.dll” for encrypt/decrypt a string in my app. I am using the
following code from this blog:

  1. I have a class BCEngine, exactly the same as the one given in the link mentioned above.

    public class BCEngine
    {
       private readonly Encoding _encoding;
       private readonly IBlockCipher _blockCipher;
       private PaddedBufferedBlockCipher _cipher;
       private IBlockCipherPadding _padding;
    
       public BCEngine(IBlockCipher blockCipher, Encoding encoding)
       {
          _blockCipher = blockCipher;
          _encoding = encoding;
       }
    
       public void SetPadding(IBlockCipherPadding padding)
       {
           if (padding != null)
             _padding = padding;
       }
    
       public string Encrypt(string plain, string key)
       {
           byte[] result = BouncyCastleCrypto(true, _encoding.GetBytes(plain), key);
           return Convert.ToBase64String(result);
       }
    
       public string Decrypt(string cipher, string key)
       {
          byte[] result = BouncyCastleCrypto(false, Convert.FromBase64String(cipher), key);
          return _encoding.GetString(result);
       }
    
       /// <summary>
       ///
       /// </summary>
       /// <param name="forEncrypt"></param>
       /// <param name="input"></param>
       /// <param name="key"></param>
       /// <returns></returns>
       /// <exception cref="CryptoException"></exception>
       private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, string key)
       {
           try
           {
               _cipher = _padding == null ? new PaddedBufferedBlockCipher(_blockCipher) : new PaddedBufferedBlockCipher(_blockCipher, _padding);
               byte[] keyByte = _encoding.GetBytes(key);
               _cipher.Init(forEncrypt, new KeyParameter(keyByte));
               return _cipher.DoFinal(input);
           }
           catch (Org.BouncyCastle.Crypto.CryptoException ex)
           {
               throw new CryptoException(ex.Message);
           }
       }
    }
    

I am using an asp.net form in which i have written code as given below:

    public partial class EncryptionForm : System.Web.UI.Page
    {
      Encoding _encoding;
      IBlockCipherPadding _padding;
      string key = "DFGFRT";
       string textToBeEncrypted = "Original text. Please encrypt me.";
       string txtEncryptedText = string.empty;
       string txtDecryptedText = string.empty;

      protected void Page_Load(object sender, EventArgs e)
      {
          _encoding = Encoding.ASCII; 
          Pkcs7Padding pkcs = new Pkcs7Padding();
          _padding = pkcs;   
      }

      protected void btnEncrypt_Click(object sender, EventArgs e)
      {
          txtEncryptedText = AESEncryption(textToBeEncrypted, key, true);
      }

      protected void btnDecrypt_Click(object sender, EventArgs e)
      {
          txtDecryptedText = AESDecryption(txtEncryptedText.Text, key, true);
      }

      public string AESEncryption(string plain, string key, bool fips)
      {
          BCEngine bcEngine = new BCEngine(new AesEngine(), _encoding);
          bcEngine.SetPadding(_padding);
          return bcEngine.Encrypt(plain, key);
      }

      public string AESDecryption(string cipher, string key, bool fips)
      {
          BCEngine bcEngine = new BCEngine(new AesEngine(), _encoding);
          bcEngine.SetPadding(_padding);
          return bcEngine.Decrypt(cipher, key);
      }
    }

Not sure, but due to some reason, I get an exception when I call the btnEncrypt_Click

“Key length not 128/192/256 bits.”

Can anybody please guide? I am a complete newbie to this.
Thanks in Advance.

  • 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-21T20:58:17+00:00Added an answer on May 21, 2026 at 8:58 pm

    Your string key = "DFGFRT"; is not 128/192/256 bits.

    DFGFRT is 6 characters, which is 6 (or 12?) bytes = 8*12 = 96 bits (at most).

    To get a 128 bit key you need a 16 byte string, so I’d go on the safe side and use a 16 character string so it will be a 128 bit key if using single byte characters and 256 if using wide characters.

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

Sidebar

Related Questions

How do encrypt/decrypt a long or int using the Bouncy Castle crypto routines for
This is the code I'm currently using. It uses the BouncyCastle Provider. static {
I am trying to write a small application using bouncycastle algorithm, from the BouncyCastleProvider.java
I am trying to use BouncyCastle to encrypt a file using the PKCS 7
I'm trying to debug & extend an existing piece of Java code using BouncyCastle
Using jQuery's UI Tabs. This is my code. <div id=tabs> <ul> <li><a href=#tabs-1>Find a
I need to encrypt a stream with pgp using the bouncycastle provider. All of
Using online interfaces to a version control system is a nice way to have
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C#, I need a class called User that has a username, password, active

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.