密码学 - 文件解密



文件解密是一种解密方法,它将文件转换回明文或可读数据。使用此方法可确保授权人员可以访问您的数据,他们可以使用解密密钥读取内容。

在本章中,我们将看到不同的解密技术来解密文件数据。让我们深入探讨。

文件解密的基本方法

解密是将加密数据更改回其原始可读形式的过程。以下是文件解密的一些基本方法:

对称密钥解密

单个密钥用于加密和解密。要解密文件,请使用与加密文件时相同的密钥。解密过程通常涉及使用解密密钥将加密算法的逆运算添加到加密数据中。

from cryptography.fernet import Fernet

# Generate a key
key = Fernet.generate_key()
cipher_suite = Fernet(key)

# Read the file
with open('plain_text.txt', 'rb') as f:
   plaintext = f.read()

# Encrypt the file
encrypted_text = cipher_suite.encrypt(plaintext)

# Write the encrypted file
with open('encrypted_file.txt', 'wb') as f:
   f.write(encrypted_text)
    
# Print message after file is encrypted
print("File encrypted successfully.")


# Decrypt the file
decrypted_text = cipher_suite.decrypt(encrypted_text)

# Write the decrypted file
with open('decrypted_file.txt', 'wb') as f:
   f.write(decrypted_text)
    
# Print message after file is decrypted
print("File decrypted successfully.")

输出

File encrypted successfully.
File decrypted successfully.

请参见下面的输出图像,其中显示了plain_text.txt、encrypted_file.txt和decrypted_file.txt文件。

Python File Decryption

非对称密钥解密

非对称密钥加密需要两个密钥:公钥和私钥。公钥用于加密,私钥用于解密。要解密非对称加密的文件,您需要私钥。非对称加密通常用于安全通信和密钥交换。

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes

# Generate key pair
private_key = rsa.generate_private_key(
   public_exponent=65537,
   key_size=2048
)
public_key = private_key.public_key()

# Save private key
with open("private.pem", "wb") as f:
   f.write(
      private_key.private_bytes(
         encoding=serialization.Encoding.PEM,
         format=serialization.PrivateFormat.TraditionalOpenSSL,
         encryption_algorithm=serialization.NoEncryption()
      )
   )

# Save public key
with open("public.pem", "wb") as f:
   f.write(
      public_key.public_bytes(
         encoding=serialization.Encoding.PEM,
         format=serialization.PublicFormat.SubjectPublicKeyInfo
      )
   )

def encrypt_file(file_path, public_key_path, output_path):
   # Load public key
   with open(public_key_path, "rb") as f:
      public_key = serialization.load_pem_public_key(f.read())

   # Encrypt file
   with open(file_path, "rb") as f:
      plaintext = f.read()
   ciphertext = public_key.encrypt(
      plaintext,
      padding.OAEP(
         mgf=padding.MGF1(algorithm=hashes.SHA256()),
         algorithm=hashes.SHA256(),
         label=None
      )
   )

   # Save encrypted file
   with open(output_path, "wb") as f:
      f.write(ciphertext)

def decrypt_file(file_path, private_key_path, output_path):
   # Load private key
   with open(private_key_path, "rb") as f:
      private_key = serialization.load_pem_private_key(
         f.read(),
         password=None
      )

   # Decrypt file
   with open(file_path, "rb") as f:
      ciphertext = f.read()
   plaintext = private_key.decrypt(
      ciphertext,
      padding.OAEP(
         mgf=padding.MGF1(algorithm=hashes.SHA256()),
         algorithm=hashes.SHA256(),
         label=None
      )
   )

   # Save decrypted file
   with open(output_path, "wb") as f:
      f.write(plaintext)

# Encrypt file
encrypt_file("plain_text.txt", "public.pem", "encrypted_file.bin")

# Decrypt file
decrypt_file("encrypted_file.bin", "private.pem", "decrypted_plaintext.txt")

输出

使用“python program.py”运行上述代码后,它将创建公钥和私钥,然后使用公钥加密名为plain_text.txt的文件,然后使用私钥解密加密的文件。

Asymmetric File Decryption

基于密码的解密

密码或密码短语会创建一个用于加密和解密的密钥。相同的密码用于加密和解密数据。要解密受基于密码的加密保护的文件,您必须输入正确的密码。

from cryptography.fernet import Fernet

# get password from user
password = input("Enter password: ").encode()

# derive key from password
key = Fernet.generate_key()

# create Fernet cipher suite with the derived key
cipher_suite = Fernet(key)

# read the encrypted file
with open('encrypted_file.txt', 'rb') as f:
   encrypted_text = f.read()

# decrypt the file
try:
   decrypted_text = cipher_suite.decrypt(encrypted_text)
   # write the decrypted file
   with open('decrypted_file.txt', 'wb') as f:
      f.write(decrypted_text)
   print("File decrypted successfully.")
except Exception as e:
   print("Error decrypting file:", str(e))

输入/输出

Enter password: 12345
Error decrypting file:    

密钥派生

某些加密方法使用密钥派生函数 (KDF) 从密码或密码短语生成密钥。然后使用生成的密钥进行加密和解密。密钥派生确保使用相同的密码创建相同的密钥,从而允许安全解密。

初始化向量 (IV) 的使用

初始化向量 (IV) 用于加密算法中,以防止密文与明文相同。解密文件时,通常需要同时提供 IV 和解密密钥。IV 通常包含在加密文件中或与其一起发送。

广告