首先,我仍然在學習面向對象的編程。好的,我有一個包含不同類型對稱算法的組合框。對稱加密算法功能
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?我要對這個完全錯誤的標題## ##
您應該爲此使用一個接口而不是抽象基類。 –
@WillVousden - 它會剪掉一大堆語法。 – ChaosPandion
@WillVousden好點。我同意。 –