Click here to Skip to main content
15,912,457 members
Home / Discussions / C#
   

C#

 
GeneralRe: How much memory do uninitialized array take up? Pin
N a v a n e e t h4-Jul-08 16:47
N a v a n e e t h4-Jul-08 16:47 
GeneralRe: How much memory do uninitialized array take up? Pin
Guffa5-Jul-08 4:00
Guffa5-Jul-08 4:00 
GeneralRe: How much memory do uninitialized array take up? Pin
N a v a n e e t h5-Jul-08 16:33
N a v a n e e t h5-Jul-08 16:33 
AnswerRe: How much memory do uninitialized array take up? Pin
Guffa5-Jul-08 3:55
Guffa5-Jul-08 3:55 
Questionhow to enable wave out mixer or stero mixer option using c# Pin
theredonion4-Jul-08 10:49
theredonion4-Jul-08 10:49 
QuestionEncrypted string changed when Serialized [modified] Pin
DaveyM694-Jul-08 10:03
professionalDaveyM694-Jul-08 10:03 
AnswerRe: Encrypted string changed when Serialized Pin
Guffa4-Jul-08 12:28
Guffa4-Jul-08 12:28 
GeneralRe: Encrypted string in dictionary Pin
DaveyM694-Jul-08 22:03
professionalDaveyM694-Jul-08 22:03 
It returns a string so it shouldn't be anything to do with references.

As I said the codes at work but I've coded up a quick example that replicates the issue here. As you will see, the encrypted keys are different (why?) but it decrypts correctly. (This example has ruled out serializazing/deserializing as the issue)
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Crypt
{
    class Program
    {
        public static string password;
        static void Main(string[] args)
        {
            Console.Write("Please enter your password: ");
            password = Console.ReadLine();
            string plainKey = "Test key";
            string value = "Test value";
            MyDictionary myDictionary = new MyDictionary();
            Console.WriteLine("Adding key: " + plainKey);
            myDictionary.AddItem(plainKey, value);
            Console.WriteLine("Looking for " + plainKey.Encrypt(password));
            Console.WriteLine("Found :" + myDictionary.ContainsKey(plainKey.Encrypt(password)));
            Console.WriteLine("Dictionary contents:");
            foreach (KeyValuePair<string, string> entry in myDictionary)
            {
                Console.WriteLine("Encrypted: " + entry.Key);
                Console.WriteLine("Decrypted: " + entry.Key.Decrypt(password));
            }
            Console.ReadKey();
        }
    }

    class MyDictionary : SortedDictionary<string, string>
    {
        public void AddItem(string plainKey, string value)
        { this.Add(plainKey.Encrypt(Program.password), value); }
    }

    public static class Extensions
    {
        public static string Encrypt(this string str, string password)
        {
            RijndaelManaged cipher = new RijndaelManaged();
            byte[] plainText = System.Text.Encoding.Unicode.GetBytes(str);
            byte[] salt = Encoding.ASCII.GetBytes(password.Length.ToString());
            cipher.Padding = PaddingMode.ISO10126;
            PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt);
            ICryptoTransform encryptor = cipher.CreateEncryptor(key.GetBytes(32), key.GetBytes(16));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(plainText, 0, plainText.Length);
            cryptoStream.FlushFinalBlock();
            byte[] cipherBytes = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            return Convert.ToBase64String(cipherBytes);
        }
        public static string Decrypt(this string str, string password)
        {
            try
            {
                RijndaelManaged cipher = new RijndaelManaged();
                byte[] encryptedText = Convert.FromBase64String(str);
                byte[] salt = Encoding.ASCII.GetBytes(password.Length.ToString());
                cipher.Padding = PaddingMode.ISO10126;
                PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt);
                ICryptoTransform decryptor = cipher.CreateDecryptor(key.GetBytes(32), key.GetBytes(16));
                MemoryStream memoryStream = new MemoryStream(encryptedText);
                CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
                byte[] plainText = new byte[encryptedText.Length];
                int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
                memoryStream.Close();
                cryptoStream.Close();
                return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
            }
            catch
            { return string.Empty; }
        }
    }
}


Dave

GeneralRe: Encrypted string in dictionary Pin
DaveyM694-Jul-08 22:07
professionalDaveyM694-Jul-08 22:07 
QuestionWeb service and multithreading Pin
Le centriste4-Jul-08 9:02
Le centriste4-Jul-08 9:02 
AnswerRe: Web service and multithreading Pin
DaveyM694-Jul-08 10:09
professionalDaveyM694-Jul-08 10:09 
GeneralRe: Web service and multithreading Pin
Le centriste7-Jul-08 5:46
Le centriste7-Jul-08 5:46 
AnswerRe: Web service and multithreading Pin
N a v a n e e t h4-Jul-08 18:07
N a v a n e e t h4-Jul-08 18:07 
AnswerRe: Web service and multithreading Pin
leppie4-Jul-08 18:22
leppie4-Jul-08 18:22 
GeneralRe: Web service and multithreading Pin
Le centriste7-Jul-08 6:06
Le centriste7-Jul-08 6:06 
Questionstring equal Pin
netJP12L4-Jul-08 8:41
netJP12L4-Jul-08 8:41 
AnswerRe: string equal Pin
MoustafaS4-Jul-08 9:39
MoustafaS4-Jul-08 9:39 
GeneralRe: string equal Pin
DaveyM694-Jul-08 9:50
professionalDaveyM694-Jul-08 9:50 
AnswerRe: string equal Pin
DaveyM694-Jul-08 9:47
professionalDaveyM694-Jul-08 9:47 
AnswerRe: string equal Pin
N a v a n e e t h4-Jul-08 18:20
N a v a n e e t h4-Jul-08 18:20 
QuestionIssue Setting Remote Registry Key Value Pin
CNewbie4-Jul-08 8:24
CNewbie4-Jul-08 8:24 
QuestionProcess class access denied on smss Pin
stwu4-Jul-08 8:18
stwu4-Jul-08 8:18 
Questiontyped dataset schema relating to xml file Pin
steve_rm4-Jul-08 8:13
steve_rm4-Jul-08 8:13 
QuestionLine in PictureBox Pin
godspeed1234-Jul-08 7:44
godspeed1234-Jul-08 7:44 
AnswerRe: Line in PictureBox Pin
netJP12L4-Jul-08 8:35
netJP12L4-Jul-08 8:35 

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.