Click here to Skip to main content
15,912,021 members
Home / Discussions / Algorithms
   

Algorithms

 
QuestionThe degree of a node in a tree is the number of children the node has. If a tree has n1 nodes of degree 1, n2 nodes of degree 2, ..., nm nodes of degree m, compute the number of leaves in the tree in terms of n1, n2, . . . , nm. Pin
amistry_petlad28-Mar-13 7:04
amistry_petlad28-Mar-13 7:04 
NewsCaltech's FREE online Machine Learning course -- LAST CHANCE Pin
Matt T Heffron27-Mar-13 11:53
professionalMatt T Heffron27-Mar-13 11:53 
NewsNew computer vision algorithms, check out the videos Pin
BupeChombaDerrick20-Feb-13 20:33
BupeChombaDerrick20-Feb-13 20:33 
GeneralRe: New computer vision algorithms, check out the videos Pin
Simon_Whale1-Mar-13 0:13
Simon_Whale1-Mar-13 0:13 
GeneralRe: New computer vision algorithms, check out the videos Pin
BupeChombaDerrick1-Mar-13 23:24
BupeChombaDerrick1-Mar-13 23:24 
QuestionComplexity and Routing Problems Pin
brkonja18-Feb-13 13:10
brkonja18-Feb-13 13:10 
AnswerRe: Complexity and Routing Problems Pin
Alan Balkany21-Feb-13 4:24
Alan Balkany21-Feb-13 4:24 
GeneralRe: Complexity and Routing Problems Pin
BupeChombaDerrick21-Feb-13 20:38
BupeChombaDerrick21-Feb-13 20:38 
QuestionLinear algebra jacobi Singular Value Decomposition (SVD) algorithm Pin
BupeChombaDerrick12-Feb-13 20:25
BupeChombaDerrick12-Feb-13 20:25 
AnswerRe: Linear algebra jacobi Singular Value Decomposition (SVD) algorithm Pin
BupeChombaDerrick13-Feb-13 19:32
BupeChombaDerrick13-Feb-13 19:32 
QuestionAlgorithm for creating new parser Pin
somanath kavalase1-Feb-13 21:18
somanath kavalase1-Feb-13 21:18 
AnswerRe: Algorithm for creating new parser Pin
Eddy Vluggen2-Feb-13 0:52
professionalEddy Vluggen2-Feb-13 0:52 
AnswerRe: Algorithm for creating new parser Pin
jschell2-Feb-13 7:55
jschell2-Feb-13 7:55 
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 

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.