惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

ChinaYuanGe 的个人博客

有点忘记了,赶紧写一篇 - ChinaYuanGe 的个人博客 博客的新皮肤 - ChinaYuanGe 的个人博客 新春快乐! - ChinaYuanGe 的个人博客 又是新的一年了 - ChinaYuanGe 的个人博客 如何正确搞定手机来电 - ChinaYuanGe 的个人博客
C# 里的 Array.Clear() 和 array = null
2025-10-10 · via ChinaYuanGe 的个人博客

最近正在编写一个高加密的通讯库,目前写到涉及到密钥销毁,于是就思考如何真正且安全的将密钥“销毁”。

其实论安全来说,最核心的就是:非程序活动时间内,不应该在内存保留密钥数据。于是就衍生出了直接将 array 设置为 null 和 Array.Clear() 两个方法。

接下来就做一个小实验,需要请到大名鼎鼎的作弊工具:Cheat Engine。当然我只需要用到他的内存读取功能。

现在我写了一个这样的代码,定义一个字符串,然后将其转换为数组数据,接着把第一个字符恢复正常以方便在 CE 搜索。

using System.Text;

namespace TestGround
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input = "Fxample Mem.";
            byte[] toArray = Encoding.ASCII.GetBytes(input);
            toArray[0] = (byte)'E';

            retry:
            Console.WriteLine("1. Null it.");
            Console.WriteLine("2. Clear it.");
            string read = Console.ReadLine();
            switch (read[0]) {
                case '1':
                    Console.WriteLine("Tell GC don't collect.");
                    if (GC.TryStartNoGCRegion(16384)) {
                        Console.WriteLine("GC won't collect now.");
                    }
                    toArray = null;
                    Console.WriteLine("Array is null now.");
                    break;
                case '2':
                    Console.WriteLine("Clear array.");
                    Array.Clear(toArray, 0, toArray.Length);
                    Console.WriteLine("Array is cleared now.");
                    break;
                default:
                    goto retry;
            }
            Console.ReadLine();
        }
    }
}

当程序启动后,他只会打印出操作方法,然后经过还原出 "Example Mem." 字符串,我们尝试在 CE 搜索这个字符串,就得出这个结果:

当我们输入 1、回车 的时候,可以发现执行后,内存的字符串依旧在这里。(GC 的自动回收已经在代码里压制了,所以实验结果不受影响)

然后我们就测试一下 Array.Clear() 方法的能力。因为在 C# 里的 Array 引用已经为 null,所以需要重开程序才可以验证,也就是重复上面的步骤。

接着我们输入 2、回车,看看内存发生了什么变化:

可以看到,内存数据已经被抹掉了。

总结:对于一些很敏感的数据,一定要使用 Array.Clear() 方法进行彻底抹除,否则黑客有可能会通过 Dump 内存获取相关信息,从而截获客户的会话、甚至是账号访问权。