Summary of Python string encryption and decryption Methods

Source: Internet
Author: User

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. 1. The simplest method is to use base64:
  2.  
  3. Import base64
  4.  
  5. S1 = base64.encodestring ('Hello World ')
  6. S2 = base64.decodestring (s1)
  7. Print s1, s2
  8.  
  9. # AGVsbG8gd29ybGQ = \ n
  10. # Hello world
  11.  
  12. 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.
  13.  
  14.  
  15.  
  16.  
  17. 2. The second method is to use win32com. client
  18.  
  19. Import win32com. client
  20. Def encrypt (key, content): # key: key, content: plaintext
  21. EncryptedData = win32com. client. Dispatch ('capicom. encrypteddata ')
  22. EncryptedData. Algorithm. KeyLength = 5
  23. EncryptedData. Algorithm. Name = 2
  24. EncryptedData. SetSecret (key)
  25. EncryptedData. Content = content
  26. Return EncryptedData. Encrypt ()
  27.  
  28. Def decrypt (key, content): # key: key, content: ciphertext
  29. EncryptedData = win32com. client. Dispatch ('capicom. encrypteddata ')
  30. EncryptedData. Algorithm. KeyLength = 5
  31. EncryptedData. Algorithm. Name = 2
  32. EncryptedData. SetSecret (key)
  33. EncryptedData. Decrypt (content)
  34. Str = EncryptedData. Content
  35. Return str
  36.  
  37. S1 = encrypt ('lovebread', 'Hello World ')
  38. S2 = decrypt ('lovebread', s1)
  39. Print s1, s2
  40.  
  41. # MGEGCSsGAQQBgjdYA6BUMFIGCisGAQQBgjdYAwGgRDBCAgMCAAECAmYBAgFABAgq
  42. # GpllWj9cswQQh/fnBUZ6ijwKDTH9DLZmBgQYmfaZ3VFyS/lq391oDtjlcRFGnXpx
  43. # LG7o
  44. # Hello world
  45.  
  46. 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!
  47.  
  48.  
  49. 3. You can also write your own encryption and decryption algorithms, such:
  50.  
  51. Def encrypt (key, s ):
  52. B = bytearray (str (s). encode ("gbk "))
  53. N = len (B) # calculate the number of bytes of B
  54. C = bytearray (n * 2)
  55. J = 0
  56. For I in range (0, n ):
  57. B1 = B [I]
  58. B2 = b1 ^ key # b1 = b2 ^ key
  59. C1 = b2 % 16
  60. C2 = b2 // 16 # b2 = c2 * 16 + c1
  61. C1 = c1 + 65
  62. C2 = c2 + 65 # c1, c2 are all 0 ~ The number between 15, plus 65 becomes the character encoding of the A-P
  63. C [j] = c1
  64. C [j + 1] = c2
  65. J = j + 2
  66. Return c. decode ("gbk ")
  67.  
  68. Def decrypt (key, s ):
  69. C = bytearray (str (s). encode ("gbk "))
  70. N = len (c) # calculate the number of bytes of B
  71. If n % 2! = 0:
  72. Return ""
  73. N = n // 2
  74. B = bytearray (n)
  75. J = 0
  76. For I in range (0, n ):
  77. C1 = c [j]
  78. C2 = c [j + 1]
  79. J = j + 2
  80. C1 = c1-65
  81. C2 = c2-65
  82. B2 = c2 * 16 + c1
  83. B1 = b2 ^ key
  84. B [I] = b1
  85. Try:
  86. Return B. decode ("gbk ")
  87. Except t:
  88. Return "failed"
  89.  
  90. Key = 15
  91. S1 = encrypt (key, 'Hello World ')
  92. S2 = decrypt (key, s1)
  93. Print s1, '\ n', s2
  94.  
  95. # HGKGDGDGAGPCIHAGNHDGLG
  96. # Hello world
  97.  
  98.  
  99. 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.
  100.  
  101. 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:
  102. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.