Encryption and decryption are usually performed on strings in programming, especially for private strings, such as passwords. You need to encrypt them. You can summarize three types of encryption: base64 and win32com. the client and itself write encryption and decryption algorithms, of course, the most secure is to write their own encryption and decryption algorithms.
- 1. The simplest method 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 it is not safe enough, because if someone else obtains your ciphertext, they can decrypt it to obtain the plaintext. However, they can process the ciphertext string, for example, if a letter is converted to a number or a special character, it is much safer to decrypt it by replacing it with base64.decodestring.
-
-
-
-
- 2. The second method is to use win32com. client
-
- Import win32com. client
- Def encrypt (key, content): # key: key, content: plaintext
- 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 you can set your own key, which is more secure than the first method. If you do not have high security requirements, this method is the first choice for encryption and decryption!
-
-
- 3. You can also write your own encryption and decryption algorithms, such:
-
- Def encrypt (key, s ):
- B = bytearray (str (s). encode ("gbk "))
- N = len (B) # calculate the number of bytes of B
- C = bytearray (n * 2)
- J = 0
- For I in range (0, n ):
- B1 = B [I]
- B2 = b1 ^ key # b1 = b2 ^ key
- C1 = b2 % 16
- C2 = b2 // 16 # b2 = c2 * 16 + c1
- C1 = c1 + 65
- C2 = c2 + 65 # c1, c2 are all 0 ~ The number between 15, plus 65 becomes the character encoding of the A-P
- 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) # calculate the number of bytes of 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-65
- C2 = c2-65
- B2 = c2 * 16 + c1
- B1 = b2 ^ key
- B [I] = b1
- Try:
- Return B. decode ("gbk ")
- Except t:
- Return "failed"
-
- Key = 15
- S1 = encrypt (key, 'Hello World ')
- S2 = decrypt (key, s1)
- Print s1, '\ n', s2
-
- # HGKGDGDGAGPCIHAGNHDGLG
- # Hello world
-
-
- Note: This is a simple example on the Internet. You can customize your own algorithms for encryption and decryption. There are also many complicated encryption algorithms that you can refer to on your own cryptographic algorithms.
-
- 4. For python, you can also compile the python source code file into a pyc binary file, so that others will not see your source code. It is also an encryption method as follows:
- Run the python-m py_compile create_slave.py command to directly generate a create_slave.pyc file, and then use create_slave.pyc to replace create_slave.py for script execution.
This article from the "Wang Wei" blog, please be sure to keep this source http://wangwei007.blog.51cto.com/68019/1108784