In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to encrypt files with Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
A useful basic encryption library in Python is called cryptography. It is not only a basic library of "security", but also a "danger" layer. "danger" layer needs to be more careful and relevant knowledge, and the use of it is prone to security vulnerabilities. In this introductory article, we will not cover anything in the "danger" layer!
The most useful advanced security feature in the cryptography library is an Fernet implementation. Fernet is a practical standard for encrypting buffers. It is not suitable for very large files, such as files with gigabytes or more, because it requires you to load the contents to be encrypted or decrypted into the memory buffer at once.
Fernet supports symmetric (symmetric) (that is, key (secret key)) encryption *: encryption and decryption use the same key, so security must be maintained.
Generating a key is simple:
> k = fernet.Fernet.generate_key () > type (k)
These bytes can be written to files with appropriate permissions on secure machines.
With the key, encryption is also easy:
> frn = fernet.Fernet (k) > encrypted = frn.encrypt (b "x marks the spot") > encrypted [: 10] broomgAAAAABb1'
If you encrypt it on your machine, you will see a slightly different value. Not only because (I hope) you generated a different key from mine, but also because Fernet connects the values to be encrypted with some randomly generated buffers. This is one of the "practices" I mentioned earlier: it will prevent opponents from distinguishing which encryption values are the same.
Decryption is equally simple:
> frn = fernet.Fernet (k) > frn.decrypt (encrypted) b'x marks the spot'
Note that this only encrypts and decrypts byte strings. In order to encrypt and decrypt text strings, you usually need to encode and decode them using UTF-8.
One of the most interesting developments in cryptography in the mid-20th century was public key (public key) encryption. It can keep the decryption key secret while issuing the encryption key. For example, it can be used to hold the API key used by the server: the server is the party that can access the decryption key, but anyone can save the public encryption key.
Although cryptography does not have any security features that support public key encryption, the PyNaCl library does. PyNaCl encapsulates and provides some good ways to use the NaCl encryption system invented by Daniel J. Bernstein.
NaCl always encrypts (encrypt) and signs (sign) or decrypts (decrypt) and verifies signatures (verify signature) at the same time. This is a method to prevent scalability (malleability-based)-based, where lawbreakers modify encryption values.
Encryption is done with a public key, while signing is done with a key:
> from nacl.public import PrivateKey, PublicKey, Box > source = PrivateKey.generate () > with open ("target.pubkey", "rb") as fpin:. Target_public_key = PublicKey (fpin.read ()) > enc_box = Box (source, target_public_key) > result = enc_box.encrypt (b "x marks the spot") > result [: 4] b'\ xe2\ x1c0\ xa4'
Decryption reverses the role: it requires a private key for decryption and a public key to verify the signature:
> > from nacl.public import PrivateKey, PublicKey, Box > with open ("source.pubkey", "rb") as fpin:. Source_public_key = PublicKey (fpin.read ()) > > with open ("target.private_key", "rb") as fpin: Target = PrivateKey (fpin.read ()) > dec_box = Box (target, source_public_key) > dec_box.decrypt (result) b'x marks the spot'
The PocketProtector library is built on top of PyNaCl and contains a complete key management scheme.
This is the end of the content of "how to encrypt files with Python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.