本文介绍了如何安全地管理通过 Windows.Security.Cryptography 生成的 IBuffer 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继我之前的问题:Windows.Security.Cryptography.CryptographicBuffer 中的方法生成的 IBuffer 对象是否具有安全功能?

IBuffer 对象由 WinRT 中的加密例程返回和使用.正如我之前的问题所回答的那样,这些缓冲区的任何安全管理都必须由用户维护 - 例如覆盖内存,在不需要时加密等.但是,与底层 IBuffer 数据交互的方法在 C# 级别受到限制.

IBuffer objects are returned and used by the cryptographic routines in WinRT. As my previous question was answered, any secure management of those buffers has to be maintained by the user--e.g. overwriting the memory, encrypting when it isn't actively needed, etc. However, methods to interact with the data underlying IBuffers are limited at the C# level.

那么,C# 开发人员如何正确管理这些 IBuffer 中的敏感数据?

So, how can C# developers properly manage sensitive data in these IBuffers?

推荐答案

如果您愿意,可以在使用后清除缓冲区,即使使用 C#.这是一个方便的助手:

You can wipe out the buffer if you like after use, even with C#. Here is a handy helper:

public static class BufferExtensions
{
  public async static Task ClearContentsAsync(this IBuffer buff)
  {
    var writer = new DataWriter(buff.AsStream().AsOutputStream());
    for (var i = 0; i < buff.Capacity; i++)
      writer.WriteByte(42);
    await writer.StoreAsync();
  }
}

像这样使用它:

  var buff = CryptographicBuffer.GenerateRandom(20);
  var before = buff.ToArray();
  await buff.ClearContentsAsync();
  var after = buff.ToArray();
  Debug.WriteLine("{0},{1},{2} - {3},{4},{5}", 
    before[0], before[1], before[2], after[0], after[1], after[2]);

注意before(清除前拷贝)的值是随机的,而after(清除后拷贝)的值都是42.你可以当然使用您选择的不同值:-).

Note that the values in before (copy taken before clearing) are random, but the values in after (copy taken after clearing) are all 42. You can of course use a different value of your choice :-).

这篇关于如何安全地管理通过 Windows.Security.Cryptography 生成的 IBuffer 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 18:22