Python string encryption and decryption methods (base64win32com)

Source: Internet
Author: User
This article mainly introduces three methods to encrypt and decrypt python strings, including base64 and win32com. client and self-written encryption and decryption algorithms. for details, refer to. 1. the simplest method is to use base64:

The code is as follows:


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 gets your ciphertext, they can decrypt it to obtain the plaintext.


2. The second method is to use win32com. client

The code is as follows:


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

3. you can also write your own encryption and decryption algorithms, such:

The code is as follows:


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

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.