跳至主要内容

白话 IT 之 聊聊数据加密

原文地址白话 IT 之 聊聊数据加密,作者是 嘀嗒嘀嗒 微信公众号运营者angela,表示感谢

嘀嗒嘀嗒按:今天的分享来自我们 Airbnb 的数据加密的大牛桑立锋。说到桑同学,那比我牛出不止一个数量级了。俄亥俄州立大学毕业的 PhD,曾在Yahoo!、LinkedIn 等多家公司担任技术骨干。在过去三四年间获各种数据加密相关的专利达五项之多。

除了技术扎实,桑同学平时也是极好相处,又特别愿意帮助别人。在我过去和他不多的几次合作中,也不时又机会请教一二。虽然对于数据加密仍然是个门外汉,但是桑同学解释问题总能听得懂。因此在我的再三请求下,有了这篇科普性的文章。

虽然我们都就职于 Airbnb,但是这篇文章和 Airbnb 现在使用的技术没有直接关联。下面是正文。



数据加密是一个古老的问题,但又是一个无法做到完美的问题,尤其在一个复杂的大型系统中需要考虑方方面面的问题,包括 security, availability, usability, consistentcy, performance,extensibility 等等。除了那些完全不做的,大部分公司都会根据自身的需求,环境,资源以及工程师的能力等,设计和开发适合公司实际情况的解决方案。有做的好的,也有坦诚做的不好的,还有自己认为做的好的但其实经不起真正考验的。各种各样的都有。

就算在同一个公司,很多也会有不同的解决方案并存。有些是因为历史原因,比方说早期用方案A,后来开发了更牛逼的方案B,但没有把A完全干掉;或者扩张原因,比方说兼并了一个用不同方案的别家公司;又或者是政治原因,不同部门谁都不屌谁,各自用自认为合适的方案。  无论公司选择什么方案,因为话题的敏感性(法律原因,或者圈内的潜规则,或者仅仅是不想当出头鸟而被黑客们盯上),绝大部分都不会公开数据加密的细节。同样原因,这篇文章也不会涉及笔者公司怎么做数据加密的具体细节。想到哪儿写到哪儿吧。


先说为什么要做数据加密。越来越多的数据泄露事件,比方说 Yahoo 2013年被盗超过10亿用户信息 [链接1], Yahoo 2014年又被盗超过5亿用户信息 [链接2], LinkedIn 被盗一亿多用户密码 [链接3],Ashley Madison 被盗三千多万用户数据以及大量支付信息 [链接4],Target 被盗近七千万用户数据和银行账号 [链接5],Adobe 被盗三千八百万用户数据 [链接6] 等等,以及由此导致的大量法律纠纷和巨额赔偿,还有很多很多大大小小没有被公布的安全事件,都说明了数据加密和保护的重要性。

说到这些安全事件中用户密码的保护,插个题外话。笔者曾经跟很多工程师(包括很多应用领域专家)聊天的时候,都会被问到,为什么笔者认为仅仅 Hash 用户密码是不够的。很多人(包括网上的很多文章)都误认为用户密码只要 Hash了,就安全了,其实不然。不说 weak Hash 函数诸如 RC4 [链接7], MD5 [链接8]就算用 Bcrypt [链接9],KDF [链接10] 等运算复杂的Hash函数,虽然能防 Rainbow Table Attack [链接11],但对 Dictionary Attack [链接12] 却是无效的。当然这不是 Hash 函数本身的错,而是很多人会选一个容易记的密码,而这类密码往往 entropy 不够,很容易被解密。对用户密码而言,笔者一直建议不仅需要Hash,还要加密 (比方说用 keyed hash function,or MAC) [链接13]

还有很多公司要做加密不仅仅是因为保护用户隐私,更是法律法规的要求,不得不做。有些涉及特殊数据,如信用卡号码,那就要做 PCI (The Payment Card Industry Data Security Standard );如用户健康信息,那就要做 HIPAA (The Health Insurance Portability and Accountability Act)等等。

在笔者看来,任何一个收集以及存储客户数据的公司,就算是初创公司,都应该认真对待这个问题。在圈内我们常说,不是系统会不会黑,数据会不会被盗,而是何时被黑被盗的问题。也许有人会说,就算偷了,盗了,那又如何?这种事可大可小,笔者私下就知道有公司被黑后,被迫关门了的。所以安全领域内的及时投资,对公司长期来讲都是非常有益的。对重要数据(包括系统密码,用户信息等)进行有效保护,数据被黑被盗的门槛就高了;就算有一天系统被黑了,数据被盗了,也能把损失降到最小。


你说数据加密这么重要,怎么做呢?如果把加密这个问题抽象出来,其实就是要计算一个加密函数:
encrypt(data, key)
(当然还有一个函数就是解密,跟加密类似道理,暂且不论)。看上去似乎是一个非常简单的问题,但要把它做好非常不容易。尤其是在一个系统复杂的公司,要考虑的问题很多很多。比方说,密码界有很多算法,应该用什么加密算法,对称的还是非对称的?具体选哪个,AES,DES,RSA,ECC等等,各有什么特点?题外话,笔者在曾经工作过的公司见过很多有趣的例子,比方说有些早期开发人员用 XOR 来加密,或者做点简单的迭代替换,或者自创所谓的加密算法(有点掩耳盗铃的感觉)等等,这些最后都变成 technical debt,需要花很大力气去清理。

每个算法也有不同的变种和模式,各有什么特点,性能如何?如果某个算法被宣布不安全了,如何快速迭代?这个 key 怎么来?多长才是安全的?怎么启动?怎么保存?怎么传播?怎么控制访问权限?怎么知道谁访问了什么?怎么来监控?怎么来预警?怎么来系统性的更新这些 key?key 能不能丢,丢了怎么办?不同的应用可能是用不同的语言写的,怎么兼容?怎么支持大流量等等,等等。还有更加不近人情的要求,比方说如何保证被加密的数据能够 preserve 原始数据的顺序,支持搜索,但又不牺牲安全性等(这方面 MIT 有学者在研究,有兴趣的朋友可以看看他们的论文)。

一个好的加密设计方案,不仅仅方案本身要满足安全上严格的要求,解决上面提到的很多问题,还需要实用,容易扩展和维护。在数据加密变得越来越重要,系统越来越复杂的年代,如果资源允许,应该把加密服务独立出来,然后提供高性能,统一,简单又容易理解的接口来进行数据加密,让应用开发人员很方便的使用,这样他们只需要专注他/她所擅长的领域,而不需要去思考怎么解决安全问题,因为术业有专攻。好的加密方案应该把数据和秘钥的存储分开,并且把存储和运算分开,尤其在 SOA 架构下, 这可能跟很多传统的数据加密方法(比方说直接实用某个语言的内置库加密解密)非常不一样。很多解决方案往往密钥和被加密的数据同时存在一个服务中,结果就是如果那个服务被黑了,那就整个被黑了。下面就捡几个要点简略讲讲。

(1)为什么要把运算和存储的分离?很多需要被加密的数据,往往和具体商业逻辑数据一起,属于不同的服务,比方说支付信息属于支付服务,护照号码属于用户服务等。把运算和存储分离可以带来很多好处,比方说:
  • 加密服务变得简单和高效。因为不需要存储那些被加密的数据,加密服务系统不会很复杂,也不需要负责存储系统所带来的维护,扩展等诸多问题;
  • 加密服务的安全性能提高很多。因为运算和存储的分离,如果仅仅是被加密的数据泄露(比方说数据库被盗),那些数据就没法被解密,因为黑客没法从外部访问加密服务。如果仅仅是客户服务被黑,想要盗取大量数据并且通过加密服务来解密而不被发现也很难。如果仅仅是加密服务服务被黑,因为加密服务本身并不拥有数据,被泄露的数据也不会很多。只有当加密服务和客户服务同时被黑,才会泄露大量数据,而同时能够侵入加密服务和客户服务的难度要高很多很多;
  • 灵动性。因为数据属于客户服务,不同的客户可以对数据进行不同的处理,比方说不同的有效性规则,数据交易完整性等等。

(2)其次是 granular control。假设支付服务要求加密/解密信用卡号码,用户服务要求加密/解密护照号码,如何能保证用户服务不能加密/解密信用卡号码。这就需要解决两个基本问题:客户认证(authentication)和权限控制(authorization)。怎么做客户认证(authentication)?客户认证要知道每一个请求是谁发出的。因为只有知道客户是谁才可以进行权限检查。常用的有基于客户证书的(如 client certificate over TLS),基于 OAuth(开放授权)的,或者各种各样定制的方案,如基于 Curve25519 [链接14] 等等。笔者一般遵循两个原则:
  1. 是不是业界公认的。如果不是,最好避免。业界公认的解决方案往往经过过严格的检验, 评价,批评,经得起考验;
  2. 有没有广泛的类库支持,以及多语言的支持。如果有,可以节省大量的开发以及维护的时间和精力。

(3)数据监控和预警。为了审计以及安全的需求,一般要对加密服务做很多的数据监控,预警的工作。这样可以知道谁在访问这些数据,何时访问的,怎么访问的,访问的模式是怎么样的。监控系统还需要侦测异常的流量变化,进行流量控制以及快速的反攻击保护。

(4)最后,重中之重,怎么保护 root key?无论是用哪种 envelope encryption的变种,都会涉及到怎么保护 root key。这是一个很有意思的话题,但篇幅关系就不展开来讲了。常见的有用 secret sharing [链接15] 的一些变种方式,也有通过公证人公证整个过程藏在银行保险箱的,也有藏在创始人地下室的:)怎么来 bootstrap 和 deploy 这个 root key 到加密服务里也是一个非常有意思和挑战的问题。

总的来说,数据加密不是一件神秘的事情,但要做好非常不容易,需要一定的技术积累和资源的投入。做任何一个安全系统,风险都不小。圈内人第一反应是怀疑,因为职业病;圈外人很多也觉得重要,但不关心,或者说不知道怎么关心。但不管如何,无论是因为潜在的法律风险还是用户的信任风险,这种安全上的投入从长期来说一定是值得的。


链接1:http://money.cnn.com/2016/12/14/technology/yahoo-breach-billion-users/
链接2:http://money.cnn.com/2016/09/22/technology/yahoo-data-breach/?iid=EL
链接3:https://techcrunch.com/2016/05/18/117-million-linkedin-emails-and-passwords-from-a-2012-hack-just-got-posted-online/
链接4:https://www.wired.com/2015/08/happened-hackers-posted-stolen-ashley-madison-data/
链接5:http://money.cnn.com/2015/10/14/pf/target-pin-credit-card/index.html?iid=EL
链接6:http://krebsonsecurity.com/2013/10/adobe-breach-impacted-at-least-38-million-users/
链接7:https://en.wikipedia.org/wiki/RC4
链接8:https://en.wikipedia.org/wiki/MD5
链接9:https://en.wikipedia.org/wiki/Bcrypt
链接10:https://en.wikipedia.org/wiki/Key_derivation_function
链接11:https://en.wikipedia.org/wiki/Rainbow_table
链接12:https://en.wikipedia.org/wiki/Dictionary_attack
链接13:https://en.wikipedia.org/wiki/Message_authentication_code
链接14:https://en.wikipedia.org/wiki/Curve25519
链接15:https://en.wikipedia.org/wiki/Secret_sharing

Popular posts from 产品随想的博客

Apple's One-Dollar-a-Year Man, By Steve Jobs, 2000

(FORTUNE Magazine) – Now that Steve Jobs has showed his hand on Apple's Internet and system software strategies and dropped the "interim" from his title, other questions loom. He's always denied it, but isn't it true that his old company, Next, did wind up taking over Apple? Will there ever be an encore to the 15-year-old Macintosh? Short of that, does Apple have any plans to jump into the "Internet appliance" fray? Will Apple ever build computers for business people again? And what, pray tell, does Steve think of all these young Internet zillionaires? Let's ask. Practically every technology that your old company, Next, possessed when Apple acquired it in 1997 is now being used by Apple in some strategic way. This must seem like sweet vindication.  The thing about Next was that we produced something that was truly brilliant for an audience that our heart really wasn't into selling to--namely, the enterprise. I suppose if you were wr...

巴菲特致股东信-1980年

 笔记: 会计中对于下属股权公司的记账方式有3种: 持股50%以上,全部并入 持续20%--50%,则按持股比例并入 持股20%以下,则以实际收到的利润返还,计入报表 这种会计方式,会导致伯克希尔旗下,不少的企业,未能暴露实际的收益情况 对伯克希尔而言,对盈余的认定并非取决于持股比例是100%,50%,20%,5%或是1%,盈余的真正价值在于其将来再投资所能产生的效益 我们宁愿将所赚的盈余继续交由不受我们控制的人好好发挥,也不希望转由我们自己来浪费 高通货膨胀等于是对投入的资本额外课了一次税 翻译: https://xueqiu.com/6217262310/131837878 https://archive.ph/XMX5n  原文: Buffett’s Letters To Berkshire Shareholders 1980 巴菲特致股东的信 1980 年 Operating earnings improved to $41.9 million in 1980 from $36.0 million in 1979, but return on beginning equity capital (with securities valued at cost) fell to 17.8% from 18.6%. We believe the latter yardstick to be the most appropriate measure of single-year managerial economic performance. Informed use of that yardstick, however, requires an understanding of many factors, including accounting policies, historical ca...

产品爱好者周刊 第26期:PRISM, XKeyscore, Trust No One

  Products Gitea - Git with a cup of tea   https://gitea.io/en-us/ A painless self-hosted Git service. 自建Git服务,避免GitHub隐私侵犯 https://github.com/objective-see/LuLu LuLu is the free macOS firewall 监视Mac的出站流量,且阻断 OverSight   https://github.com/objective-see/OverSight OverSight monitors a mac's mic and webcam, alerting the user when the internal mic is activated, or whenever a process accesses the webcam. 监视是否有应用调用Mac的麦克风、摄像头 Mozilla Hubs   https://github.com/mozilla/hubs The client-side code for Mozilla Hubs, an online 3D collaboration platform that works for desktop, mobile, and VR platforms. 开源的多人虚拟空间,Mozilla打造,企业级VR诉求 数字移民   https://shuziyimin.org 关于内容源、工具的推荐,适合刚接入国际的新人 SimpleLogin   https://simplelogin.io/ 匿名邮箱工具,转发用,Michael Bazzell推荐 Telegram 群组、频道、机器人 - 汇总分享   https://congcong0806.github.io/2018/04/24/Telegram/#机器人-bot https://archive.ph/iJMBj 献给那些将来到Telegram的朋友 Design Patrick Wardle   https://www.instagram.com/patrickwardle/?hl=en 他的IG,摄影也精彩,审美...

《Becoming Steve Jobs》Chapter 16 Blind Spots, Grudges, and Sharp Elbows

Steve could be pretty thin-skinned when someone prominent criticized the aesthetics of his products. He took great umbrage that Neil would, as Steve put it, “pop off in public like that without coming to talk to us about his technical concerns first.” From that point on he had rebuffed all of Neil’s attempts to smoke the peace pipe. 有趣 He had blind spots, grating behavioral habits, and a tendency to give in to emotional impulse that persisted his entire life. These characteristics are often used to make the case that Steve was an “asshole” or a “jerk,” or perhaps simply “binary”—that odd adjective often used to convey the sense that he was half asshole/half genius from birth to death. These aren’t useful, interesting, or enlightening descriptions. What’s more illuminating is to take a look at the specific ways in which Steve failed to do an effective job of tempering some of his weaknesses and antisocial traits, and to consider how, when, and why some of them continued to flare up even...

Steve Jobs: `There's Sanity Returning', 1998

Nobody can doubt the charisma of Steven P. Jobs. The interim CEO of Apple Computer Inc., who returned to the company last July after his ignominious 1985 ouster, has brought back his legendary vision, impatience, and infectious passion for the Macintosh. Jobs spoke to Business Week Correspondent Andy Reinhardt in Apple's stark, fourth-floor boardroom, just after the company rolled out its new software strategy on May 11. Note: This is an extended, online-only version of the Q&A that appears in the May 25, 1998, issue of Business Week. Q: Now that you've introduced the new, bold-looking iMac, are you going to do some radically different products? A: There's a lot of talk about such things -- about handhelds, set-top boxes. A lot of computer companies have been searching for a consumer product. My view is that the personal computer has been the most successful consumer product of the last 10 years. What we have to do, what the industry stopp...

产品随想 | 周刊 第127期:晨光只开一刻钟,但比千年松,并无甚不同

Cherry Studio   https://github.com/CherryHQ/cherry-studio Cherry Studio is a desktop client that supports for multiple LLM providers. Support deepseek-r1 Aalto Repository beta   https://repo.aalto.fi/ Images, sounds and videos from Aalto University 这个系列,价值极高 Nokia Design Archive   https://nokiadesignarchive.aalto.fi/ 芬兰这个国家很了不起 对话影石刘靖康:两代未出现划时代的产品,就会沦为平庸的品牌   https://www.geekpark.net/news/308996 还挺喜欢这个创始人的,有一种海盗的内涵 从哈佛、明星创业者到酷家乐副总裁,苏奇的传奇   https://app.modaiyun.com/mdy/article/3FO4K4W0M259 WHO关于猫狗咬伤、抓伤的处理建议 动物咬伤: https://www.who.int/zh/news-room/fact-sheets/detail/animal-bites 狂犬病: https://www.who.int/zh/news-room/fact-sheets/detail/rabies 关于狂犬病的10个事实: https://www.who.int/zh/news-room/facts-in-pictures/detail/rabies INDIGO 新年直播(2025)   https://www.youtube.com/live/ZIgPvSDGAfY 对2024年AI发展的回顾部分特别好 Artab   https://github.com/get-artab/artab Get Inspired by the World's Greatest Artworks Every Time You Open a New Tab. Extension Available for Chrome, Edge, and...

A Sister’s Eulogy for Steve Jobs

I grew up as an only child, with a single mother. Because we were poor and because I knew my father had emigrated from Syria, I imagined he looked like Omar Sharif. I hoped he would be rich and kind and would come into our lives (and our not yet furnished apartment) and help us. Later, after I’d met my father, I tried to believe he’d changed his number and left no forwarding address because he was an idealistic revolutionary, plotting a new world for the Arab people. Even as a feminist, my whole life I’d been waiting for a man to love, who could love me. For decades, I’d thought that man would be my father. When I was 25, I met that man and he was my brother. By then, I lived in New York, where I was trying to write my first novel. I had a job at a small magazine in an office the size of a closet, with three other aspiring writers. When one day a lawyer called me — me, the middle-class girl from California who hassled the boss to buy us health insurance — and said his cl...

SS机场常用服务器线路微普及

原文link:https://www.duyaoss.com/archives/57/   为何写这么个帖子? 更新时间:2019-11-29 由于机场用户增多,很多新用户压根不懂节点上面的名字代表什么,也不知道什么服务器比较适合自己,不懂什么是原生,等等。 所以开一个小帖,稍微介绍一下比较常见的服务器, 专业知识有限,所以只是给小白们介绍一下,其实我也很白,各位大佬见笑了。 在这里尤其感谢 Sukka 苏卡卡大佬和喵酱指导,以及 Nexitally 佩奇提供的资料介绍,否则我真不知道从哪儿开始动笔。后面地区内容都是佩奇帮忙码出来的。时间有限,慢慢再继续填充和修整 本文仅仅是抛砖引玉写一些机场主们告知我的 ISP、IDC 的体验,仅供参考。网络环境每天都在变化,今天飞快的服务器明天有可能龟速,有写的不对或者过时的地方还望大家指正。所以本文也算是一些机场主们把曾经踩过的坑分享给大家吧。(本来是想给小白写服务器介绍的,佩奇大佬写着写着就专业惯性的转到了商家哈哈哈,这是一个悲伤的故事) 测速图 Telegram 频道: https://t.me/DuyaoSS 主用链接: DuyaoSS - 毒药机场简介博客 常见名词: IPLC: "International Private Leased Circuit"的缩写,即“国际专线”。不过大部分机场通常看到的iplc,都只是阿里的经典网络,跨数据中心内网互通,阿里内网,并不是严格意义的iplc专线;当然也有其他渠道的,或真iplc,不过比较少。阿里云的内网互通底层原理是通过采购多个点对点的iplc专线,来连接各个数据中心,从而把各个数据中心纳入到自己的一套内网里面来。这样做有两个好处,其一是iplc链路上的带宽独享,完全不受公网波动影响,其二是过境的时候不需要经过GFW,确保了数据安全且不受外界各种因素干扰。但是需要注意一下阿里云的iplc也是有带宽上限的,如果过多的人同时挤到同一条专线上,峰值带宽超过专线的上限的话也同样会造成网络不稳定。其他渠道购买到的iplc价格很高,阿里云内网这种性价比超高这种好东西且用且珍惜。 IEPL国际以太网专线(International Ethernet Private Line,简称IEPL),构建于MSTP设备平台上...

产品随想 | 周刊 第130期:集结信徒,而非官僚

On Dyson, techno-centric design and social consumption   https://2earth.github.io/website/20250707.html 如何創造偉大的事物   https://ryolu.notion.site/1610a94b9c108079a95be4362afd4a26 集結信徒,而非官僚 Reflections on OpenAI   https://calv.info/openai-reflections 创业架构 Shui   https://github.com/rock-zhang/Shui 好好喝水 https://www.ghibli.jp/works/ 吉卜力作品的高清图 From Skeuomorphic to Liquid Glass: Apple's Strategic Bet on the Post-Touch Future   https://omc345.substack.com/p/from-skeuomorphic-to-liquid-glass 迄今为止关于苹果Liquid Glass变革的解读,最好的一篇 The Nueva School   https://en.wikipedia.org/wiki/The_Nueva_School?useskin=vector 看起来是很酷的一个学校 The Barbican   https://arslan.io/2025/05/12/barbican-estate/ 史蒂夫·乔布斯希望你阅读的 9 本书   https://www.douban.com/doulist/147158849/ 《禅者的初心》里有句话:“做任何事,其实都是在展示内心的天性,这是我们存在的唯一理由。” Chuck Feeney was one of the greatest philanthropists ever   https://www.gatesnotes.com/Remembering-Chuck-Feeney 慈善家 My new deadline: 20 years to give away virtually all my wealt...

Interview with Steve Jobs, WGBH, 1990

Interviewer: what is it about this machine? Why is this machine so interesting? Why has it been so influential? Jobs: Ah ahm, I'll give you my point of view on it. I remember reading a magazine article a long time ago ah when I was ah twelve years ago maybe, in I think it was Scientific American . I'm not sure. And the article ahm proposed to measure the efficiency of locomotion for ah lots of species on planet earth to see which species was the most efficient at getting from point A to point B. Ah and they measured the kilocalories that each one expended. So ah they ranked them all and I remember that ahm...ah the Condor, Condor was the most efficient at [CLEARS THROAT] getting from point A to point B. And humankind, the crown of creation came in with a rather unimpressive showing about a third of the way down...