1,python common methods for string encryption:
[Python]View Plaincopy
- <pre code_snippet_id="340592" snippet_file_name="blog_20140512_1_2282504" name="code" class= "Python" >1. The simplest way is to use base64:
- Import Base64
- S1 = base64.encodestring (' Hello World ')
- S2 = base64.decodestring (S1)
- Print S1,s2
- # agvsbg8gd29ybgq=n
- # Hello World
- Note: This is the simplest method, but not enough insurance, because if someone else gets your ciphertext, you can decrypt it yourself to get the plaintext, but you can handle the ciphertext string, such as converting letters to numbers or special characters, It's a lot safer to replace the base64.decodestring when you're decrypting it.
- 2. The second method is to use the Win32com.client
- Import Win32com.client
- def encrypt (key,content): # Key: Key, Content: Clear text
- EncryptedData = Win32com.client.Dispatch (' CAPICOM. EncryptedData ')
- EncryptedData.Algorithm.KeyLength = 5
- EncryptedData.Algorithm.Name = 2
- Encrypteddata.setsecret (Key)
- Encrypteddata.content = Content
- return Encrypteddata.encrypt ()
- def decrypt (key,content): # Key: Key, content: Ciphertext
- EncryptedData = Win32com.client.Dispatch (' CAPICOM. EncryptedData ')
- EncryptedData.Algorithm.KeyLength = 5
- EncryptedData.Algorithm.Name = 2
- Encrypteddata.setsecret (Key)
- Encrypteddata.decrypt (content)
- str = encrypteddata.content
- return str
- S1 = Encrypt (' lovebread ', ' Hello World ')
- S2 = Decrypt (' Lovebread ', S1)
- Print S1,s2
- # MGEGCSSGAQQBGJDYA6BUMFIGCISGAQQBGJDYAWGGRDBCAGMCAAECAMYBAGFABAGQ
- # GPLLWJ9CSWQQH/FNBUZ6IJWKDTH9DLZMBGQYMFAZ3VFYS/LQ391ODTJLCRFGNXPX
- # lg7o
- # Hello World
- Note: This method is also very convenient, and can set their own key, more secure than the first method, if the security level is not very high, this method is the preferred encryption and decryption strategy!
- 3. There are also write encryption and decryption algorithms, such as:
- def encrypt (key, s):
- b = ByteArray (str (s). Encode ("GBK"))
- n = len (b) # to find the number of bytes in B
- c = ByteArray (n2)
- j = 0
- for I in range (0, N):
- B1 = B[i]
- B2 = B1 ^ Key # B1 = b2^ key
- C1 = b2%
- C2 = b2// # b2 = c2*16 + C1
- C1 = C1 +
- C2 = C2 + # C1,C2 is the number between 0~15, plus 65 becomes the encoding of the a-p character
- C[J] = C1
- c[j+1] = C2
- j = j+2
- return C.decode ("GBK")
- Def decrypt (key, s):
- c = ByteArray (str (s). Encode ("GBK"))
- n = Len (c) # Calculates the number of bytes in B
- If n% 2! = 0:
- return ""
- n = n// 2
- b = ByteArray (n)
- j = 0
- for I in range (0, N):
- C1 = C[j]
- C2 = c[j+1]
- j = j+2
- C1 = C1-
- C2 = C2-
- B2 = c2*+ C1
- B1 = b2^ Key
- B[i]= B1
- Try:
- return B.decode ("GBK")
- except:
- return "failed"
- Key =
- S1 = Encrypt (key, ' Hello World ')
- S2 = Decrypt (key, S1)
- Print S1,' n ', S2
- # HGKGDGDGAGPCIHAGNHDGLG
- # Hello World
- Note: This is an online copy of a simple example, you can customize their own algorithm for encryption and decryption, there are many complex encryption algorithms, you can self-access cryptography related algorithms.
- 4. For Python, it is also possible to compile the Python source file into a PYC binary format file, so that others can not see your source code, is also considered a method of encryption, the method is as follows:
- Execute command python-m py_compile create_slave.py can generate a CREATE_SLAVE.PYC file directly, then you can use CREATE_SLAVE.PYC to replace create_slave.py as a script to execute.
[Python]python Study Notes (vii)--encryption