跳至主要内容

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

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

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

Foobar2000 组件安装教程

 原作者 博客地址   汉化作者 Asion博客   关于foobar 2000的一些资源 前言 foobar2000 由于其软件架构特点以及开放的姿态,使得第三方很容易开发组件(component)来拓展它的功能。由于在官网下载的默认安装文件只带了少量几个默认的组件,满足不了使用的需求,例如:默认不带 ape,tta,tak 等音频文件格式的解码器,很多无损压缩格式音乐没法播放。所以自己下载安装组件是必备的基本技能。 foobar2000 的中文汉化版(Asion 汉化)为了方便使用,集成了无损压缩文件解码器以及一些其它有用的插件,安装时选上即可,不喜欢折腾的建议使用汉化版。 这里组件指的是 foobar2000 标准组件(*.dll 文件),而非 vst 插件等其它插件,姑且把组件分为两类: 官方组件: 英文版安装包自带,安装时可选择; 第三方组件:非官方自带的组件 除了 foo_input_std.dll 和 foo_ui_std.dll 这两个组件是必须的外,其它的所有组件都 非必需 的,可以随需要增删。第三方组件可以去 官网 、 官方论坛 或者 官方 wiki 去找,也可以去贴吧等地逛逛。 下载 还是要强调一下,这里说的是 foobar2000 component ,不是中文网上通常说的 vst 插件。 下载好的组件包一般是 xxx.zip 或 xxx.fb2k-component 格式的文件,也有用 7z 打包的。前两种都是 zip 压缩(只要把 fb2k-component 改成 zip 文件就变成了 zip: 包)。标准状况下压缩包里的内容结构应该是 xxx.zip yyy.dll README.txt (可能没有) LICENCE.txt (可能没有) (其它杂七杂八) 除少数外一般只有一个 xxx.dll 文件.一定要注意压缩包结构不能是: xxx.zip yy folder (文件夹) zzz.dll … 否则要解压缩,提取那个 dll 文件。 安装 方法一(推荐) 打开 foobar2000 的菜单 文件 > 首选项(file >preferences) 的 组件(components...

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

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

2018各行业应届生薪资不完全样本往期汇总-职场红领巾

文章来源自职场红领巾公众号2018.4.21日推送,在此表示感谢 产品岗 百度商业产品 14K*14 拼多多产品管培 12K*14 今日头条产品 16K*18 头条PM整个Package接近300K/年 美团产品Offer 14K*16 base上海 百度产品研究生 11.5K*14.6 base 上海 京东产品17K*13 百度产品 220K/年 网易 产品培训生 硕士 15K*18 SP base杭州 不知名互联网公司校招PM 12K*15 base北京 技术岗 微软 软件工程师 本科 260K/年 蚂蚁金服算法工程师 20K*16 拼多多开发本科400K/年 商汤科技本科技术岗 14K/月 税前 海康威视研究院 算法工程师 220K/年 微信算法岗 SP 360K/年 的package 今日头条 程序员 研究生 10K/月 base北京 滴滴程序员 16K*16 亚马逊 小四年经验 研发 50K/月 Facebook应届毕业生  软件开发工程师   打包 115k$/年(30%-40%税) base湾区 京东算法 普通Offer 234K/年 运营岗 滴滴北京运营岗 硕士 12K*15 奖金另算 网易游戏运营 150K/年 左右 网易运营 8K*13(奖金0~3个月) 网易新闻运营8K/月 腾讯游戏运营 本科6K/月 上海京东时尚本科8K/月 京东运营岗 11K/月 base北京亦庄总部 今日头条 渠道营销运营 6K/月(加房补) 网易考拉 活动运营 13K*16 OFO城市运营管培13K*14 爱范儿运营 8K/月 滴滴长三角某二线城市运营管培生 薪资 7.6K*13 +每个月40%绩效 货车帮 数据运营 12K/月 卡宾电商 管培 10K/月 含浮动绩效 曹操专车 运营管培生  加各种补贴税前5.4K/月  base杭州 京东金融海龟回来8K/月 北京蓝港互动...

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

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

产品随想 | 陪读《Make Something Wonderful:Steve Jobs in his own words》1976-1996

  There’s lots of ways to be, as a person. And some people express their deep appreciation in different ways. But one of the ways that I believe people express their appreciation to the rest of humanity is to make something wonderful and put it out there. And you never meet the people. You never shake their hands. You never hear their story or tell yours. But somehow, in the act of making something with a great deal of care and love, something’s transmitted there. And it’s a way of expressing to the rest of our species our deep appreciation. So we need to be true to who we are and remember what’s really important to us. ──Steve, 2007 Introduction by Laurene Powell Jobs Much of what’s in these pages reflects guiding themes of Steve’s life: his sense of the worlds that would emerge from marrying the arts and technology; his unbelievable rigor, which he imposed first and most strenuously on himself; his tenacity in pursuit of assembling and leading great teams; and perhaps, above all,...

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