日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

用 Python 加密日常文件

系統 1916 0

用 Python 加密日常文件_第1張圖片

圖片來源于網絡? 文/Python高效編程

生活中,有時候我們需要對一些重要的文件進行加密 ,Python 提供了諸如 hashlib,base64 等便于使用的加密庫。

            

但對于日常學習而言,我們可以借助異或操作,實現一個簡單的文件加密程序,從而強化自身的編程能力。 記得給公眾號加個星標,不會錯過精彩內容。

基礎知識

在 Python 中異或操作符為: ^ ,也可以記作 XOR。按位異或的意思是:相同值異或為 0,不同值異或為 1。具體來講,有四種可能:0 ^ 0 = 0,0 ^ 1 = 1, 1 ^ 0 ?= 1, 1 ^ 1 = 0。我們還可總結出規律(A 為 0 或 1):0 和 A 異或為 A本身;1 和 A 異或為 A 反。

讓我們想看看一位二進制數滿足的性質:

b ^ b = 0

a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c

(a ^ b) ^ b = a ^ (b ^ b) = a ^ 0 = a

易知,對任意長二進制數都滿足上述性質。

原理

通過了解異或操作的性質,加密原理就非常清晰了。

加密操作:

首先將文件轉換成二進制數,再生成與該二進制數等長的隨機密鑰,將二進制數與密鑰進行異或操作,得到加密后的二進制數。

解密操作:

將加密后的二進制程序與密鑰進行異或操作,就得到原二進制數,最后將原二進制數恢復成文本文件。

生成隨機密鑰:

secrets 庫是 Python 3.6 引入的偽隨機數模塊,適合生成隨機密鑰。token_bytes 函數接受一個 int 參數,用于指定隨機字節串的長度。int.from_bytes 把字節串轉換為 int,也就是我們需要的二進制數。

              
                from?secrets?import?token_bytesdef?random_key(length):????key =?token_bytes(nbytes=length)????key_int =?int.from_bytes(key,?'big')????return?key_int
                
                  import
                
                ?token_bytes
                

def ? random_key (length) :
????key =?token_bytes(nbytes=length)
????key_int =?int.from_bytes(key,? 'big' )
???? return ?key_int
加密單元:

encrypt 函數接受一個 str 對象,返回元組 (int, int)。通過 encode 方法,我們將字符串編碼成字節串。int.from_bytes 函數將字節串轉換為 int 對象。最后對二進制對象和隨機密鑰進行異或操作,就得到了加密文本。

              
                def?encrypt(raw):????raw_bytes?=?raw.encode()????raw_int?=?int.from_bytes(raw_bytes,?'big')????key_int =?random_key(len(raw_bytes))????return?raw_int?^?key_int,?key_int
                
????raw_bytes?=?raw.encode()
????raw_int?=?int.from_bytes(raw_bytes,? 'big' )
????key_int =?random_key(len(raw_bytes))
???? return ?raw_int?^?key_int,?key_int
解密單元:

decrypt 接受兩個 int 對象,分別為加密文本和隨機密鑰。首先對兩者進行異或操作,計算解密出來的 int 對象所占比特數。decrypted.bit_length 函數得到的是二進制數的位數,除以 8 可以得到所占比特大小。為了防止,1 ~ 7 位的二進制數整除 8 得到 0,所以要加上 7,然后再進行整除 8 的操作。使用 int.to_bytes 函數將解密之后的 int 的對象轉換成 bytes 對象。最后通過 decode 方法,將字節串轉換成字符串。

              
                def?decrypt(encrypted,?key_int):????decrypted?=?encrypted?^?key_int????length?=?(decrypted.bit_length()?+?7)?//?8????decrypted_bytes?=?int.to_bytes(decrypted,?length,?'big')?????return?decrypted_bytes.decode()
                
????decrypted?=?encrypted?^?key_int
????length?=?(decrypted.bit_length()?+? 7 )?//? 8
????decrypted_bytes?=?int.to_bytes(decrypted,?length,? 'big' )?
???? return ?decrypted_bytes.decode()

利用上述函數,我們可以很輕松對文本文件進行加密、解密操作。

              
                >>>?raw?=?'畫圖省識春風面,環珮空歸夜月魂'>>>?encrypted?=?encrypt(raw)>>>?encrypted(217447100157746604585...,?9697901906831571319...)>>>?decrypt(*encrypted)'畫圖省識春風面,環珮空歸夜月魂'
                
>>>?encrypted?=?encrypt(raw)
>>>?encrypted
( 217447100157746604585. ..,
? 9697901906831571319. ..)
>>>?decrypt(*encrypted)
'畫圖省識春風面,環珮空歸夜月魂'

加密文本文件

path 為待加密文件的地址,如果不指定密鑰地址,則在該目錄下新建目錄和文件。

              
                import?jsonfrom?pathlib?import?Pathdef?encrypt_file(path,?key_path=None,?*,?encoding='utf-8'):????path?=?Path(path)????cwd?=?path.cwd()?/?path.name.split('.')[0]????path_encrypted?=?cwd?/?path.name?????if?key_path?is?None:????????key_path?=?cwd?/?'key'????if?not?cwd.exists():????????cwd.mkdir()????????path_encrypted.touch()????????key_path.touch()????with?path.open('rt',?encoding=encoding)?as?f1,?????????path_encrypted.open('wt',?encoding=encoding)?as?f2,?????????????key_path.open('wt',?encoding=encoding)?as?f3:????????encrypted,?key?=?encrypt(f1.read())????????json.dump(encrypted,?f2)????????json.dump(key,?f3)
                
from ?pathlib? import ?Path

def ? encrypt_file (path,?key_path=None,?*,?encoding= 'utf-8' ) :
????path?=?Path(path)
????cwd?=?path.cwd()?/?path.name.split( '.' )[ 0 ]
????path_encrypted?=?cwd?/?path.name?
???? if ?key_path? is ? None :
????????key_path?=?cwd?/? 'key'
???? if ? not ?cwd.exists():
????????cwd.mkdir()
????????path_encrypted.touch()
????????key_path.touch()

???? with ?path.open( 'rt' ,?encoding=encoding)? as ?f1,?
????????path_encrypted.open( 'wt' ,?encoding=encoding)? as ?f2,?
????????????key_path.open( 'wt' ,?encoding=encoding)? as ?f3:
????????encrypted,?key?=?encrypt(f1.read())
????????json.dump(encrypted,?f2)
????????json.dump(key,?f3)

解密文件

              
                def?decrypt_file(path_encrypted,?key_path=None,?*,?encoding='utf-8'):????path_encrypted?=?Path(path_encrypted)????cwd?=?path_encrypted.cwd()????path_decrypted?=?cwd?/?'decrypted'?????if?not?path_decrypted.exists():????????path_decrypted.mkdir()????????path_decrypted?/=?path_encrypted.name????????path_decrypted.touch()????if?key_path?is?None:????????key_path?=?cwd?/?'key'????with?path_encrypted.open('rt',?encoding=encoding)?as?f1,?????????key_path.open('rt',?encoding=encoding)?as?f2,?????????path_decrypted.open('wt',?encoding=encoding)?as?f3:????????decrypted?=?decrypt(json.load(f1),?json.load(f2))????????f3.write(decrypted)
                
????path_encrypted?=?Path(path_encrypted)
????cwd?=?path_encrypted.cwd()
????path_decrypted?=?cwd?/? 'decrypted' ?
???? if ? not ?path_decrypted.exists():
????????path_decrypted.mkdir()
????????path_decrypted?/=?path_encrypted.name
????????path_decrypted.touch()
???? if ?key_path? is ? None :
????????key_path?=?cwd?/? 'key'
???? with ?path_encrypted.open( 'rt' ,?encoding=encoding)? as ?f1,?
????????key_path.open( 'rt' ,?encoding=encoding)? as ?f2,?
????????path_decrypted.open( 'wt' ,?encoding=encoding)? as ?f3:
????????decrypted?=?decrypt(json.load(f1),?json.load(f2))
????????f3.write(decrypted)

執行完加密、解密文件操作,得到的解密文件與原文件相同,示意圖如下:

用 Python 加密日常文件_第2張圖片

熱 門 推 薦
用Python創建微信機器人

用Python機器人監聽微信群聊

用Python獲取攝像頭并實時控制人臉

開源項目 | 用Python美化LeetCode倉庫

推薦Python中文社區旗下的幾個服務類公眾號
征稿啟事 | Python中文社區有獎征文

用 Python 加密日常文件_第3張圖片

▼ 點擊成為 社區注冊會員? ? ? ? ?? 「在看」 一下,一起PY!


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 射洪县| 平遥县| 凌源市| 轮台县| 吉安县| 临沧市| 嘉峪关市| 瑞昌市| 即墨市| 松江区| 枣庄市| 绍兴县| 安徽省| 嘉定区| 游戏| 理塘县| 烟台市| 郁南县| 始兴县| 年辖:市辖区| 屏东市| 通化县| 五莲县| 吴桥县| 神木县| 山东| 湖南省| 监利县| 绥江县| 伽师县| 嵊州市| 南投市| 崇阳县| 晋州市| 岳阳市| 攀枝花市| 沂南县| 利川市| 榆树市| 三台县| 昭通市|