0
votes

I want to encrypt and decrypt the isolated storage file.

The Microsoft site took me here

  1. While using Isolated Storage on the emulator, it can persist only until the emulator is running.

  2. There is no way to get the physical location of the Isolated Storage.

I hope the above statements of mine are correct.

Now, I want to know how can I encrypt the Isolated Storage file ?

Taking the example provided by Microsoft, (application name is GasMileage)

here is the code

namespace CodeBadger.GasMileage.Persistence
{
    public class IsolatedStorageGateway
    {
        private const string StorageFile = "data.txt";
        private readonly XmlSerializer _serializer;

    public IsolatedStorageGateway()
    {
        _serializer = new XmlSerializer(typeof (Notebook));
    }

    public Notebook LoadNotebook()
    {
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (var stream = GetStorageStreamForReading(store))
            using (var reader = new StreamReader(stream))
            {
                return reader.EndOfStream 
                    ? new Notebook() 
                    : (Notebook) _serializer.Deserialize(reader);
            }
        }
    }

    public NotebookEntry LoadEntry(Guid guid)
    {
        var notebook = LoadNotebook();
        return notebook.Where(x => x.Id == guid).FirstOrDefault();
    }

    public void StoreEntry(NotebookEntry entry)
    {
        var notebook = LoadNotebook();
        AssignId(entry);
        RemoveExistingEntryFromNotebook(notebook, entry);
        Console.WriteLine(entry);
        notebook.Add(entry);
        WriteNotebookToStorage(notebook);
    }

    public void DeleteEntry(NotebookEntry entry)
    {
        var notebook = LoadNotebook();
        RemoveExistingEntryFromNotebook(notebook, entry);
        WriteNotebookToStorage(notebook);
    }

    private void WriteNotebookToStorage(Notebook notebook)
    {
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        using (var stream = GetStorageStreamForWriting(store))
        {
            _serializer.Serialize(stream, notebook);
        }
    }

    private static void AssignId(NotebookEntry entry)
    {
        if (entry.Id == Guid.Empty) entry.Id = Guid.NewGuid();
    }

    private static void RemoveExistingEntryFromNotebook(Notebook notebook, NotebookEntry entry)
    {
        var toRemove = notebook.Where(x => x.Id == entry.Id).FirstOrDefault();
        if (toRemove == null) return;
        notebook.Remove(toRemove);
    }

    private static IsolatedStorageFileStream GetStorageStreamForWriting(IsolatedStorageFile store)
    {
        return new IsolatedStorageFileStream(StorageFile, FileMode.Create, FileAccess.Write, store);
    }

    private static IsolatedStorageFileStream GetStorageStreamForReading(IsolatedStorageFile store)
    {
        return new IsolatedStorageFileStream(StorageFile, FileMode.OpenOrCreate, FileAccess.Read, store);
    }
}

Now I want to know, How to encrypt the data.txt given in the context.

On Application load, decrypt the file and on application termination, it should encrypt.

Can someone help me on this ?

1

1 Answers

2
votes

The ProtectedData class will encrypt/decrypt a byte array for storing on isolated storage. You can supply your own additional entropy, but by default:

In Silverlight for Windows Phone, both the user and machine credentials are used to encrypt or decrypt data

For more information, see How to: Encrypt Data in a Windows Phone Application