跳至主要内容

有关密码学 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 产品随想的博客

产品随想 | 周刊 第69期:Do not go gentle into that good night

Products Windows Apps That Amaze Us   https://amazing-apps.gitbook.io/windows-apps-that-amaze-us/ 令人精细的Windows App 文物出版社   https://book.douban.com/press/2456/ 这是一个宝藏出版社,出品书籍质量非常高,大开眼界 blind   https://www.teamblind.com/ 老外的匿名职场社交工具,挺有意思,看看硅谷的meme 中国科学技术大学测速网站   https://test.ustc.edu.cn/ 看着还不错,挺靠谱的 底层代码是LibreSpeed   https://github.com/librespeed/speedtest 能不能好好说话?   https://github.com/itorr/nbnhhsh 也是我的一个痛点 Tree Style Tab (aka TST)   https://github.com/piroor/treestyletab 一个超强的浏览器扩展插件,树状呈现浏览器标签 Failory Pitch Decks   https://www.failory.com/pitch-deck 超级多的融资计划投资板,Pitch Book AutoCut   https://github.com/mli/autocut 用文本编辑器剪视频 全网漫游指南   https://tagly.notion.site/tagly/a333efd8c3e54e12b123acd541e8d3e6 数字时代的指引,希望他们成功 IT eBooks   https://it-ebooks.info/ IT书籍下载 ToastFish   https://github.com/Uahh/ToastFish 一个利用摸鱼时间背单词的软件。 利用Win10通知栏,出现、背单词 Ideas 沈向洋:IDEA 如何找到创新的「甜区」   https://mp.weixin.qq.com/s/OlI5VUxQKU_ijWZClQCG0Q AIGC How Did Nor...

Interview at the All Things Digital D5 Conference, Steve and Bill Gates spoke with journalists Kara Swisher and Walt Mossberg onstage in May 2007.

Kara Swisher: The first question I was interested in asking is what you think each has contributed to the computer and technology industry— starting with you, Steve, for Bill, and vice versa. Steve Jobs: Well, Bill built the first software company in the industry. And I think he built the first software company before anybody really in our industry knew what a software company was, except for these guys. And that was huge. That was really huge. And the business model that they ended up pursuing turned out to be the one that worked really well for the industry. I think the biggest thing was, Bill was really focused on software before almost anybody else had a clue that it was really the software that— KS: Was important? SJ: That’s what I see. I mean, a lot of other things you could say, but that’s the high-order bit. And I think building a company’s really hard, and it requires your greatest persuasive abilities to hire the best ...

产品随想 | 周刊 第43期:历史上的今天

Products Huberman Lab   https://hubermanlab.com/ 一款聚焦于健康的播客 今日热榜   https://tophub.today/ 聚合展示,国内各热门榜单,对跟进热点非常有帮助,热点运营的好帮手 SketchyBar   https://github.com/FelixKratz/SketchyBar A highly customizable macOS status bar replacement Mac菜单栏定制 自定义程度很高,看作者展示的案例,暂时没想出这样的好处(不过应用本身的编辑,确实也没啥意义)生命在于折腾吧! Thanks-Mirror   https://github.com/eryajf/Thanks-Mirror 整理记录各个包管理器,系统镜像,以及常用软件的好用镜像,Thanks Mirror。 Musicn   https://github.com/zonemeen/musicn 一个下载高品质音乐的命令行工具,音乐来源: 咪咕 Planet Minecraft A creative Minecraft community fansite sharing maps, minecraft skins, resource packs, servers, mods, and more. 里面有很多动人的故事 可能是世界上最大的Minecraft社区,从2010年至今 The Uncensored Library   https://www.uncensoredlibrary.com/en blockworks   https://www.blockworks.uk/ "Distinctive maps for Minecraft that have educated players and risen to the level of art" 游戏也可以让人有更高的实现,而不仅仅是沉迷其中,国外游戏厂商比我们做的好太多 Minecraft_Memory_Bypass_GUI   https://github.com/xingchuanzhen/Minecraft_Memory_Bypass_GUI 绕过Minecraft...

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

犀流研习班第十期001-2讲听课笔记

一. 杂谈   1. Max  逼真的效果图和视频   2. SU  显示模式,费力可以作出好看的效果图   3. Maya  支持Nurbs,但不快捷,对CAD导出也不方便

内网域名访问内网服务器

部门ftp服务器和远程服务器内网域名无法访问问题困扰我好久,钻研了几天,终于明白了一些,和大家做一个分享, 原帖子在这里 ,表示感谢

《逢いたくていま》──仁医主题曲

原始链接: 听歌学日语 | 唱哭很多人的《逢いたくていま》 あ いたくていま - MISIA 现在好想见你- MISIA 初 はじ めて 出会 であ った 日 ひ のこと  覚 おぼ えてますか 第一次相遇的那天 你是不是还记得呢?   過 す ぎ 行 ゆ く 日 ひ の 思 おも い 出 で を  忘 わす れずにいて 那些过去日子的回忆 我一直没有有忘记   あなたが 見 み つめた  全 すべ てを  感 かん じていたくて 凝视着你 这一切的全部 我都想要感觉   空 そら を 見上 みあ げた 抬头仰望天空   今 いま はそこで  私 わたし を  見守 みまも っているの? 你到现在是否还在那里守护着我?   教 おし えて… 请你告诉我 今 いま   逢 あ いたいあなたに 现在好想见你 伝 つた えたい 事 こと がたくさんある 有好多想要告訴你的事情   ねえ  逢 あ いたい  逢 あ いたい 呐 好想见你 好想见你   気 き づけば  面影 おもかげ   探 さが して  悲 かな しくて 如果能注意到的话 你的面容 是在寻找着 还是悲伤着 どこにいるの?  抱 だ きしめてよ 到底在哪里呢? 好想抱紧你 私 わたし はここにいるよ ずっと 我 会一直在这里 一直等你 もう 二度 にど と 逢 あ えないことを  知 し っていたなら 如果能早点知道 已经再也无法相见   繋 つな いだ 手 て をいつまでも  離 はな さずにいた 我会牵在一起的手 永远都不会放开   『ここにいて』と そう 素直 すなお に  泣 な いていたなら 如果当初诚实哭泣地告诉你『留在我身边』的话   今 いま もあなたは  変 か わらぬまま 现在的你是否也依然不变地   私 わたし の 隣 とな りで ...

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