2017-12-20 18:49:11 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
2017-12-21 18:24:01 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2017-12-20 18:49:11 +01:00
|
|
|
|
using Windows.Security.Cryptography.DataProtection;
|
|
|
|
|
using Windows.Storage.Streams;
|
|
|
|
|
using ModernKeePassLib.Native;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePassLib.Cryptography
|
|
|
|
|
{
|
|
|
|
|
public static class ProtectedData
|
|
|
|
|
{
|
2017-12-21 18:24:01 +01:00
|
|
|
|
public static async Task ProtectStream(byte[] buffer, IOutputStream stream)
|
2017-12-20 18:49:11 +01:00
|
|
|
|
{
|
2017-12-21 18:24:01 +01:00
|
|
|
|
//instantiate a DataProtectionProvider for decryption
|
|
|
|
|
var dpp = new DataProtectionProvider("LOCAL=user");
|
|
|
|
|
|
|
|
|
|
//Open a stream to load data in
|
|
|
|
|
using (var inputStream = new InMemoryRandomAccessStream())
|
2017-12-20 18:49:11 +01:00
|
|
|
|
{
|
2017-12-21 18:24:01 +01:00
|
|
|
|
//cteate data writer to write data to the input stream
|
|
|
|
|
using (var dw = new DataWriter(inputStream))
|
|
|
|
|
{
|
|
|
|
|
//write data to the stream
|
|
|
|
|
dw.WriteBuffer(buffer.AsBuffer());
|
|
|
|
|
await dw.StoreAsync();
|
|
|
|
|
|
|
|
|
|
//encrypt the intput stream into the file stream
|
|
|
|
|
await dpp.ProtectStreamAsync(inputStream.GetInputStreamAt(0),
|
|
|
|
|
stream);
|
|
|
|
|
}
|
2017-12-20 18:49:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 18:24:01 +01:00
|
|
|
|
public static async Task<byte[]> UnprotectStream(IInputStream stream)
|
2017-12-20 18:49:11 +01:00
|
|
|
|
{
|
2017-12-21 18:24:01 +01:00
|
|
|
|
//instantiate a DataProtectionProvider for decryption
|
|
|
|
|
var dpp = new DataProtectionProvider();
|
|
|
|
|
|
|
|
|
|
//create a stream to decrypte the data to
|
|
|
|
|
using (var outputStream = new InMemoryRandomAccessStream())
|
2017-12-20 18:49:11 +01:00
|
|
|
|
{
|
2017-12-21 18:24:01 +01:00
|
|
|
|
//decrypt the data
|
|
|
|
|
await dpp.UnprotectStreamAsync(stream, outputStream);
|
|
|
|
|
|
|
|
|
|
//fill the data reader with the content of the outputStream,
|
|
|
|
|
//but from position 0
|
|
|
|
|
using (var dr = new DataReader(outputStream.GetInputStreamAt(0)))
|
|
|
|
|
{
|
|
|
|
|
//load data from the stream to the dataReader
|
|
|
|
|
await dr.LoadAsync((uint)outputStream.Size);
|
|
|
|
|
|
|
|
|
|
//load the data from the datareader into a buffer
|
|
|
|
|
IBuffer data = dr.ReadBuffer((uint)outputStream.Size);
|
|
|
|
|
|
|
|
|
|
return data.ToArray();
|
|
|
|
|
}
|
2017-12-20 18:49:11 +01:00
|
|
|
|
}
|
2017-12-21 18:24:01 +01:00
|
|
|
|
}
|
2017-12-20 18:49:11 +01:00
|
|
|
|
|
2017-12-21 18:24:01 +01:00
|
|
|
|
public static byte[] Unprotect(byte[] pbEnc, byte[] mPbOptEnt, DataProtectionScope currentUser)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] Protect(byte[] pbPlain, byte[] mPbOptEnt, DataProtectionScope currentUser)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
2017-12-20 18:49:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|