Click here to Skip to main content
15,888,351 members
Home / Discussions / Algorithms
   

Algorithms

 
AnswerRe: Algorithm for creating new parser Pin
sep11-Feb-13 4:18
sep11-Feb-13 4:18 
Questionlanguage translation Pin
serena simmy30-Jan-13 18:12
serena simmy30-Jan-13 18:12 
AnswerRe: language translation Pin
Alan Balkany1-Feb-13 5:04
Alan Balkany1-Feb-13 5:04 
GeneralRe: language translation Pin
serena simmy2-Feb-13 2:35
serena simmy2-Feb-13 2:35 
QuestionAES algorithm Pin
lamar nadeen16-Jan-13 4:36
lamar nadeen16-Jan-13 4:36 
AnswerRe: AES algorithm Pin
Alan N16-Jan-13 5:10
Alan N16-Jan-13 5:10 
AnswerRe: AES algorithm Pin
pasztorpisti23-Jan-13 20:34
pasztorpisti23-Jan-13 20:34 
AnswerRe: AES algorithm Pin
Joezer BH11-Feb-13 19:46
professionalJoezer BH11-Feb-13 19:46 
C#
using System;
using System.IO;
using System.Security.Cryptography;

namespace Aes_Example
{
    class AesExample
    {
        public static void Main()
        {
            try
            {

                string original = "Here is some data to encrypt!";

                // Create a new instance of the AesManaged 
                // class.  This generates a new key and initialization  
                // vector (IV). 
                using (AesManaged myAes = new AesManaged())
                {

                    // Encrypt the string to an array of bytes. 
                    byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);

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

                    //Display the original data and the decrypted data.
                    Console.WriteLine("Original:   {0}", original);
                    Console.WriteLine("Round Trip: {0}", roundtrip);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }
        static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments. 
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");
            byte[] encrypted;
            // Create an AesManaged object 
            // with the specified key and IV. 
            using (AesManaged aesAlg = new AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption. 
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {

                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }


            // Return the encrypted bytes from the memory stream. 
            return encrypted;

        }

        static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments. 
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");

            // Declare the string used to hold 
            // the decrypted text. 
            string plaintext = null;

            // Create an AesManaged object 
            // with the specified key and IV. 
            using (AesManaged aesAlg = new AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting stream 
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;

        }
    }
}

Cheees,
Edo

QuestionImplementation of Secure Remote Password in VB.NET Pin
Dominick Marciano12-Jan-13 14:22
professionalDominick Marciano12-Jan-13 14:22 
GeneralRe: Implementation of Secure Remote Password in VB.NET Pin
harold aptroot12-Jan-13 22:17
harold aptroot12-Jan-13 22:17 
GeneralRe: Implementation of Secure Remote Password in VB.NET Pin
Dominick Marciano13-Jan-13 2:17
professionalDominick Marciano13-Jan-13 2:17 
GeneralRe: Implementation of Secure Remote Password in VB.NET Pin
harold aptroot13-Jan-13 5:03
harold aptroot13-Jan-13 5:03 
QuestionRobot voice and FFT Pin
ltronghau28-Dec-12 21:30
ltronghau28-Dec-12 21:30 
AnswerRe: Robot voice and FFT Pin
Albert Holguin11-Feb-13 14:59
professionalAlbert Holguin11-Feb-13 14:59 
GeneralRe: Robot voice and FFT Pin
Andy Allinger21-Jan-14 15:44
professionalAndy Allinger21-Jan-14 15:44 
GeneralRe: Robot voice and FFT Pin
Albert Holguin22-Jan-14 3:14
professionalAlbert Holguin22-Jan-14 3:14 
QuestionHow to get the index of a cell in a Table Pin
pix_programmer20-Dec-12 1:27
pix_programmer20-Dec-12 1:27 
AnswerRe: How to get the index of a cell in a Table Pin
Richard MacCutchan20-Dec-12 2:10
mveRichard MacCutchan20-Dec-12 2:10 
Questionconfusions Pin
en41117-Dec-12 22:37
en41117-Dec-12 22:37 
AnswerRe: confusions Pin
Alan Balkany18-Dec-12 5:43
Alan Balkany18-Dec-12 5:43 
QuestionWhen completing a bespoke programme should I give the code up? Pin
itorchuk17-Dec-12 8:08
itorchuk17-Dec-12 8:08 
AnswerRe: When completing a bespoke programme should I give the code up? Pin
jschell17-Dec-12 8:27
jschell17-Dec-12 8:27 
GeneralRe: When completing a bespoke programme should I give the code up? Pin
itorchuk17-Dec-12 8:37
itorchuk17-Dec-12 8:37 
QuestionPrime Number Generation Pin
Joshua Guyette14-Dec-12 9:36
Joshua Guyette14-Dec-12 9:36 
AnswerRe: Prime Number Generation Pin
Alan Balkany17-Dec-12 4:39
Alan Balkany17-Dec-12 4:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.