您好,登錄后才能下訂單哦!
小編給大家分享一下Python3對(duì)稱加密算法AES、DES3有什么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體如下:
python3.6此庫安裝方式,需要pip3 install pycryptodome。
如有site-packages中存在crypto、pycrypto,在pip之前,需要pip3 uninstall crypto
、pip3 uninstall pycrypto
,否則無法安裝成功。
C:\WINDOWS\system32>pip3 install pycryptodome
Collecting pycryptodome
Downloading https://files.pythonhosted.org/packages/0f/5d/a429a53eacae3e13143248c3868c76985bcd0d75858bd4c25b574e51bd4d/pycryptodome-3.6.3-cp36-cp36m-win_amd64.whl (7.9MB)
100% |████████████████████████████████| 7.9MB 111kB/s
Installing collected packages: pycryptodome
Successfully installed pycryptodome-3.6.3
這里順帶說一下pycrypto,這個(gè)庫已經(jīng)有很久沒有人維護(hù)了,如果需要安裝此庫,需要先安裝 VC++ build tools
然后將 ~\BuildTools\VC\Tools\MSVC\14.15.26726\include 目錄下的 stdint.h 拷貝到 C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt 下。(Win10 需管理員權(quán)限)
接著將同目錄下的 inttypes.h 中的 #include <stdint.h>
(第十四行),改成 #include "stdint.h"
。
然后使用 pip3 install pycrypto
,就能直接安裝了。
注:如果不是業(yè)務(wù)需要,請(qǐng)盡可能使用 pycryptodome。
AES:
import Crypto.Cipher.AES import Crypto.Random import base64 import binascii def auto_fill(x): if len(x) <= 32: while len(x) not in [16, 24, 32]: x += " " return x.encode() else: raise "密鑰長度不能大于32位!" key = "asd" content = "abcdefg1234567" x = Crypto.Cipher.AES.new(auto_fill(key), Crypto.Cipher.AES.MODE_ECB) a = base64.encodebytes(x.encrypt(auto_fill(content))) b = x.decrypt(base64.decodebytes(a)) print(a) print(b) a = binascii.b2a_base64(x.encrypt(auto_fill(content))) b = x.decrypt(binascii.a2b_base64(a)) print(a) print(b) key = "dsa" iv = Crypto.Random.new().read(16) # 向量,必須為16字節(jié) content = "1234567abcdefg" y = Crypto.Cipher.AES.new(auto_fill(key), Crypto.Cipher.AES.MODE_CBC, iv) c = binascii.b2a_base64(y.encrypt(auto_fill(content))) z = Crypto.Cipher.AES.new(auto_fill(key), Crypto.Cipher.AES.MODE_CBC, iv) d = z.decrypt(binascii.a2b_base64(c)) print(c) print(d)
運(yùn)行結(jié)果:
b'jr/EIUp32kLHc3ypZZ1cyg==\n'
b'abcdefg1234567 '
b'jr/EIUp32kLHc3ypZZ1cyg==\n'
b'abcdefg1234567 '
b'j+Ul9KQd0HnuiHW3z9tD7A==\n'
b'1234567abcdefg '
DES3:
import Crypto.Cipher.DES3 import base64 import binascii def auto_fill(x): if len(x) > 24: raise "密鑰長度不能大于等于24位!" else: while len(x) < 16: x += " " return x.encode() key = "asd" content = "abcdefg1234567" x = Crypto.Cipher.DES3.new(auto_fill(key), Crypto.Cipher.DES3.MODE_ECB) a = base64.encodebytes(x.encrypt(auto_fill(content))) print(a) b = x.decrypt(base64.decodebytes(a)) print(b) a = binascii.b2a_base64(x.encrypt(auto_fill(content))) b = x.decrypt(binascii.a2b_base64(a)) print(a) print(b)
運(yùn)行結(jié)果:
b'/ee3NFKeNqEk/qMNd1mjog==\n'
b'abcdefg1234567 '
b'/ee3NFKeNqEk/qMNd1mjog==\n'
b'abcdefg1234567 '
附:AES的工廠模式封裝Cipher_AES.py(封裝Crypto.Cipher.AES)
''' Created on 2018年7月7日 @author: ray ''' import Crypto.Cipher.AES import Crypto.Random import base64 import binascii class Cipher_AES: pad_default = lambda x, y: x + (y - len(x) % y) * " ".encode("utf-8") unpad_default = lambda x: x.rstrip() pad_user_defined = lambda x, y, z: x + (y - len(x) % y) * z.encode("utf-8") unpad_user_defined = lambda x, z: x.rstrip(z) pad_pkcs5 = lambda x, y: x + (y - len(x) % y) * chr(y - len(x) % y).encode("utf-8") unpad_pkcs5 = lambda x: x[:-ord(x[-1])] def __init__(self, key="abcdefgh22345678", iv=Crypto.Random.new().read(Crypto.Cipher.AES.block_size)): self.__key = key self.__iv = iv def set_key(self, key): self.__key = key def get_key(self): return self.__key def set_iv(self, iv): self.__iv = iv def get_iv(self): return self.__iv def Cipher_MODE_ECB(self): self.__x = Crypto.Cipher.AES.new(self.__key.encode("utf-8"), Crypto.Cipher.AES.MODE_ECB) def Cipher_MODE_CBC(self): self.__x = Crypto.Cipher.AES.new(self.__key.encode("utf-8"), Crypto.Cipher.AES.MODE_CBC, self.__iv.encode("utf-8")) def encrypt(self, text, cipher_method, pad_method="", code_method=""): if cipher_method.upper() == "MODE_ECB": self.Cipher_MODE_ECB() elif cipher_method.upper() == "MODE_CBC": self.Cipher_MODE_CBC() cipher_text = b"".join([self.__x.encrypt(i) for i in self.text_verify(text.encode("utf-8"), pad_method)]) if code_method.lower() == "base64": return base64.encodebytes(cipher_text).decode("utf-8").rstrip() elif code_method.lower() == "hex": return binascii.b2a_hex(cipher_text).decode("utf-8").rstrip() else: return cipher_text.decode("utf-8").rstrip() def decrypt(self, cipher_text, cipher_method, pad_method="", code_method=""): if cipher_method.upper() == "MODE_ECB": self.Cipher_MODE_ECB() elif cipher_method.upper() == "MODE_CBC": self.Cipher_MODE_CBC() if code_method.lower() == "base64": cipher_text = base64.decodebytes(cipher_text.encode("utf-8")) elif code_method.lower() == "hex": cipher_text = binascii.a2b_hex(cipher_text.encode("utf-8")) else: cipher_text = cipher_text.encode("utf-8") return self.unpad_method(self.__x.decrypt(cipher_text).decode("utf-8"), pad_method) def text_verify(self, text, method): while len(text) > len(self.__key): text_slice = text[:len(self.__key)] text = text[len(self.__key):] yield text_slice else: if len(text) == len(self.__key): yield text else: yield self.pad_method(text, method) def pad_method(self, text, method): if method == "": return Cipher_AES.pad_default(text, len(self.__key)) elif method == "PKCS5Padding": return Cipher_AES.pad_pkcs5(text, len(self.__key)) else: return Cipher_AES.pad_user_defined(text, len(self.__key), method) def unpad_method(self, text, method): if method == "": return Cipher_AES.unpad_default(text) elif method == "PKCS5Padding": return Cipher_AES.unpad_pkcs5(text) else: return Cipher_AES.unpad_user_defined(text, method)
使用方法:
加密:Cipher_AES(key [, iv]).encrypt(text, cipher_method [, pad_method [, code_method]])
解密:Cipher_AES(key [, iv]).decrypt(cipher_text, cipher_method [, pad_method [, code_method]])
key:密鑰(長度必須為16、24、32)
iv:向量(長度與密鑰一致,ECB模式不需要)
text:明文(需要加密的內(nèi)容)
cipher_text:密文(需要解密的內(nèi)容)
cipher_method:加密方法,目前只有"MODE_ECB"、"MODE_CBC"兩種
pad_method:填充方式,解決 Java 問題選用"PKCS5Padding"
code_method:編碼方式,目前只有"base64"、"hex"兩種
來段調(diào)用封裝類 Cipher_AES 的 demo_Cipher_AES.py,方便大家理解:
import Cipher_AES key = "qwedsazxc123321a" iv = key[::-1] text = "我愛小姐姐,可小姐姐不愛我 - -" cipher_method = "MODE_CBC" pad_method = "PKCS5Padding" code_method = "base64" cipher_text = Cipher_AES(key, iv).encrypt(text, cipher_method, pad_method, code_method) print(cipher_text) text = Cipher_AES(key, iv).decrypt(cipher_text, cipher_method, pad_method, code_method) print(text) ''' 運(yùn)行結(jié)果: uxhf+MoSko4xa+jGOyzJvYH9n5NvrCwEHbwm/A977CmGqzg+fYE0GeL5/M5v9O1o 我愛小姐姐,可小姐姐不愛我 - - '''
以上是“Python3對(duì)稱加密算法AES、DES3有什么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。