How can I write 3 encryption methods in one form in C# with combobox?

Melissa 1 Reputation point
2022-10-18T19:36:54.653+00:00

encryption methods are the CAESAR ENCRYPTION ALGORITHM, VIGENERE CRYPTO, SUBSTITUTION CIPHER

JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
912 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,503 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rijwan Ansari 746 Reputation points MVP
    2022-10-19T12:15:00.037+00:00

    Hi

    You can do it in various way.

    Example:

    if (comboBox1.Text == "CAESAR ENCRYPTION ALGORITHM")  
    {  
       EncryptUsingCaesar(Text, Key);  
    }  
    else if (comboBox1.Text == "VIGENERE CRYPTO")  
    {  
        EncryptUsingVigenere(Text, Key);  
    }  
    else if (comboBox1.Text == "SUBSTITUTION CIPHER")  
    {  
     EncryptUsingSubstitution(Text, Key);  
    }  
    

    Then you can separate or use same class to define encryption methods.
    Example

     Public String EncryptUsingCaesar(Text, Key)  
    {  
    //code  
    }  
      
     Public String EncryptUsingVigenere(Text, Key)  
    {  
    // code  
    }  
      
     Public String EncryptUsingSubstitution(Text, Key);  
    {  
    // code  
    }  
    
    1 person found this answer helpful.
    0 comments No comments

  2. Michael Taylor 50,111 Reputation points
    2022-10-18T21:45:37.547+00:00

    Can you be more specific about what you're asking for? You mention a combobox but that just displays a set of items. If you want to show a list of encryption algorithms then you could either store them directly in the combo (via the designer) or put them into an array or something.

    If you want the user to be able to select an encryption algorithm and then encrypt data then you might instead store the available algorithms in a collection, set the combo to display that collection of items and then, when it is time to encrypt, retrieve the selected algorithm from the combo using the standard SelectedItem or SelectedValue property.

    As for generalizing it, that depends upon where your algorithms are coming from. If you're using the built in algorithms and they are all symmetric/asymetric then there is already a base type that you can cast all the algorithms to. However if you're mixing algorithms (such that each needs a different setup) or you're creating your own then you'll want to introduce an interface that exposes the functionality your app needs. Then create a wrapper type for each supported algorithm that implements your interface.

       public interface IEncryptionAlgorithm  
       {  
          string Name { get; }  
         
          byte[] Encrypt ( string value );  
          string Decrypt ( byte[] value );  
       }  
         
       public class MySimpleEncryptionAlgorithm : IEncryptionAlgorithm  
       {  
          ...  
       }  
         
       public class MyComplexEncryptionAlgorithm : IEncryptionAlgorithm  
       {  
          ...  
       }  
         
       var algorithms  = new List<IEncryptionAlgorithm>();  
       algorithms.Add(new MySimpleEncryptionAlgorithm());  
       algorithms.Add(new MyComplexEncryptionAlgorithm());  
         
       //Bind the data, may not work as is, depends on your combobox setup  
       comboBoxAlgorithms.DataSource = algorithms;  
         
       //Get the selected algorithm  
       var algorithm = comboBoxAlgorithms.SelectedItem;  
       algorithm.Encrypt("Hello");