In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use sm4 algorithm in python". In daily operation, I believe many people have doubts about how to use sm4 algorithm in python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use sm4 algorithm in python"! Next, please follow the editor to study!
National Secret sm4
Sm4 algorithm is a grouping algorithm for WLAN products. The packet length of the algorithm is 128 bits and the key length is 128 bits. Both encryption algorithm and key expansion algorithm adopt 32 rounds of nonlinear iterative structure. The structure of the decryption algorithm is the same as that of the encryption algorithm, but the use order of the round key is opposite, and the decryption round key is the reverse order of the encryption round key.
GmSSL
GmSSL is an open source encryption package of python implementation, support SM2/SM3/SM4 and other national secret (national commercial password) algorithm, the project uses business-friendly BSD-like open source license, open source and can be used for closed-source commercial applications.
Python programs from gmssl.sm4 import CryptSM4, SM4_ENCRYPT, SM4_DECRYPTimport binasciifrom heapq import heappush, heappopfrom collections import OrderedDictclass SM4: "National Secret sm4 encryption" def _ _ init__ (self): self.crypt_sm4 = CryptSM4 () def str_to_hexStr (self) " Hex_str): "string to hex: param hex_str: string: return: hex" hex_data = hex_str.encode ('utf-8') str_bin = binascii.unhexlify (hex_data) return str_bin.decode (' utf-8') def encrypt (self, encrypt_key Value): "" National Secret sm4 encryption: param encrypt_key: sm4 encryption key: param value: string to be encrypted: return: Hexx value after sm4 encryption "" crypt_sm4 = self.crypt_sm4 crypt_sm4.set_key (encrypt_key.encode ()) SM4_ENCRYPT) encrypt_value = crypt_sm4.crypt_ecb (value.encode ()) # bytes type return encrypt_value.hex () def decrypt (self, decrypt_key Encrypt_value): "" National Secret sm4 decryption: param decrypt_key:sm4 encryption key: param encrypt_value: hexe value to be decrypted: return: original string "" crypt_sm4 = self.crypt_sm4 crypt_sm4.set_key (decrypt_key.encode ()) SM4_DECRYPT) decrypt_value = crypt_sm4.crypt_ecb (bytes.fromhex (encrypt_value)) # bytes type return self.str_to_hexStr (decrypt_value.hex ()) str_data = {"ffffffwsdwefewd": "fefefewfwrv", "qazqaz": "vfbfrbgtrnujy"} key = "3l5butlj26hvv313" SM4 = SM4 () print ("content to be encrypted:", str_data) encoding = SM4.encrypt (key, str_data) print ("results of sm4 encryption:" Encoding) print ("results of National Secret sm4 decryption:", SM4.decrypt (key, encoding)) python result
At this point, the study on "how to use the sm4 algorithm in python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.