网络传输时通常需要压缩数据,可以用Windows.Storage.Compression.Compressor类:
public sealed class Compressor : IOutputStream, IDisposable {
// Bytes are compressed in a buffer of 'blocksize' bytes and written to underlying stream
public Compressor(IOutputStream stream, CompressAlgorithm algorithm, UInt32 blockSize);
// Compresses a buffer's bytes; the first write includes a header indicating the algorithm
public IAsyncOperationWithProgress<UInt32, UInt32> WriteAsync(IBuffer buffer);
// Called after last WriteAsync; stores internal buffer's remaining bytes to stream
public IAsyncOperation<Boolean> FinishAsync();
// Disassociates stream; stream will NOT be closed when Dispose is called
public IOutputStream DetachStream();
// Closes stream (if not detached); does NOT call FinishAsync
public void Dispose();
// Calls FlushAsync on underlying stream
public IAsyncOperation<Boolean> FlushAsync();
}
public enum CompressAlgorithm {
InvalidAlgorithm = 0, // Invalid; used for error checking
NullAlgorithm = 1, // No compression; typically used for testing
Mszip = 2, // MSZIP algorithm
Xpress = 3, // XPRESS algorithm
XpressHuff = 4, // XPRESS algorithm with Huffman encoding
Lzms = 5, // LZMS algorithm
}
压缩的例子如下:
async Task CompressFileAsync(IStorageFile originalFile, IStorageFile compressedFile) {
using (var input = await originalFile.OpenAsync(FileAccessMode.Read))
using (var output = await compressedFile.OpenAsync(FileAccessMode.ReadWrite))
using (var compressor = new Compressor(output, CompressAlgorithm.Mszip, 0)) {
// NOTE: Compressor implements the IOutputStream interface
await RandomAccessStream.CopyAsync(input, compressor);
await compressor.FinishAsync();
}
}
解压数据的例子如下:
public sealed class Decompressor : IInputStream, IDisposable {
// Bytes are decompressed as they are read from the underlying stream
public Decompressor(IInputStream underlyingStream);
// Decompresses a stream's bytes; the first read includes a header indicating the algorithm
public IAsyncOperationWithProgress<IBuffer, UInt32> ReadAsync(
IBuffer buffer, UInt32 count, InputStreamOptions options);
// Disassociates stream; stream will NOT be closed when Dispose is called
public IInputStream DetachStream();
// Closes stream (if not detached)
public void Dispose();
}
解压文件更简单:
async Task DecompressFileAsync(IStorageFile compressedFile, IStorageFile decompressedFile) {
using (var decompressor = new Decompressor(
await compressedFile.OpenAsync(FileAccessMode.Read)))
using (var output = await decompressedFile.OpenAsync(FileAccessMode.ReadWrite)) {
// NOTE: Decompressor implements the IInputStream interface
await RandomAccessStream.CopyAsync(decompressor, output);
}
}
加解密可以用Windows.Security.Cryptography.DataProtection.DataProtectionProvider类:
public sealed class DataProtectionProvider {
// When encrypting a buffer or stream, use these three members
public DataProtectionProvider(String protectionDescriptor);
public IAsyncOperation<IBuffer> ProtectAsync(IBuffer data);
public IAsyncAction ProtectStreamAsync(IInputStream src, IOutputStream dest);
// When decrypting a buffer or stream, use these three members
public DataProtectionProvider();
public IAsyncOperation<IBuffer> UnprotectAsync(IBuffer data);
public IAsyncAction UnprotectStreamAsync(IInputStream src, IOutputStream dest);
}
加密文件内容:
async Task EncryptFileAsync(IStorageFile originalFile, IStorageFile encryptedFile,String protectionDescriptor) {
using (var input = await originalFile.OpenAsync(FileAccessMode.Read))
using (var output = await encryptedFile.OpenAsync(FileAccessMode.ReadWrite)) {
var dpp = new DataProtectionProvider(protectionDescriptor);
await dpp.ProtectStreamAsync(input, output);
}
}
加密时必须要指定加密选项,选项有: ■■“LOCAL=logon” 对本机当前登陆账户加密
■■“LOCAL=user” 对本机所有登陆账户加密
■■“LOCAL=machine” 对本机任何账户加密
■■“WEBCRE DENTIALS=Jeffrey,wintellect.com” 对WEB账户加密
■■“SID=S-1-5-21-4392301 AND SID=S-1-5-21-3101812” 对域账户加密
■■“SDDL=O:S-1-5-5-0-290724G:SYD:(A;;CCDC;;;S-1-5-5-0-290724)(A;;DC;;;WD)” 对域账户加密
解密:
async Task DecryptFileAsync(IStorageFile encryptedFile, IStorageFile decryptedFile) {
using (var input = await encryptedFile.OpenAsync(FileAccessMode.Read))
using (var output = await decryptedFile.OpenAsync(FileAccessMode.ReadWrite)) {
var dpp = new DataProtectionProvider();
await dpp.UnprotectStreamAsync(input, output);
}
}