跳至主要内容

有关密码学 Cryptography

% Crypto 101% CUI Hao

密码学 Cryptography



加密:军事、商业保密、身份认证、日记...
  • 计算机出现前:加密人类语言文字
  • 计算机出现后:加密比特流(ASCII文本、网络协议)

古典替换式密码

凯撒密码

文本中每个字母在字母表上后移 k 个位置。

ATTACK -> DWWDFN (k=3)
IBM -> HAL (k=-1)

改进

重新排列字母表(单字母替换):


alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
  map to: RCPWUXNQBZFMYTLIEGVDJOAKHS
 example: ATTACK -> RDDRPF
"密码组合"有 26! 种之多。

维吉尼亚密码

每k个字母一组,与长度k的密码做加法


  ATTACKATDAWN (plaintext)
+ LEMONLEMONLE (key: LEMON)
= LXFOPVEFRNHR (ciphertext)
多个字母的凯撒密码。

替换式密码

substitution cipher

单字母替换/多字母替换/密码本...
  • 加密算法:映射
  • 密码:描述映射关系
  • 解密算法:反过来映射

另一种设计方案



也许是中国人发明的吧:
群书万卷常暗诵, 主人顾盼千金重。 药物楚老渔商市, 丸剑跳踯霜雪浮。
移位式密码 (transposition cipher)

列移位密码

按密码重新排列文本各列,然后竖着读出来:


 KEY: 6 3 2 4 1 5
TEXT: W E A R E D      CT:
      I S C O V E  ==> EVLNEACDTKESEAQROFOJDEECUWIREE
      R E D F L E 
      E A T O N C 
      E Q K J E U 
      (WE ARE DISCOVERED. FLEE AT ONCE)

密码分析

  • 系统是否安全?
  • 如何破解加密?

密码攻击 (COA)

仅仅获取到密文(ciphertext only attack),如何破解?
LetterFreq.
e12.702%
t9.056%
a8.167%
.............
样本越多,猜测越准确。

密码攻击 (CPA)

二战中,英国人对德国密码系统的种花攻击 (gardening):
  • 在A地放地雷
  • 截取密文 ...FRQALFFDSFGRE...
  • 在B地放地雷
  • 截取密文 ...FRQALFFDSFUGA...
选择明文攻击 (chosen plaintext attack):
试探设定的明文的加密结果,获取映射关系。

一致性破坏

不破解明文,仍可以有意地篡改解密结果,欺骗接收方:
  • PT: JOHN 1000; JACK 2000
  • CT: [01] [02]; [03] [04]
  • CT (modified): [01] [04]; [03] [02]
  • PT (modified): JOHN 2000; JACK 1000

极端的安全性



  • 经常更换密码 -> 一次性密码
  • 更大的密码空间 -> 超长的密码
  • 防止字典猜测 -> 密码随机选择

One Time Pad

维吉尼亚密码:使用和明文同样长随机字母串作为密码
计算机的维吉尼亚密码:加法 -> 按位异或


            U        S        T        C
 plaintext: 01010101 01010011 01010100 01000011
       key: 10110111 11110111 10111110 10011010
ciphertext: 11100010 10100100 11101010 11011001

OTP 为什么安全

非OTP:错误的密码 -> 无效的结果(乱码,无意义文本)
OTP:错误的密码 -> 可能得到看似正确的结果
  • 不可能被COA(密文得到很多看似正确的结果)
  • 不可能被CPA(一次性)


ciphertext: 11100010 10100100 11101010 11011001
 wrong key: 10101010 11110001 10111001 10001101
wrong text: 01001000 01010101 01010011 01010100
            H        U        S        T

一次性

异或运算的性质:(p1 xor k) xor (p2 xor k) = p1 xor p2
如果多次使用OTP,则可以通过密文间异或消去密码
已知的漏洞:
  • WIFI加密(WEP)
  • MS-PPTP(MSCHAP-v1)

实用性

  • Q: 如何安全地传送一次性密码?
  • A: 加密传输密码?死循环...
现实场景:密码应当便于交换和记录...
(其实可以通过量子密码实现绝对的OTP)

实际的加密方案

密码本模式

electronic code book, ECB

各种加密算法
  • 都可以归于替换式密码和移位式密码的复杂组合
  • 归根结底,就是多"字母"替换
实际的例子:
  • DES: 64位的替换单位
  • AES: 128/192/256 位的替换单位
缺点:同前

密码块链模式

cipher block chaining

为了避免ECB模式中移动块顺序篡改数据的风险:
  • 第i块明文加密之前,和上一块密文异或操作
  • 第1块明文和随机生成的初始向量 (Initial Vector) 做异或
既避免了篡改数据,还可以防止COA、CPA(相同数据两次加密结果不同)

流式密码

stream cipher

块密码中,如果任何一位传输错误,则会影响整个块(甚至下一块)的解密。
基于OTP的思路,设计一种伪OTP:
  • 密钥作为随机数种子
  • 通过伪随机数算法生成伪OTP密钥
  • 按照OTP的流程进行加密
为了避免重用攻击,也可以引入初始向量增加随机性。

其他模式

思考:文件系统加密,上述方法是否实用?

非对称加密



对称加密的矛盾:
  • 密钥必须被安全地保护起来
  • 密钥必须被分发给需要的用户
是否存在一种算法:
  • Decrypt(Encrypt(P, K1), K2) = P
  • 很难从K1推出K2
...以后再讲 (例:RSA)

生日悖论



如果一个房间里有23个或23个以上的人,那么至少有两个人的生日相同的概率要大于50%。


   n(p, d) = sqrt(2d ln(1/(1-p)))
 n(0.5, d) = 1.2 sqrt(d)
n(0.99, d) = 3.0 sqrt(d)
不要低估暴力穷举破解 (brute-force) 的危险性。

最后的话

公开算法

Kerckhoff 原则:密码学算法应当是公开的
  • 保密的是数据和密钥
  • 算法可以得到更多研究者的检验
不要对自己的轮子保有过度的信心。

密码学其他科技

  • 数字签名
  • DRM (蓝光加密算法 AACS)
  • 同态加密 (Homomorphic encryption)
  • ......

the End

谨慎采信本 slides 的内容:
似乎本 slides 没有出现 Alice 和 Bob




Popular posts from 产品随想的博客

产品随想 | 周刊 第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...

《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...

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...

写给大家看的中文排版指南

作者:Hindy 原文地址: http:// zhuanlan.zhihu.com/uici rcle/20506092 前言:很遗憾,我们的周围充斥着大量排版丑陋的文章。我国的字体排印与日本、美国等设计强国差距实在太大。我希望能够做些力所能及的小事,让更多人意识到“设计”的价值和其必要性,创造更美好的视觉环境。本文旨在帮助普及、提升大家对文字排版的认识,让大家在平时的学习工作中能有更专业的文字排版素养。 必看人群: 设计师、编辑、作家、撰稿人、教师、学生 目录: 1. 中文排版 1.1 引号 1.2 省略号与破折号 1.3 行首行尾禁则 2. 西文排版基础 2.1 西文撰写基础 2.2 西文标点相关 2.3 斜体的用法 2.4 大小写的区别 3. 中西文混排 3.1 基础原则 3.2 标点相关 1. 中文排版 1.1 引号 我国国家标准要求弯引号,个人建议使用直角引号。 示例:你竟然喜欢“苹果表”? 引号中再用引号使用双直角引号。 示例:我问他,“你竟然喜欢‘苹果表’?” 当引号表示讽刺、反语暗示时,使用弯引号(用法参考“西文排版”部分)。 示例:说真的,我也很 “喜欢”“苹果表”哦。 1.2 省略号(删节号)与破折号 省略号占两个汉字空间,包含六个点。 正确示例:中国设计还有太长路要走…… 错误示例:中国设计还有太长路要走… 破折号占两个汉字空间。 示例:中国设计还有太长路要走──加油罢。 1.3 行首行尾禁则 点号(顿号、逗号、句号等)、结束引号、结束括号等,不能出现在一行的开头。 错误示例: 排版时注意某些 符号不能在行首 ,别弄错了。 正确示例: 排版时注意某些 符号不能在行首, 别弄错了。 开始引号、开始括号、开始双书名号等,不能出现在一行的结尾。 错误示例: 她对我们说:“ 这书太赞了。” 正确示例: 她对我们说: “这书太赞了。” 2. 西文排版基础 2.1 西文撰写基础 句首字母大写。 单词间留空格。 示例:Have a question? 2.2 西文标点相关 点号后加一个空格(如逗号、句号等)。 示例:Hello everyone! Welcome to my blog....

360T7 刷机步骤及固件

https://cmi.hanwckf.top/p/360t7-firmware/   360T7的固件支持由immortalwrt-mt798x项目提供支持,请参考: https://cmi.hanwckf.top/p/immortalwrt-mt798x https://github.com/hanwckf/immortalwrt-mt798x 刷机步骤 参考 此处 的办法开启原厂固件的UART和telnet功能 在以下链接下载360T7测试固件(纯净版,无任何插件) https://wwd.lanzout.com/b0bt9idwd 密码:ezex (此固件已过时,请选择其它更新的固件) 接下来将刷入修改版uboot。修改版uboot的优点有: 固件分区可达108MB,原厂uboot只能使用36M 自带一个简单的webui恢复页面 到以下仓库的Release页面下载uboot,目前暂时仅支持360T7,后续将支持更多mt798x路由器。 推荐使用 mt7981_360t7-fip-fixed-parts.bin , fixed-parts 代表uboot分区表在编译期间固定,不会随着uboot环境变量变化。 https://github.com/hanwckf/bl-mt798x/releases/latest 将 mt7981_360t7-fip-fixed-parts.bin 通过HFS等方式上传到路由器,使用以下命令刷入uboot mtd write mt7981_360t7-fip-fixed-parts.bin fip 确认刷入完毕后,拔掉路由器电源。然后将电脑的IP地址设置为固定的 192.168.1.2 ,接着按住路由器的RESET按钮后通电开机,等待8s后用浏览器进入 192.168.1.1 在uboot恢复页面选择要刷入的固件。immortalwrt-mt798x目前编译两个版本的360T7固件。 建议修改版uboot直接使用 immortalwrt-mediatek-mt7981-mt7981-360-t7-108M-squashfs-factory.bin ,两种固件区别如下: mt7981-360-t7-108M 为108M固件分区,原厂uboot不可启动,需要修改版u...

黑客技术论坛推荐

原文来自知乎, 世界各大黑客技术论坛TOP排行榜 ,表示感谢 这份名单基本上囊括了目前世界上各大最佳黑客技术论坛。

Steve Jobs at 44, By Michael Krantz, 1999

Differences and Similarities Between Apple and Pixar Apple turns out many products--a dozen a year; if you count all the minor ones, probably a hundred. Pixar is striving to turn out one a year. But the converse of that is that Pixar's products will still be used fifty years from now, whereas I don't think you'll be using any product Apple brings to market this year fifty years from now. Pixar is making art for the ages. Kids will be watching Toy Story in the future. And Apple is much more of a constant race to continually improve things and stay ahead of the competition.  His Role At Pixar At Pixar my job is to help build the studio and recruit people and help create a situation where they can do the best work of their lives. And to some degree it's the same at Apple. But at Pixar, I don't direct the movies, whereas at Apple probably, if I had to pick a role out of a film production, I'd be the director. So it...

产品随想 | 周刊 第40期:献出心脏,直到高墙倒塌

标题来自本期的一个链接,觉得充满理想主义,献给大家! Products Movie Robot   https://github.com/pofey/movie_robot 定时自动从豆瓣电影的想看、在看、看过中获取影音信息,然后去PT站(支持多家站点)自动检索种子,找到最佳资源后按豆瓣电影分类提交到BT下载工具下载。在下载前,会自动检查你的Emby中是否已经存在。 工具超赞,一次搭建,可以很长一段时间躺平,但针对我这样对4K、HDR有高要求用户,可能不大行 Plash   https://github.com/sindresorhus/Plash Make any website your Mac desktop wallpaper 将任意网站,定义为Mac桌面 https://github.com/sindresorhus/Plash/issues/1,网友给的一些网站,也比较有意思 HomeBank   http://homebank.free.fr/zh/index.php 自由、开源的会计软件 GnuCash   https://www.gnucash.org/ https://github.com/Gnucash/gnucash 自由开源,多平台的财务软件,且有很好看的官方App,since 1998 beancount   https://github.com/beancount/beancount Beancount: Double-Entry Accounting from Text Files. 纯文本的复式记账工具 rimerc: rimer's dictionary & config   https://github.com/Bambooin/rimerc Rime输入法在各个平台的配置 Natural Selection Labs   https://github.com/NaturalSelectionLabs DIYGod主导的聚焦Web3的项目,非常有雄心,保持关注! PineTab   https://www.pine64.org/pinetab/ The Open Source ARM 64-bit Tablet 自由开源的64位平板电脑(在28...

产品随想 | 周刊 第128期:将时间转化为知识和体验

自行车棚效应:我们为什么在小事上纠结,却对大事视而不见?   https://limboy.me/posts/bike-shedding Benz-Patent   https://www.mercedes-benz.com/en/innovation/milestones/benz-patent-motor-car/ 豐田博物館   https://toyota-automobile-museum.jp/tw/ 值得一去的汽车博物馆 一人公司   https://github.com/cyfyifanchen/one-person-company one-person-company, 一人公司 AI 工具系列 wujiaxian   https://wujiaxian.com/ 可能吧创始人的个人Blog,介绍自己的方式,很值得学习 日本京都10大設計熱點!「建築大師貝聿銘操刀美術館,皆川明設計旅館、選物店,還有全球最美的藍瓶咖啡店舖!」   https://www.elle.com/tw/life/style/g33846398/2020-kyoto-10-hotspots/ 京都真是非常美 Henry L. Stimson   https://en.wikipedia.org/wiki/Henry_L._Stimson?useskin=vector While Kyoto may have satisfied the military criteria for a useful target, Stimson objected, declaring in a meeting if the Interim Committee on June 1, 1945, "...there was one city that they must not bomb without my permission and that was Kyoto." 人性的努力保护了京都 Brooke Astor, 105, Aristocrat of the People, Dies   https://www.nytimes.com/2007/08/14/obituaries/14astor.html 值得尊敬...

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...