1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| import base64 print(base64.b64encode('lx'.encode())) print(base64.b64decode('bHg='.encode()))
import hashlib m=hashlib.md5() m.update(str.encode("utf8")) m.hexdigest()
import hmac,hashlib key='key'.encode() text='lx'.encode() mac=hmac.new(key,text,hashlib.sha256) mac.digest() mac.hexdigest()
import binascii from pyDes import des,CBC,PAD_PKCS5 def des_encrypt(secret_key,s): iv=secret_key k=des(secret_key,CBC,iv,pad=None) en=k.encrypt(s,padmode=PAD_PCKS5) return binascii.b2a_hex(en) def des_decrypt(secret_key,s): iv=secret_key k=des(secret_key,CBC,iv,pad=None,padmode=PAD_PCKS5) de=k.decrypt(binascii.a2b_hex(s),padmode=PAD_PKCS5) return de secret_str=des_encrypt('999','lx-message') clear_str=des_decrypt('999',secret_str)
import base64 from Crypto.Cipher import AES def add_to_16(value): while len(value)%16!=0: value+='\0' return str.encode(value) def encrypt(key,text): aes=AES.new(add_to_16(key),AES.MODE_ECB) encrypt_aes=aes.encrypt(add_to_16(text)) encrypted_text=str(base64.encodebytes(encrypt_aes),encoding='utf-8') return encrypted_text def decrypt(key,text): aes=AES.new(add_to_16(key),AES.MODE_ECB) base64_decrypted=base64.decodebytes(text.encode(encoding='utf-8')) decrypted_text=str(aes.decrypt(base64_decrypted),encoding='utf-8').replace('\0','') return decrypted_text
import base64,rsa from rsa import common class RsaUtil(object): PUBLIC_KEY_PATH='public_key.pem' PRIVATE_KEY_PATH='private_key.pem' def _init_(self,company_pub_file=PUBLIC_KEY_PATH,company_pri_file=PRIVATE_KEY_PATH): if company_pub_file: self.company_public_key=rsa.PublicKey.load_pkcs1_openssl_pem(open(company_pub_file).read()) if company_pri_file: self.company_private_key=rsa.PrivateKey.load_pkcs1(open(company_pri_file).read()) def get_max_length(self,rsa_key,encrypt=True): blocksize=common.byte_size(rsa_key.n) reserve_size=11 if not encrypt: reserve_size=0 maxlength=blocksize-reserve_size return maxlength def encrypt_by_pblic_key(self,message): encrypt_result=b'' max_length=self.get_max_length(self.company_public_key) while message: input=message[:max_length] message=message[max_length:] out=rsa.encrypt(input,self.company_public_key) encrypt_result+=out encrypt_result=base64.b64decode(encrypt_result) return encrypt_result def decrypt_by_private_key(self,message): decrypt_result=b"" max_length=self.get_max_length(self.company_private_key,False) decrypt_message=base64.b64decode(message) while decrypt_message: input=decrypt_message[:max_length] decrypt_message=decrypt_message[max_length:] out=rsa.decrypt(input.self.company_private_key) decrypt_result+=out return decrypt_result def sign_by_private_key(self,data): signature=rsa.sign(str(data),priv_key=self.company_private_key,hash='SHA-1') return base64.b64encode(signature) def verify_by_public_key(self,message,signature): signature=base64.b64decode(signature) return rsa.verify(message,signature,self.company_public_key)
|