Skip to main content

Python 的加密库入门 | Linux 中国


密码学俱乐部的第一条规则是:永远不要自己发明密码系统。密码学俱乐部的第二条规则是:永远不要自己实现密码系统:在现实世界中,在实现以及设计密码系统阶段都找到过许多漏洞。
Python 中的一个有用的基本加密库就叫做 cryptography。它既是一个“安全”方面的基础库,也是一个“危险”层。“危险”层需要更加小心和相关的知识,并且使用它很容易出现安全漏洞。在这篇介绍性文章中,我们不会涵盖“危险”层中的任何内容!
cryptography 库中最有用的高级安全功能是一种 Fernet 实现。Fernet 是一种遵循最佳实践的加密缓冲区的标准。它不适用于非常大的文件,如千兆字节以上的文件,因为它要求你一次加载要加密或解密的内容到内存缓冲区中。
Fernet 支持对称symmetric(即密钥secret key)加密方式*:加密和解密使用相同的密钥,因此必须保持安全。
生成密钥很简单:
  1. >>> k = fernet.Fernet.generate_key()
  2. >>> type(k)
  3. <class 'bytes'>
这些字节可以写入有适当权限的文件,最好是在安全的机器上。
有了密钥后,加密也很容易:
  1. >>> frn = fernet.Fernet(k)
  2. >>> encrypted = frn.encrypt(b"x marks the spot")
  3. >>> encrypted[:10]
  4. b'gAAAAABb1'
如果在你的机器上加密,你会看到略微不同的值。不仅因为(我希望)你生成了和我不同的密钥,而且因为 Fernet 将要加密的值与一些随机生成的缓冲区连接起来。这是我之前提到的“最佳实践”之一:它将阻止对手分辨哪些加密值是相同的,这有时是攻击的重要部分。
解密同样简单:
  1. >>> frn = fernet.Fernet(k)
  2. >>> frn.decrypt(encrypted)
  3. b'x marks the spot'
请注意,这仅加密和解密字节串。为了加密和解密文本串,通常需要对它们使用 UTF-8 进行编码和解码。
20 世纪中期密码学最有趣的进展之一是公钥public key加密。它可以在发布加密密钥的同时而让解密密钥保持保密。例如,它可用于保存服务器使用的 API 密钥:服务器是唯一可以访问解密密钥的一方,但是任何人都可以保存公共加密密钥。
虽然 cryptography 没有任何支持公钥加密的安全功能,但 PyNaCl 库有。PyNaCl 封装并提供了一些很好的方法来使用 Daniel J. Bernstein 发明的 NaCl 加密系统。
NaCl 始终同时加密encrypt签名sign或者同时解密decrypt验证签名verify signature。这是一种防止基于可伸缩性malleability-based的攻击的方法,其中攻击者会修改加密值。
加密是使用公钥完成的,而签名是使用密钥完成的:
  1. >>> from nacl.public import PrivateKey, PublicKey, Box
  2. >>> source = PrivateKey.generate()
  3. >>> with open("target.pubkey", "rb") as fpin:
  4. ... target_public_key = PublicKey(fpin.read())
  5. >>> enc_box = Box(source, target_public_key)
  6. >>> result = enc_box.encrypt(b"x marks the spot")
  7. >>> result[:4]
  8. b'\xe2\x1c0\xa4'
解密颠倒了角色:它需要私钥进行解密,需要公钥验证签名:
  1. >>> from nacl.public import PrivateKey, PublicKey, Box
  2. >>> with open("source.pubkey", "rb") as fpin:
  3. ... source_public_key = PublicKey(fpin.read())
  4. >>> with open("target.private_key", "rb") as fpin:
  5. ... target = PrivateKey(fpin.read())
  6. >>> dec_box = Box(target, source_public_key)
  7. >>> dec_box.decrypt(result)
  8. b'x marks the spot'
最后,PocketProtector 库构建在 PyNaCl 之上,包含完整的密钥管理方案。

via: https://opensource.com/article/19/4/cryptography-python

Comments

Popular posts from this blog

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checking a shared sec

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into /opt/course/1/context_default_no_kubectl.sh , but without the use of k

标 题: 关于Daniel Guo 律师

发信人: q123452017 (水天一色), 信区: I140 标  题: 关于Daniel Guo 律师 关键字: Daniel Guo 发信站: BBS 未名空间站 (Thu Apr 26 02:11:35 2018, 美东) 这些是lz根据亲身经历在 Immigration版上发的帖以及一些关于Daniel Guo 律师的回 帖,希望大家不要被一些马甲帖广告帖所骗,慎重考虑选择律师。 WG 和Guo两家律师对比 1. fully refund的合约上的区别 wegreened家是case不过只要第二次没有file就可以fully refund。郭家是要两次case 没过才给refund,而且只要第二次pl draft好律师就可以不退任何律师费。 2. 回信速度 wegreened家一般24小时内回信。郭律师是在可以快速回复的时候才回复很快,对于需 要时间回复或者是不愿意给出确切答复的时候就回复的比较慢。 比如:lz问过郭律师他们律所在nsc区域最近eb1a的通过率,大家也知道nsc现在杀手如 云,但是郭律师过了两天只回复说让秘书update最近的case然后去网页上查,但是上面 并没有写明tsc还是nsc。 lz还问过郭律师关于准备ps (他要求的文件)的一些问题,模版上有的东西不是很清 楚,但是他一般就是把模版上的东西再copy一遍发过来。 3. 材料区别 (推荐信) 因为我只收到郭律师写的推荐信,所以可以比下两家推荐信 wegreened家推荐信写的比较长,而且每封推荐信会用不同的语气和风格,会包含lz写 的research summary里面的某个方面 郭家四封推荐信都是一个格式,一种语气,连地址,信的称呼都是一样的,怎么看四封 推荐信都是同一个人写出来的。套路基本都是第一段目的,第二段介绍推荐人,第三段 某篇或几篇文章的abstract,最后结论 4. 前期材料准备 wegreened家要按照他们的模版准备一个十几页的research summary。 郭律师在签约之前说的是只需要准备五页左右的summary,但是在lz签完约收到推荐信 ,郭律师又发来一个很长的ps要lz自己填,而且和pl的格式基本差不多。 总结下来,申请自己上心最重要。但是如果选律师,lz更倾向于wegreened,