locked
my Questions is about symmetric encryption algorithms

    Question

  • hello , thank you for full explain 2 line in blow code

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

    }
    Friday, February 04, 2011 6:09 PM

Answers

  • The keysize is in bits, Getbytes gets uhmm well bytes, so you have to divide by 8 to go from bits to bytes. the IV is the initalisation vector, see this wikipedia article on why and how it works.
    • Proposed as answer by eryang Monday, February 07, 2011 10:10 AM
    • Marked as answer by Paul Zhou Friday, February 18, 2011 4:37 AM
    Friday, February 04, 2011 8:42 PM

All replies

  • lol blow code?

    What his the question?

    Adam


    Ctrl+Z
    Friday, February 04, 2011 6:11 PM
  • i meaning is below code

     

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

    thanks

    Friday, February 04, 2011 6:22 PM
  • yes I know what you meant. What is your question?

    Adam


    Ctrl+Z
    Friday, February 04, 2011 6:41 PM
  • I ask you the full explain step by step the codes

     in this 2 code,what is the IV and why alg.KeySize and alg.BlockSize is divide 8?

    Friday, February 04, 2011 7:07 PM
  • The keysize is in bits, Getbytes gets uhmm well bytes, so you have to divide by 8 to go from bits to bytes. the IV is the initalisation vector, see this wikipedia article on why and how it works.
    • Proposed as answer by eryang Monday, February 07, 2011 10:10 AM
    • Marked as answer by Paul Zhou Friday, February 18, 2011 4:37 AM
    Friday, February 04, 2011 8:42 PM