[Python]python Study Notes (vii)--encryption

Source: Internet
Author: User
Tags decrypt

1,python common methods for string encryption:

[Python]View Plaincopy
  1. <pre code_snippet_id="340592" snippet_file_name="blog_20140512_1_2282504" name="code" class= "Python" >1. The simplest way is to use base64:
  2. Import Base64
  3. S1 = base64.encodestring (' Hello World ')
  4. S2 = base64.decodestring (S1)
  5. Print S1,s2
  6. # agvsbg8gd29ybgq=n
  7. # Hello World
  8. 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.
  9. 2. The second method is to use the Win32com.client
  10. Import Win32com.client
  11. def encrypt (key,content): # Key: Key, Content: Clear text
  12. EncryptedData = Win32com.client.Dispatch (' CAPICOM. EncryptedData ')
  13. EncryptedData.Algorithm.KeyLength = 5
  14. EncryptedData.Algorithm.Name = 2
  15. Encrypteddata.setsecret (Key)
  16. Encrypteddata.content = Content
  17. return Encrypteddata.encrypt ()
  18. def decrypt (key,content): # Key: Key, content: Ciphertext
  19. EncryptedData = Win32com.client.Dispatch (' CAPICOM. EncryptedData ')
  20. EncryptedData.Algorithm.KeyLength = 5
  21. EncryptedData.Algorithm.Name = 2
  22. Encrypteddata.setsecret (Key)
  23. Encrypteddata.decrypt (content)
  24. str = encrypteddata.content
  25. return str
  26. S1 = Encrypt (' lovebread ', ' Hello World ')
  27. S2 = Decrypt (' Lovebread ', S1)
  28. Print S1,s2
  29. # MGEGCSSGAQQBGJDYA6BUMFIGCISGAQQBGJDYAWGGRDBCAGMCAAECAMYBAGFABAGQ
  30. # GPLLWJ9CSWQQH/FNBUZ6IJWKDTH9DLZMBGQYMFAZ3VFYS/LQ391ODTJLCRFGNXPX
  31. # lg7o
  32. # Hello World
  33. 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!
  34. 3. There are also write encryption and decryption algorithms, such as:
  35. def encrypt (key, s):
  36. b = ByteArray (str (s). Encode ("GBK"))
  37. n = len (b) # to find the number of bytes in B
  38. c = ByteArray (n2)
  39. j = 0
  40. for I in range (0, N):
  41. B1 = B[i]
  42. B2 = B1 ^ Key # B1 = b2^ key
  43. C1 = b2%
  44. C2 = b2// # b2 = c2*16 + C1
  45. C1 = C1 +
  46. C2 = C2 + # C1,C2 is the number between 0~15, plus 65 becomes the encoding of the a-p character
  47. C[J] = C1
  48. c[j+1] = C2
  49. j = j+2
  50. return C.decode ("GBK")
  51. Def decrypt (key, s):
  52. c = ByteArray (str (s). Encode ("GBK"))
  53. n = Len (c) # Calculates the number of bytes in B
  54. If n% 2! = 0:
  55. return ""
  56. n = n// 2
  57. b = ByteArray (n)
  58. j = 0
  59. for I in range (0, N):
  60. C1 = C[j]
  61. C2 = c[j+1]
  62. j = j+2
  63. C1 = C1-
  64. C2 = C2-
  65. B2 = c2*+ C1
  66. B1 = b2^ Key
  67. B[i]= B1
  68. Try:
  69. return B.decode ("GBK")
  70. except:
  71. return "failed"
  72. Key =
  73. S1 = Encrypt (key, ' Hello World ')
  74. S2 = Decrypt (key, S1)
  75. Print S1,' n ', S2
  76. # HGKGDGDGAGPCIHAGNHDGLG
  77. # Hello World
  78. 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.
  79. 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:
  80. 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

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.