一、Python SHA1加密
1、Python SHA1加密演算法是什麼?
SHA1全稱為Secure Hash Algorithm 1,是一種Hash演算法,該演算法將數據轉換為散列值,並輸出一個長度為160位的散列值字元串,用於保證數據的完整性和安全性。在Python中,我們可以使用hashlib庫中的sha1()方法進行SHA1加密。
import hashlib def sha1_encrypt(content): sha1_obj = hashlib.sha1() sha1_obj.update(content.encode('utf-8')) return sha1_obj.hexdigest()
2、如何使用Python SHA1加密?
在使用Python SHA1加密時,我們需要首先引入hashlib模塊,然後使用sha1()方法進行加密。將待加密的字元串通過update()方法更新到sha1對象中,最後調用hexdigest()方法獲取加密後的字元串。
二、Python SHA1加密解密代碼
1、Python SHA1加密代碼示例:
import hashlib def sha1_encrypt(content): sha1_obj = hashlib.sha1() sha1_obj.update(content.encode('utf-8')) return sha1_obj.hexdigest() if __name__ == '__main__': content = 'Hello, world!' print(sha1_encrypt(content))
2、Python SHA1解密代碼示例:
由於SHA1演算法是單向加密演算法,不可逆,因此無法進行解密。
三、Python加密的庫
1、Python自帶的加密庫:
Python內置的hashlib庫提供了多個Hash演算法,包括MD5、SHA1、SHA224、SHA256、SHA384和SHA512等。除此之外,Python還提供了hmac模塊可用於加密的密鑰生成。
2、第三方Python加密庫:
在Python中,還有一些第三方庫可用於加密,例如pycrypto、pycryptodome、cryptography等。
四、Python加密U盤
1、Python加密U盤庫:
加密U盤是數據安全保護的重要手段,在Python中,我們可以使用pycryptodome庫進行U盤的加密。該庫提供了許多加密演算法,例如AES、DES、RSA等,可根據需求選擇合適的演算法進行加密。
2、Python加密U盤代碼示例:
from Crypto.Cipher import AES import os BLOCK_SIZE = 16 PADDING = '*' def pad(s): return s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING def encrypt(key, iv, in_filename, out_filename): chunksize = 64 * 1024 filesize = os.path.getsize(in_filename) with open(in_filename, 'rb') as infile: with open(out_filename, 'wb') as outfile: outfile.write('{0:0=16d}'.format(filesize).encode()) outfile.write(iv) aes_obj = AES.new(key, AES.MODE_CBC, iv) while True: chunk = infile.read(chunksize) if len(chunk) == 0: break elif len(chunk) % BLOCK_SIZE != 0: chunk = pad(chunk) outfile.write(aes_obj.encrypt(chunk)) def decrypt(key, iv, in_filename, out_filename): chunksize = 64 * 1024 with open(in_filename, 'rb') as infile: with open(out_filename, 'wb') as outfile: filesize = int(infile.read(16).decode()) infile.seek(BLOCK_SIZE) aes_obj = AES.new(key, AES.MODE_CBC, iv) while True: chunk = infile.read(chunksize) if len(chunk) == 0: break outfile.write(aes_obj.decrypt(chunk)) outfile.truncate(filesize) if __name__ == '__main__': key = '1234567890123456' iv = '1234567890123456' in_filename = 'test.txt' out_filename = 'test.en' encrypt(key.encode(), iv.encode(), in_filename, out_filename) decrypt(key.encode(), iv.encode(), out_filename, in_filename + '.out')
五、Python加密解密參數
1、Python加密解密常用參數:
(1) 待加密的字元串或文件;
(2) 加密演算法的類型,例如SHA1、MD5;
(3) 加密演算法的密鑰;
(4) 加密演算法的向量(IV)。
2、Python加密解密參數示例:
以AES加密演算法為例,其中KEY和IV為16位元組字元串,plaintext為待加密數據:
from Crypto.Cipher import AES KEY = '0123456789abcdef' IV = '0123456789abcdef' MODE = AES.MODE_CBC aes_obj = AES.new(KEY, MODE, IV) ciphertext = aes_obj.encrypt(plaintext)
六、Python加密解密演算法
1、Python常用的加密解密演算法:
(1) 對稱加密演算法,例如AES、DES、RC4等;
(2) 非對稱加密演算法,例如RSA、Elgamal、Diffie-Hellman等;
(3) Hash演算法,例如MD5、SHA1、SHA256、SHA512等。
2、Python加密解密演算法示例:
以AES對稱加密演算法為例,其中KEY和IV為16位元組字元串,plaintext為待加密數據:
from Crypto.Cipher import AES KEY = '0123456789abcdef' IV = '0123456789abcdef' MODE = AES.MODE_CBC aes_obj = AES.new(KEY, MODE, IV) ciphertext = aes_obj.encrypt(plaintext)
七、Python加密與解密
1、Python加密與解密的基本流程:
(1) 根據加密演算法生成加密對象;
(2) 使用加密對象對明文進行加密操作;
(3) 使用加密對象對密文進行解密操作。
2、Python加密與解密流程示例:
以AES對稱加密演算法為例,其中KEY和IV為16位元組字元串,plaintext為待加密數據:
from Crypto.Cipher import AES KEY = '0123456789abcdef' IV = '0123456789abcdef' MODE = AES.MODE_CBC aes_obj = AES.new(KEY, MODE, IV) ciphertext = aes_obj.encrypt(plaintext) decrypted_text = aes_obj.decrypt(ciphertext)
八、Python加密Excel
1、Python加密Excel庫:
在Python中,我們可以使用openpyxl庫進行Excel文件的加密,該庫支持Excel 2010及以上版本的.xlsx文件。
2、Python加密Excel代碼示例:
from openpyxl import load_workbook from openpyxl.workbook.protection import WorkbookProtection def encrypt_excel(filename, password): wb = load_workbook(filename) ws = wb.active ws.protection.password = password ws.protection.sheet = True wb.security = WorkbookProtection(workbookPassword=password, revisionsPassword=password) wb.save(filename) if __name__ == '__main__': filename = 'test.xlsx' password = '123456' encrypt_excel(filename, password)
總結
本文對Python SHA1加密進行了詳細的講解,包括Python SHA1加密演算法、Python SHA1加密解密代碼、Python加密的庫、Python加密U盤、Python加密解密參數、Python加密解密演算法、Python加密與解密、Python加密Excel等方面。通過學習本文,可以更好地理解Python中常用的加密技術,從而保證數據的安全性和完整性。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/236076.html