views:

33

answers:

1

There are 2 buttons which will generate 2 piece of data. What I need to achieve is encrypt them separately and export 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 elabrate this.

Many thanks.

+1  A: 

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();
                }
            }
        }
    }

}
Cory Charlton
I didnt mention that it is allowed to export only one piece data too.By your way my program wont know when to export file. I think the key element by using CBC is how to use the last bit block to encrypt the new data piece.
Kelvin
Yes if there is a requirement that both encrypted values appear as a single value in the data file then my solution would need more to work. You could add another step to "obfuscate" the XML so it would appear to be a single value but this might not be exactly what you want. Not sure what you mean by "my program wont know when to export file". How is the application currently deciding when to export?
Cory Charlton
After looking at it, this solution seems to work for me.I will use it if I couldn't figure out a way by using stream only.Because my peer suggested this to me.Thank you for ur post anywayvery helpful
Kelvin
If this answered your question can you accept it so it will no longer be in the "Unanswered" bucket. Hope your progress is going well.
Cory Charlton