Encrypt 2 pieces of data separately and export as one file via Rijndael in C#
There are 2 buttons which will generate 2 piece of data. What I need to achieve is encrypt them separately and expo开发者_如何学Crt to same file. Then I need be able to decrypt it later.
Is it possible to use CBC mode? Using the same stream to encrypt the 1st block of the 2nd piece of data by the last block of the 1st piece of data? (To avoid IV)
Or
I can do this in ECB mode? Would be good if someone can elaborate on this.
You could always store the two values as separate properties of the file.
[Serializable]
public class EncryptedValues
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public static EncryptedValues FromXml(string xmlString)
{
if (!string.IsNullOrEmpty(xmlString))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(EncryptedValues));
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamWriter streamWriter = new StreamWriter(memoryStream))
{
streamWriter.Write(xmlString);
streamWriter.Flush();
memoryStream.Flush();
memoryStream.Position = 0;
return (xmlSerializer.Deserialize(memoryStream) as EncryptedValues);
}
}
}
return null;
}
public string ToXml()
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(EncryptedValues));
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamWriter streamWriter = new StreamWriter(memoryStream))
{
xmlSerializer.Serialize(streamWriter, o);
streamWriter.Flush();
memoryStream.Flush();
memoryStream.Position = 0;
using (StreamReader streamReader = new StreamReader(memoryStream))
{
return streamReader.ReadToEnd();
}
}
}
}
}
精彩评论