Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对称加密算法函数_C#_Encryption_Encryption Symmetric - Fatal编程技术网

C# 对称加密算法函数

C# 对称加密算法函数,c#,encryption,encryption-symmetric,C#,Encryption,Encryption Symmetric,首先,我还在学习面向对象编程。好的,我有一个包含不同类型对称算法的组合框 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"); com

首先,我还在学习面向对象编程。好的,我有一个包含不同类型对称算法的组合框

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;
}
然后我让我的加密函数检查它们是什么类型

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);
}

但当我不想在每个if语句中都放入加密流时。那么,有没有一种方法可以将检查转移到函数并返回Symmetrialgorithm类型?用钥匙和IV?我完全错了吗?######

更面向对象的方法是:

创建要在组合框中显示的算法界面:

public interface IAlgorithmItem
{
    SymmetricAlgorithm CreateAlgorithm();

    string DisplayName { get; }
}
然后,为每个所需算法创建一个新类:

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"; }
    }
}

// ...
然后,您可以创建一个新的项目列表:

var listItems = new List<IAlgorithmItem>() { new AesAlgorithm(), new RijndaelAlgorithm() };
稍后,您可以引用所选项目:

var algorithmItem = (IAlgorithmItem)comboBox1.SelectedItem;
var algorithm = algorithmItem.CreateAlgorithm();
编辑:更新了Will关于使用接口而不是抽象基类的建议。
编辑2:更新为使用create方法而不是属性,因为每次访问操作的结果都会创建一个新的算法。

好吧,我的第一个倾向是给你维基百科上的和模式的链接(在那里,我还是这么做了),但是既然你说你是一个初学者,我们就不要在前面说大炮了

基本上,您需要的是找到所有加密算法的一个共同特征,并创建一个方法,该方法将返回具有该共同特征的对象实例。这种特性的表现形式可能是C#中的抽象类或接口,你很幸运,你选择的所有加密都源自对称算法(“幸运”可能是对System.Security.Cryptography的设计者的侮辱,但为了说明起见,我相信他们会原谅我;)

因此,通过引入一种新方法,可能沿着以下思路,只需编写代码即可:

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");
  }
}

您可以很容易地从代码的其余部分了解如何使用这个新方法。

您可能应该使用接口,而不是抽象基类。@WillVousden-它会删除一大堆语法。我会将属性更改为方法。这个设计让我觉得AlgorithmItem是对称算法的一个实例。@ChaosPandion我也同意。这就是我的想法,谢谢你让我接触到一些新的东西。我能欣赏这个设计的简单性。@ChaosPandion:谢谢。我觉得其他答案,虽然从面向对象的角度来看可能更好,但对初学者来说可能会让人望而生畏。一步一个脚印,激起他们对更多的渴望:)非常感谢!我想这会管用的,我知道开关箱的事!C#与我所知道的编程语言非常不同,因此需要一些时间来适应。谢谢你的支持。@user1657838:不客气,我真的很高兴能帮上忙。
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");
  }
}