WinRT中 压缩/解压缩,加密/解密

网络传输时通常需要压缩数据,可以用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);
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Windows 10 创建 Toast 通知,并在其添加一个监听按钮,可以使用以下步骤: 1. 创建 XML 文件:创建一个包含 Toast 通知内容的 XML 文件。在 XML 文件,使用 `<actions>` 元素来定义一个或多个监听按钮,如下所示: ```xml <toast> <visual> <binding template="ToastGeneric"> <text>标题</text> <text>消息正文</text> </binding> </visual> <actions> <action content="按钮1" arguments="action1"/> <action content="按钮2" arguments="action2"/> </actions> </toast> ``` 在上面的代码,`<actions>` 元素定义了两个监听按钮,分别是“按钮1”和“按钮2”,它们的 `arguments` 属性分别为“action1”和“action2”。 2. 加载 XML 文件:使用 `XmlDocument` 类加载上一步创建的 XML 文件。 ```csharp XmlDocument toastXml = new XmlDocument(); toastXml.LoadXml(xml); ``` 3. 创建 Toast 通知:使用 `ToastNotification` 类创建一个 Toast 通知,并将上一步加载的 XML 文件作为参数传递给它。 ```csharp ToastNotification toast = new ToastNotification(toastXml); ``` 4. 添加按钮点击事件处理程序:使用 `toast.Activated` 事件来添加按钮点击事件处理程序。在处理程序,可以根据按钮的 `arguments` 属性来执行相应的操作。 ```csharp toast.Activated += (sender, args) => { switch(args.Argument) { case "action1": // 执行按钮1的操作 break; case "action2": // 执行按钮2的操作 break; } }; ``` 5. 显示 Toast 通知:使用 `ToastNotificationManager.CreateToastNotifier().Show()` 方法来显示 Toast 通知。 ```csharp ToastNotificationManager.CreateToastNotifier().Show(toast); ``` 这样,就可以创建带有监听按钮的 Toast 通知,并在按钮被点击时执行相应的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值