tommym121
asked on
C# - How to encrypt a data dictionary
I have a class that read a delimited file with a string and a integer and store it into a data dictionary.
I will like to encrypt it into a file and later be able to read the encrypt file and put data back into the data dictioanry.
I have the code below and not sure where I need to add to encrypt the data.
I thought I will write data dictionary into a memory file and then write using cryptostream. Is that correct? If I do this how can I retrieve it and restore the key/value into the data dictionary
I will like to encrypt it into a file and later be able to read the encrypt file and put data back into the data dictioanry.
I have the code below and not sure where I need to add to encrypt the data.
I thought I will write data dictionary into a memory file and then write using cryptostream. Is that correct? If I do this how can I retrieve it and restore the key/value into the data dictionary
U must choose delimiters that u know are not part of the dictionary keys or values otherwise u wont get the conversion right.
Next step is to encrypt the string.
To get dictionary back from the string:
var dictionary = str.Split('|').ToDictionar y(s=>s.Spl it('@')[0] ,s=>s.Spli t('@')[1]) ;
Next step is to encrypt the string.
To get dictionary back from the string:
var dictionary = str.Split('|').ToDictionar
ASKER
I have created the encrypt file with the routine below. How would I read it back.
Do I first read all the bytes from CryptoStream. Then put into the memorystream, then read from memory stream and restore data into the dictionary. Any help will appreciated
public void EncryptAndSerialize(string filename, SymmetricAlgorithm key)
{
using (FileStream fs = File.Open(filename, FileMode.Create))
{
using (CryptoStream cs = new CryptoStream(fs, key.CreateEncryptor(), CryptoStreamMode.Write))
{
using (MemoryStream ms = new MemoryStream())
{
foreach (var item in map)
{
string line = item.Key + "," + item.Value + "\n";
byte[] array = Encoding.ASCII.GetBytes(li ne);
ms.Write(array, 0, line.Length);
}
byte[] allbytes = ms.GetBuffer();
cs.Write(allbytes, 0, allbytes.Length);
}
}
}
}
Do I first read all the bytes from CryptoStream. Then put into the memorystream, then read from memory stream and restore data into the dictionary. Any help will appreciated
public void EncryptAndSerialize(string
{
using (FileStream fs = File.Open(filename, FileMode.Create))
{
using (CryptoStream cs = new CryptoStream(fs, key.CreateEncryptor(), CryptoStreamMode.Write))
{
using (MemoryStream ms = new MemoryStream())
{
foreach (var item in map)
{
string line = item.Key + "," + item.Value + "\n";
byte[] array = Encoding.ASCII.GetBytes(li
ms.Write(array, 0, line.Length);
}
byte[] allbytes = ms.GetBuffer();
cs.Write(allbytes, 0, allbytes.Length);
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
One between key and its value and another between each key-value pair.
Example:
key1@value1|key2@value2|ke
To convert dictionary to such representation:
String.Join("|", dictionary.Select(kv=>Stri