Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python encrypts and decrypts with AES&SHA1PRNG algorithm

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "Python encrypts and decrypts with AES&SHA1PRNG algorithm". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Encryption and decryption with python3-AES SHA1PRNG algorithm

Due to the needs of the work, we need to encrypt and decrypt the aes. When collecting data, we found the core writing code from the Internet, which met the demand after a small amount of modification.

The core encryption and decryption code comes from the original text: https://blog.csdn.net/max229max/article/details/87639613

#! / usr/bin/python#-*-coding: UTF-8-* -''python3-AES SHA1PRNG algorithm encrypts and decrypts notes The following dependent libraries need to be installed: pip3 install pycryptodomepip3 install Crypto'''from Crypto.Cipher import AESimport hashlibBS = AES.block_sizedef padding_pkcs5 (value): return str.encode (value + (BS-len (value)% BS) * chr (BS-len (value)% BS)) # convert decimal to hexadecimal def get_sha1prng_key (key): signature = hashlib.sha1 (key.encode ()). Digest () signature = hashlib.sha1 (signature) .digest () return''.join ([' x'% i for i in signature]) .upper () [: 32] # encryption def encrypt (key:str Value:str)-> str: cryptor = AES.new (bytes.fromhex (key), AES.MODE_ECB) padding_value = padding_pkcs5 (value) # padding content with pkcs5 ciphertext = cryptor.encrypt (padding_value) return''.join ([' x'% i for i in ciphertext]) .upper () # decrypt def decrypt (key:str Value:str)-> str:''AES/ECB/NoPadding decrypt' 'key = bytes.fromhex (key) cryptor = AES.new (key, AES.MODE_ECB) ciphertext = cryptor.decrypt (bytes.fromhex (value)) return padding_zero (str (ciphertext, "utf-8")) # string handling Filter the special characters def padding_zero (value): list = [] for c in value: # ascii code range to get if ord (c) > 31 & ord (c) < 127,127: list.append (c) return''.join (list) if _ name__ = =' _ main__': key = 'cce33fa1-9496-4796-8a32murf 84e65dcb056' content =' test_data_10086' Edb = encrypt (get_sha1prng_key (key)) Content) print (content +'='+ edb) ddb = decrypt (get_sha1prng_key (key), edb) print (edb +'='+ ddb) "Python encrypts and decrypts with AES&SHA1PRNG algorithm" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report