コード例 #1
0
ファイル: Form1.cs プロジェクト: HoreaOros/MyCodes
        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(" ");
            listBox1.Items.Add("textul criptat:");
            string selectedItem = comboBox1.Items[comboBox1.SelectedIndex].ToString();

            if (selectedItem == "aes")
            {
                using (Aes myAes = Aes.Create())
                {
                    encrypted = new byte[listBox1.Items.Count][];;
                    myAes.Key = new byte[16];
                    myAes.GenerateKey();
                    myAes.IV = new byte[16];
                    myAes.GenerateIV();
                    key1 = myAes.Key;
                    iv1  = myAes.IV;
                    for (int i = 0; i < count; i++)
                    {
                        // Encrypt the string to an array of bytes.
                        encrypted[i] = SymmetricAlgorithm.EncryptStringToBytes_Aes(elements[i], myAes.Key, myAes.IV);
                    }


                    for (int i = 0; i < count; i++)
                    {
                        listBox1.Items.Add(Convert.ToBase64String(encrypted[i]));
                    }
                }
            }
            else if (selectedItem == "cbc")
            {
                using (var random = new RNGCryptoServiceProvider())
                {
                    var key = new byte[16];
                    random.GetBytes(key);
                    key1 = key;
                    for (int i = 0; i < count; i++)
                    {
                        // Encrypt the string to an array of bytes.
                        encrypted[i] = AnotherCipher.EncryptStringToBytes_Aes(elements[i], key);
                    }
                    for (int i = 0; i < count; i++)
                    {
                        listBox1.Items.Add(Convert.ToBase64String(encrypted[i]));
                    }
                }
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: HoreaOros/MyCodes
        public void button4_Click(object sender, EventArgs e)
        {
            // listBox1.Items.Add(" ");
            listBox1.Items.Clear();
            listBox1.Items.Add("textul criptat cu padding:");
            string selectedItem = comboBox1.Items[comboBox1.SelectedIndex].ToString();

            if (selectedItem == "aes")
            {
                using (Aes myAes = Aes.Create())
                {
                    myAes.Padding = PaddingMode.ISO10126;
                    // Encrypt the string to an array of bytes.
                    byte[] encrypted = SymmetricAlgorithm.EncryptStringToBytes_Aes(listBox1.Items.ToString(), myAes.Key, myAes.IV);

                    // Decrypt the bytes to a string.
                    string roundtrip = SymmetricAlgorithm.DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);

                    //Display the original data and the decrypted data.
                    // listBox1.Items.Clear();
                    for (int i = 0; i < 5; i++)
                    {
                        listBox1.Items.Add(encrypted);
                    }
                }
            }
            else if (selectedItem == "cbc")
            {
                using (var random = new RNGCryptoServiceProvider())
                {
                    var key = new byte[16];
                    random.GetBytes(key);
                    // Encrypt the string to an array of bytes.
                    byte[] encrypted = AnotherCipher.EncryptStringToBytes_Aes(listBox1.Items.ToString(), key);
                    // Decrypt the bytes to a string.
                    string roundtrip = AnotherCipher.DecryptStringFromBytes_Aes(encrypted, key);

                    //Display the original data and the decrypted data.
                    listBox1.Items.Add(Convert.ToBase64String(encrypted));
                }
            }
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: HoreaOros/MyCodes
        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(" ");
            listBox1.Items.Add("textul decriptat:");
            string selectedItem = comboBox1.Items[comboBox1.SelectedIndex].ToString();

            if (selectedItem == "aes")
            {
                using (Aes myAes = Aes.Create())
                {
                    myAes.Key = key1;
                    myAes.IV  = iv1;
                    string[] roundtrip = new string[count];

                    for (int i = 0; i < count; i++)
                    { // Decrypt the bytes to a string.
                        roundtrip[i] = SymmetricAlgorithm.DecryptStringFromBytes_Aes(encrypted[i], myAes.Key, myAes.IV);
                    }
                    for (int i = 0; i < count; i++)
                    {
                        listBox1.Items.Add(roundtrip[i]);
                    }
                }
            }
            else if (selectedItem == "cbc")
            {
                using (var random = new RNGCryptoServiceProvider())
                {
                    // Decrypt the bytes to a string.
                    string[] roundtrip = new string[count];
                    for (int i = 0; i < count; i++)
                    {
                        roundtrip[i] = AnotherCipher.DecryptStringFromBytes_Aes(encrypted[i], key1);
                    }
                    for (int i = 0; i < count; i++)
                    {
                        listBox1.Items.Add(roundtrip[i]);
                    }
                }
            }
        }