偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

十行 Python 代碼,使用 OTP 實(shí)現(xiàn)對文件的加密解密

安全 數(shù)據(jù)安全
OTP 就是 One-time password,翻譯過來就是一次性密碼。它的原理非常簡單,加密的過程就是明文和密鑰(key)進(jìn)行異或,得到密文,而解密的過程就是密文和密鑰(key)異或,得到明文。

不知道你是否相信,只需 10 行代碼,就可以使用 Python 100% 安全地加密文件。這背后的原理就是 OTP。

原理

OTP 就是 One-time password,翻譯過來就是一次性密碼。它的原理非常簡單,加密的過程就是明文和密鑰(key)進(jìn)行異或,得到密文,而解密的過程就是密文和密鑰(key)異或,得到明文。舉例如下:

加密:

圖片

解密:

圖片

理論上,基于以下假設(shè),這個(gè)加密被認(rèn)為是牢不可破的:

  • 密鑰是真正隨機(jī)的
  • 密鑰長度與信息長度相同
  • 密鑰永遠(yuǎn)不會全部或部分重復(fù)使用
  • 密鑰 key 很安全,不會公開

應(yīng)用:加密文件

如果自己有一個(gè)私密的文件,那么完全可以使用 OTP 來加密,密鑰保存在自己手里,很安全。話不多說,直接上代碼:

加密文件:

import os
def encryption(file):
toBeEncryptedFile = open(file, 'rb').read()
size = len(toBeEncryptedFile)
otpKey = os.urandom(size)
with open(file.split('.')[0] + '.key', 'wb') as key:
key.write(otpKey)
encryptedFile = bytes (a ^ b for (a, b) in zip(toBeEncryptedFile, otpKey))
with open(file, 'wb') as encrypted:
encrypted.write(encryptedFile)

這段代碼一共 10 行,密鑰 optKey 隨機(jī)生成并保存在文件中,然后用這個(gè)密鑰加密文件,當(dāng)需要加密文件時(shí),這樣調(diào)用 encryption 函數(shù):

if __name__ == "__main__":
encryption("/Users/aaron/Downloads/1/銀行卡.JPG")

圖片

成功執(zhí)行代碼后,我們無法再預(yù)覽或打開我們的圖像,因?yàn)樗F(xiàn)在是加密的。此外,我們的文件夾中有一個(gè)新的密鑰文件“銀行卡.key”。

圖片

現(xiàn)在,我們來解密它。

解密文件只需要 6 行代碼:

def decryption(file, otpKey):
encryptedFile = open(file, 'rb').read()
otpKey = open(otpKey, 'rb').read()
decryptedFile = bytes (a ^ b for (a, b) in zip(encryptedFile, otpKey))
with open(file, 'wb') as decrypted:
decrypted.write(decryptedFile)

這樣調(diào)用:

if __name__ == "__main__":
# encryption("/Users/aaron/Downloads/1/銀行卡.JPG")
decryption("/Users/aaron/Downloads/1/銀行卡.JPG", "/Users/aaron/Downloads/1/銀行卡.key")

這樣就完成了解密:

圖片

完整代碼

import os


def encryption(file):
toBeEncryptedFile = open(file, "rb").read()
size = len(toBeEncryptedFile)
otpKey = os.urandom(size)
with open(file.split(".")[0] + ".key", "wb") as key:
key.write(otpKey)
encryptedFile = bytes(a ^ b for (a, b) in zip(toBeEncryptedFile, otpKey))
with open(file, "wb") as encrypted:
encrypted.write(encryptedFile)


def decryption(file, otpKey):
encryptedFile = open(file, "rb").read()
otpKey = open(otpKey, "rb").read()
decryptedFile = bytes(a ^ b for (a, b) in zip(encryptedFile, otpKey))
with open(file, "wb") as decrypted:
decrypted.write(decryptedFile)


if __name__ == "__main__":
# encryption("/Users/aaron/Downloads/1/銀行卡.JPG")
decryption("/Users/aaron/Downloads/1/銀行卡.JPG", "/Users/aaron/Download


責(zé)任編輯:武曉燕 來源: Python七號
相關(guān)推薦

2022-03-14 09:57:30

Python代碼

2022-07-07 15:50:19

Python開發(fā)功能

2022-03-23 15:32:38

Python開發(fā)代碼

2025-06-18 10:05:26

2020-11-08 14:44:37

VSCode代碼編碼

2022-01-25 12:51:58

Python代碼證件照

2016-03-29 10:08:07

2023-12-06 18:09:01

2022-03-26 22:28:06

加密通信Python

2024-06-12 15:59:59

前端JavaScrip識別

2011-09-07 14:43:24

2021-05-08 05:56:15

加密OpenSSL密鑰

2022-07-21 10:08:59

代碼K線圖

2022-05-02 18:29:35

bashshellLinux

2016-04-05 11:40:17

殺毒51CTOIT技術(shù)周刊

2025-05-26 09:15:00

PythonWordcloud詞云

2023-06-25 13:31:44

2024-06-21 14:47:52

2021-02-01 08:00:00

vimLinux加密

2016-05-30 17:51:34

網(wǎng)絡(luò)安全技術(shù)周刊
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號