Symmetric Encryption Algorithm Function

To get started, I am still studying object-oriented programming. Ok, I have a combo box with 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 an encryption function that checks 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 do not want to put a crypto stream in every if statement. So, is there a way to disable function checking and return a symmetric algorithm type? With a key and IV? Am I completely wrong about this? ## Title ##

+4
source share
2 answers

A more object oriented approach:

Create an algorithm interface that will 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 the combo box to this list:

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

You can later refer to the selected item:

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

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

+2
source

Well, my first hope was to give you Wikipedia links to the factory method and abstract factory templates (there, I did it still), but since you say you're new, don't let big guns appear.

Basically, you need to find a common feature of all encryption algorithms and create a method that will return an instance of an object that has this common feature. Manifestation of such a trait can be either an abstract class or an interface in C #, and you are lucky that all your selected encryptions come from SymmetricAlgorithm ("luck" is probably an insult to the developers of System.Security.Cryptography, but I'm sure they will forgive me for the sake of illustration;).

So, just refactor your code by introducing a new method, perhaps in the following lines:

 private SymmetricAlgorithm GetAlgorithm(int index) { switch (index) { case 0: return Aes.Create(); case 1: return DES.Create(); case 2: return Rijndael.Create(); default: throw new NotSupportedException("unknown algorithm"); } } 

You can easily understand how to use this new method from the rest of your code.

+2
source

Source: https://habr.com/ru/post/1433202/


All Articles