[Python] string encryption and decryption

Source: Internet
Author: User

1. The simplest method is to use base64:

Import Base64

S1=Base64.encodestring ('Hello World')
S2=Base64.decodestring (S1)
PrintS1, S2

#Agvsbg8gd29ybgq = \ n
#Hello World

Note:This is the simplest method, but it is not safe enough, because if someone else gets your ciphertext, they can decrypt it to obtain the plaintext.


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, is the first choice for encryption and decryption!

 

3. Write encryption and decryption on your ownAlgorithmFor example:

Def Encrypt (Key, S ):
B = Bytearray (STR (s). encode ( " GBK " ))
N = Len (B) # Returns 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 and c2 are both 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 :
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, and there are many complicated encryption algorithms. You can refer to the cryptographic algorithms on your own.

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.