首先,我还在学习面向对象编程。好的,我有一个包含不同类型对称算法的组合框。
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 语句中放置一个加密流时。那么有没有办法将检查卸载到函数并返回对称算法类型?用钥匙和 IV?我完全错了吗?##标题##