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

推荐订阅源

Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
Vercel News
Vercel News
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
T
Threatpost
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
I
Intezer
P
Privacy International News Feed
D
Docker
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
A
Arctic Wolf
IT之家
IT之家
S
SegmentFault 最新的问题
S
Securelist
博客园 - 叶小钗
N
News and Events Feed by Topic
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - Franky
GbyAI
GbyAI
AI
AI
Y
Y Combinator Blog
WordPress大学
WordPress大学
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
N
News | PayPal Newsroom
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
InfoQ

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 内存获取相关信息,从而截获客户的会话、甚至是账号访问权。