PDA

View Full Version : سوال: در مورد الگوریتم های رمز نگاری متقارن؟



saeedgharedaghi
جمعه 15 بهمن 1389, 14:51 عصر
سلام ممنون میشم این 2 خط رو توضیح کامل بدید؟(فقط اگه میشه کامل توضیح بدید)

private SymmetricAlgorithm GetSelectedAlgorithm()
{
SymmetricAlgorithm Alg = null;


if (comboBox1.SelectedIndex == 0)
Alg = new DESCryptoServiceProvider();

else if (comboBox1.SelectedIndex == 1)
Alg = new RijndaelManaged();//RijndaelManaged: AES Algorithm in .NET

else if (comboBox1.SelectedIndex == 2)
Alg = new TripleDESCryptoServiceProvider();

else
Alg = new RC2CryptoServiceProvider();
return Alg;

}
private void button1_Click(object sender, EventArgs e)
{
string password = textBoxKey.Text;
string salt = textBoxSalt.Text;
string plainText = textBoxInput.Text;
byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);

//Rfc2898DeriveBytes: Used to Generate Strong Keys
Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password,
Encoding.ASCII.GetBytes(salt));//Non-English Alfhabets Will not Work on ASCII Encoding

SymmetricAlgorithm Alg = GetSelectedAlgorithm();

Alg.Key = rfc.GetBytes(Alg.KeySize / 8);
Alg.IV = rfc.GetBytes(Alg.BlockSize / 8);

MemoryStream strCiphered = new MemoryStream();//To Store Encrypted Data

CryptoStream strCrypto = new CryptoStream(strCiphered,Alg.CreateEncryptor(),
CryptoStreamMode.Write);

strCrypto.Write(plainBytes, 0, plainBytes.Length);

strCrypto.Close();

textBoxCiphered.Text = Convert.ToBase64String(strCiphered.ToArray());

strCiphered.Close();

}