在C#中,为什么在内存中固定密钥更安全?
发布时间:2020-12-15 18:34:44 所属栏目:百科 来源:网络整理
导读:微软用于C#中加密/解密的示例代码有一条无法解释的行,它将密钥固定在内存中.我理解钉扎的概念 – 它用于表示GC不应该将该内存的内容移动到其他位置.当然,很好. 固定密钥有什么好处?我很确定有一个 – 一个非常聪明的开发人员,曾经一度提到过,这是我们软件安
微软用于C#中加密/解密的示例代码有一条无法解释的行,它将密钥固定在内存中.我理解钉扎的概念 – 它用于表示GC不应该将该内存的内容移动到其他位置.当然,很好.
固定密钥有什么好处?我很确定有一个 – 一个非常聪明的开发人员,曾经一度提到过,这是我们软件安全的重要一步.来自MS article的相关代码. static void Main() { // Must be 64 bits,8 bytes. // Distribute this key to the user who will decrypt this file. string sSecretKey; // Get the key for the file to encrypt. sSecretKey = GenerateKey(); // For additional security pin the key. GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned ); // Encrypt the file. EncryptFile( @"C:MyData.txt",@"C:Encrypted.txt",sSecretKey ); // Decrypt the file. DecryptFile( @"C:Encrypted.txt",@"C:Decrypted.txt",sSecretKey ); // Remove the key from memory. ZeroMemory( gch.AddrOfPinnedObject(),sSecretKey.Length * 2 ); gch.Free(); } 解决方法
这是因为覆盖内存只会覆盖数据所在的位置.
如果垃圾收集器已将其移动,则可能存在其先前位置中剩余的数据的副本. 你为什么不使用 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |