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

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