base64 encoding and decoding in python
introduction:
In some projects, the packet is transmitted through the interface base64 encrypted, so when performing automated interfaces, parameters need to be transmitted base64 encoding, get response packet to be decoded;
Base64 encoding is a "gentleman not anti-anti-villain" encoding. Widely used in MIME protocol, e-mail as a transmission coding, coding to generate reversible, after one or two may have "=" ascii character codes are generated.
Advantages: fast, ASCII characters can not be visually understood
disadvantages: encoding relatively long, very easy to be cracked, only for the case of non-critical information encrypted
python2 Base64 encoding and decoding is performed
>>> Import Base64
>>> S = 'I string '
>>> base64.b64encode A = (S)
>>> Print A
ztLKx9fWt / u0rg ==
>>> Print base64.b64decode (A)
I is a string
python3 not the same: 3.x because the characters are unicode encoded and the parameter b64encode function is byte type, so you must transcoding.
Base64 Import encodestr = base64.b64encode ( 'abcr34r344r'.encode (' UTF-8 ')) Print (encodestr)
print results
b'YWJjcjM0cjM0NHI ='
results and we expected a little bit different, we just want to get YWJjcjM0cjM0NHI =, and string is b '' surrounded.
Then certainly it was said, with the regular taken out just fine. . . Do not worry. . .
b represents the byte mean, we just like to go back again byte conversion. . . Source follows
Base64 Import encodestr = base64.b64encode ( 'abcr34r344r'.encode (' UTF-. 8 ')) Print (STR (encodestr,' UTF-. 8 '))
print result
YWJjcjM0cjM0NHI =
introduction:
In some projects, the packet is transmitted through the interface base64 encrypted, so when performing automated interfaces, parameters need to be transmitted base64 encoding, get response packet to be decoded;
Base64 encoding is a "gentleman not anti-anti-villain" encoding. Widely used in MIME protocol, e-mail as a transmission coding, coding to generate reversible, after one or two may have "=" ascii character codes are generated.
Advantages: fast, ASCII characters can not be visually understood
disadvantages: encoding relatively long, very easy to be cracked, only for the case of non-critical information encrypted
python2 Base64 encoding and decoding is performed
>>> Import Base64
>>> S = 'I string '
>>> base64.b64encode A = (S)
>>> Print A
ztLKx9fWt / u0rg ==
>>> Print base64.b64decode (A)
I is a string
python3 not the same: 3.x because the characters are unicode encoded and the parameter b64encode function is byte type, so you must transcoding.
Base64 Import encodestr = base64.b64encode ( 'abcr34r344r'.encode (' UTF-8 ')) Print (encodestr)
print results
b'YWJjcjM0cjM0NHI ='
results and we expected a little bit different, we just want to get YWJjcjM0cjM0NHI =, and string is b '' surrounded.
Then certainly it was said, with the regular taken out just fine. . . Do not worry. . .
b represents the byte mean, we just like to go back again byte conversion. . . Source follows
Base64 Import encodestr = base64.b64encode ( 'abcr34r344r'.encode (' UTF-. 8 ')) Print (STR (encodestr,' UTF-. 8 '))
print result
YWJjcjM0cjM0NHI =