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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:44:47+00:00 2026-06-10T23:44:47+00:00

To start off, I am still learning object oriented programming. Ok, I have a

  • 0

To start off, I am still learning object oriented programming. Ok, I have a combo box with the different types of symmetric algorithms.

private void Form3_Load(object sender, EventArgs e)
{
    openencrypt();
    comboBox1.Items.Add("AES");
    comboBox1.Items.Add("DES");
    comboBox1.Items.Add("Rijndael");
    comboBox1.Items.Add("RC2");
    comboBox1.Items.Add("Triple DES");
    comboBox1.SelectedIndex = 0;
}

Then I have my encrypt function checking what types they are.

byte[] hpass;
string nFilepath = Set.nfilepath;
FileStream Open = new FileStream(oFilepath, FileMode.Open, FileAccess.Read);
FileStream Save = new FileStream(nFilepath, FileMode.OpenOrCreate, FileAccess.Write);
SHA512 sh512 = new SHA512Managed();
hpass = sh512.ComputeHash(Encoding.ASCII.GetBytes(textBox1.Text));
PasswordDeriveBytes pdb = new PasswordDeriveBytes(hpass, hash);

if (comboBox1.SelectedIndex.Equals(0))
{
    Aes alg = Aes.Create();
    alg.Key = pdb.GetBytes(32);
    alg.IV = pdb.GetBytes(16);
}
if (comboBox1.SelectedIndex.Equals(1))
{
    DES alg = DES.Create();
    alg.Key = pdb.GetBytes(32);
    alg.IV = pdb.GetBytes(16);
}
if (comboBox1.SelectedIndex.Equals(2))
{
    Rijndael alg = Rijndael.Create();
    alg.Key = pdb.GetBytes(32);
    alg.IV = pdb.GetBytes(16);
}

But when I don’t want to have to put a crypto stream in each if statement. So is there a way to offload the checking to a function and return a symmetricalgorithm type? With the Key and IV? Am I going about this completely wrong?## Heading ##

  • 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-10T23:44:48+00:00Added an answer on June 10, 2026 at 11:44 pm

    A more object oriented approach would be:

    Create an algorithm interface to be displayed in your combo box:

    public interface IAlgorithmItem
    {
        SymmetricAlgorithm CreateAlgorithm();
    
        string DisplayName { get; }
    }
    

    Then, create a new class for each desired algorithm:

    public class AesAlgorithm : IAlgorithmItem
    {
        public AesAlgorithm()
        {
        }
    
        public SymmetricAlgorithm CreateAlgorithm()
        {
            return Aes.Create();
        }
    
        public string DisplayName
        {
            get { return "AES"; }
        }
    }
    
    public class RijndaelAlgorithm : IAlgorithmItem
    {
        public SymmetricAlgorithm CreateAlgorithm()
        {
            return Rijndael.Create(); 
        }
    
        public string DisplayName
        {
            get { return "Rijndael"; }
        }
    }
    
    // ...
    

    Then, you can create a new list of items:

    var listItems = new List<IAlgorithmItem>() { new AesAlgorithm(), new RijndaelAlgorithm() };
    

    Then you can bind your combo box to this list:

    comboBox1.DataSource = listItems;
    comboBox1.DisplayMember = "DisplayName";
    

    Later, you can reference the selected item:

    var algorithmItem = (IAlgorithmItem)comboBox1.SelectedItem;
    var algorithm = algorithmItem.CreateAlgorithm();
    

    EDIT: Updated with Will’s suggestion of using an interface rather than an abstract base class.
    EDIT 2: Updated to use a create method rather than property, as the result of the operation will create a new algorithm each time it is accessed.

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

Sidebar

Related Questions

I'll start off with a solid example: I have a function that generates hashes
First, I'll start off by saying that I do not have control over the
I have a peculiar issue I'm dealing with. I'll start off vague and if
I have a few git projects where I clone them and start off with
I have developed a small application based on Play Framework (I am still learning).
Lets start off with a list of strings that will be used to filter
To start off, here is the JSON response that I've been working on: {
to start off, I know C++, C#, Python, some Ruby, and basic Javascript. Anyway,
Let me first start off, sorry for the confusing title. I didn't know how
Ok so to start off, I'm not using any sort of web service. Right

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.