Base64 library python

Base64 library python DEFAULT

The encoding scheme is used to convert arbitrary binary data to plain text. To do this, the encoder stores each group of three binary bytes as a group of four characters from the following set:

ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789+/

In addition, the character is used for padding at the end of the data stream.

Example 4-18 shows how the and functions work on file objects.

Example 4-18. Using the base64 Module to Encode Files

File: base64-example-1.py import base64 MESSAGE = "life of brian" file = open("out.txt", "w") file.write(MESSAGE) file.close() base64.encode(open("out.txt"), open("out.b64", "w")) base64.decode(open("out.b64"), open("out.txt", "w")) print "original:", repr(MESSAGE) print "encoded message:", repr(open("out.b64").read()) print "decoded message:", repr(open("out.txt").read())

Example 4-19 shows the and functions converting between strings. The functions are currently implemented as wrappers on top of and , using objects for input and output.

Example 4-19. Using the base64 Module to Encode Strings

File: base64-example-2.py import base64 MESSAGE = "life of brian" data = base64.encodestring(MESSAGE) original_data = base64.decodestring(data) print "original:", repr(MESSAGE) print "encoded data:", repr(data) print "decoded data:", repr(original_data)

Example 4-20 shows how to convert a username and a password to an HTTP basic authentication string. (Note that you don’t really have to work for the NSA to be able to decode this format.)

Example 4-20. Using the base64 Module for Basic Authentication

File: base64-example-3.py import base64 def getbasic(user, password): # basic authentication (according to HTTP) return base64.encodestring(user + ":" + password) print getbasic("Aladdin", "open sesame")

Finally, Example 4-21 shows a small utility that converts a GIF image to a Python script, for use with the Tkinter library.

Example 4-21. Using the base64 Module to Wrap GIF Images for Tkinter

File: base64-example-4.py import base64, sys if not sys.argv[1:]: print "Usage: gif2tk.py giffile >pyfile" sys.exit(1) data = open(sys.argv[1], "rb").read() if data[:4] != "GIF8": print sys.argv[1], "is not a GIF file" sys.exit(1) print '# generated from', sys.argv[1], 'by gif2tk.py' print print 'from Tkinter import PhotoImage' print print 'image = PhotoImage(data="""' print base64.encodestring(data), print '""")'
Sours: https://www.oreilly.com/library/view/python-standard-library/0596000960/ch04s11.html

Introduction

Base 64 encoding represents a common scheme for encoding binary into ASCII string format using radix 64. The base64 module is part of the standard library, which means it installs along with Python. Understanding of bytes and strings is critical to this topic and can be reviewed here. This topic explains how to use the various features and number bases of the base64 module.

Syntax

  • base64.b64encode(s, altchars=None)
  • base64.b64decode(s, altchars=None, validate=False)
  • base64.standard_b64encode(s)
  • base64.standard_b64decode(s)
  • base64.urlsafe_b64encode(s)
  • base64.urlsafe_b64decode(s)
  • base64.b32encode(s)
  • base64.b32decode(s)
  • base64.b16encode(s)
  • base64.b16decode(s)
  • base64.a85encode(b, foldspaces=False, wrapcol=0, pad=False, adobe=False)
  • base64.a85decode(b, foldpaces=False, adobe=False, ignorechars=b'\t\n\r\v')
  • base64.b85encode(b, pad=False)
  • base64.b85decode(b)

Parameters

ParameterDescription
sA bytes-like object
altcharsA bytes-like object of length 2+ of characters to replace the '+' and '=' characters when creating the Base64 alphabet. Extra characters are ignored.
sA bytes-like object
altcharsA bytes-like object of length 2+ of characters to replace the '+' and '=' characters when creating the Base64 alphabet. Extra characters are ignored.
validateIf valide is True, the characters not in the normal Base64 alphabet or the alternative alphabet are not discarded before the padding check
sA bytes-like object
sA bytes-like object
sA bytes-like object
sA bytes-like object
sA bytes-like object
sA bytes-like object
sA bytes-like object
sA bytes-like object
bA bytes-like object
foldspacesIf foldspaces is True, the character 'y' will be used instead of 4 consecutive spaces.
wrapcolThe number characters before a newline (0 implies no newlines)
padIf pad is True, the bytes are padded to a multiple of 4 before encoding
adobeIf adobe is True, the encoded sequened with be framed with '<~' and ''~>' as used with Adobe products
bA bytes-like object
foldspacesIf foldspaces is True, the character 'y' will be used instead of 4 consecutive spaces.
adobeIf adobe is True, the encoded sequened with be framed with '<~' and ''~>' as used with Adobe products
ignorecharsA bytes-like object of characters to ignore in the encoding process
bA bytes-like object
padIf pad is True, the bytes are padded to a multiple of 4 before encoding
bA bytes-like object

Up until Python 3.4 came out, base64 encoding and decoding functions only worked with or types. Now these functions accept any bytes-like object.

Encoding and Decoding Base64

To include the base64 module in your script, you must import it first:

The base64 encode and decode functions both require a bytes-like object. To get our string into bytes, we must encode it using Python's built in encode function. Most commonly, the encoding is used, however a full list of these standard encodings (including languages with different characters) can be found here in the official Python Documentation. Below is an example of encoding a string into bytes:

The output of the last line would be:

The prefix is used to denote the value is a bytes object.

To Base64 encode these bytes, we use the function:

That code would output the following:

which is still in the bytes object. To get a string out of these bytes, we can use Python's method with the encoding:

The output would then be:

If we wanted to encode the string and then decode we could use the method:

As you may have expected, the output would be the original string:

Encoding and Decoding Base32

The base64 module also includes encoding and decoding functions for Base32. These functions are very similar to the Base64 functions:

This would produce the following output:

Encoding and Decoding Base16

The base64 module also includes encoding and decoding functions for Base16. Base 16 is most commonly referred to as hexadecimal. These functions are very similar to the both the Base64 and Base32 functions:

This would produce the following output:

Encoding and Decoding ASCII85

Adobe created it's own encoding called ASCII85 which is similar to Base85, but has its differences. This encoding is used frequently in Adobe PDF files. These functions were released in Python version 3.4. Otherwise, the functions and are similar to the previous:

This outputs the following:

Encoding and Decoding Base85

Just like the Base64, Base32, and Base16 functions, the Base85 encoding and decoding functions are and :

which outputs the following:






Previous Next

Sours: https://sodocumentation.net/python/topic/8678/the-base64-module
  1. Gen 2 nkjv
  2. Datemyage.com reviews
  3. Liquidation pallets iowa

Encoding and Decoding Base64 Strings in Python

The Base64 encoding is used to convert bytes that have binary or text data into ASCII characters. Encoding prevents the data from getting corrupted when it is transferred or processed through a text-only system. In this article, we will discuss about Base64 encoding and decoding and its uses to encode and decode binary and text data.

Base64 encoding:
It is a type of conversion of bytes to ASCII characters. the list of available Base64 characters are mentioned below:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

  • 26 uppercase letters
  • 26 lowercase letters
  • 10 numbers
  • + and / for new lines

Each Base64 character represents 6 bits of data. it is also important to note that it is not meant for encryption for obvious reasons.
To convert a string into a Base64 character the following steps should be followed:



  • Get the ASCII value of each character in the string.
  • Compute the 8-bit binary equivalent of the ASCII values
  • Convert the 8-bit characters chunk into chunks of 6 bits by re-grouping the digits
  • Convert the 6-bit binary groups to their respective decimal values.
  • Use the Base64 encoding table to align the respective Base64 values for each decimal value.

The below image provides us with a Base64 encoding table.

Image Source: Wikipedia

 

Using python to encode strings:
In Python the base64 module is used to encode and decode data. First, the strings are converted into byte-like objects and then encoded using the base64 module. The below example shows the implementation of encoding strings isn’t base64 characters.

Example:

 

 

 

Output:

Encoded string: R2Vla3NGb3JHZWVrcyBpcyB0aGUgYmVzdA==

Using Python to decode strings:
Decoding Base64 string is exactly opposite to that of encoding. First we convert the Base64 strings into unencoded data bytes followed by conversion into bytes-like object into a string. The below example depicts the decoding of the above example encode string output.

Example:

 

 

 

 

Output:

Decoded string: GeeksForGeeks is the best


Sours: https://www.geeksforgeeks.org/encoding-and-decoding-base64-strings-in-python/
Python 3 Script to Encode a PDF File to Base64 String Using base64 Library Full Tutorial For Noobs

Base64 Encoding in Python

Python’s Base64 module provides functions to encode binary data to Base64 encoded format and decode such encodings back to binary data.

It implements Base64 encoding and decoding as specified in RFC 3548.

This article contains examples that demonstrate how to perform Base64 encoding in Python.

Python Base64 Encoding Example

You can use the function provided by the module to perform Base64 encoding. It accepts a bytes-like object and returns the Base64 encoded bytes -

Python Base64 URL and Filename safe Encoding

The default functions uses the standard Base64 alphabet that contains characters , , , , and . Since and characters are not URL and filename safe, The RFC 3548 defines another variant of Base64 encoding whose output is URL and Filename safe. This variant replaces with minus () and with underscore ()

Also Read: Python Base64 Decode Example

References

Sours: https://www.base64encoder.io/python/

Python base64 library

Usage

uses the same API as Python base64 “modern interface” (introduced in Python 2.4) for an easy integration.

To get the fastest decoding, it is recommended to use the and when possible.

importpybase64print(pybase64.b64encode(b'>>>foo???',altchars='_:'))# b'Pj4_Zm9vPz8:'print(pybase64.b64decode(b'Pj4_Zm9vPz8:',altchars='_:',validate=True))# b'>>>foo???'# Standard encoding helpersprint(pybase64.standard_b64encode(b'>>>foo???'))# b'Pj4+Zm9vPz8/'print(pybase64.standard_b64decode(b'Pj4+Zm9vPz8/'))# b'>>>foo???'# URL safe encoding helpersprint(pybase64.urlsafe_b64encode(b'>>>foo???'))# b'Pj4-Zm9vPz8_'print(pybase64.urlsafe_b64decode(b'Pj4-Zm9vPz8_'))# b'>>>foo???'

A command-line tool is also provided. It has encode, decode and benchmark subcommands.

usage: pybase64 [-h] [-V] {benchmark,encode,decode} ... pybase64 command-line tool. positional arguments: {benchmark,encode,decode} tool help benchmark -h for usage encode -h for usage decode -h for usage optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit

Full documentation on Read the Docs.

Benchmark

Running Python 3.7.2, Apple LLVM version 10.0.0 (clang-1000.11.45.5), Mac OS X 10.14.2 on an Intel Core i7-4870HQ @ 2.50GHz

pybase64 0.5.0 (C extension active - AVX2) bench: altchars=None, validate=False pybase64._pybase64.encodebytes: 1734.776 MB/s (13,271,472 bytes -> 17,928,129 bytes) pybase64._pybase64.b64encode: 4039.539 MB/s (13,271,472 bytes -> 17,695,296 bytes) pybase64._pybase64.b64decode: 1854.423 MB/s (17,695,296 bytes -> 13,271,472 bytes) base64.encodebytes: 78.352 MB/s (13,271,472 bytes -> 17,928,129 bytes) base64.b64encode: 539.840 MB/s (13,271,472 bytes -> 17,695,296 bytes) base64.b64decode: 287.826 MB/s (17,695,296 bytes -> 13,271,472 bytes) bench: altchars=None, validate=True pybase64._pybase64.b64encode: 4156.607 MB/s (13,271,472 bytes -> 17,695,296 bytes) pybase64._pybase64.b64decode: 4107.997 MB/s (17,695,296 bytes -> 13,271,472 bytes) base64.b64encode: 559.342 MB/s (13,271,472 bytes -> 17,695,296 bytes) base64.b64decode: 143.674 MB/s (17,695,296 bytes -> 13,271,472 bytes) bench: altchars=b'-_', validate=False pybase64._pybase64.b64encode: 2786.776 MB/s (13,271,472 bytes -> 17,695,296 bytes) pybase64._pybase64.b64decode: 1124.136 MB/s (17,695,296 bytes -> 13,271,472 bytes) base64.b64encode: 322.427 MB/s (13,271,472 bytes -> 17,695,296 bytes) base64.b64decode: 205.195 MB/s (17,695,296 bytes -> 13,271,472 bytes) bench: altchars=b'-_', validate=True pybase64._pybase64.b64encode: 2806.271 MB/s (13,271,472 bytes -> 17,695,296 bytes) pybase64._pybase64.b64decode: 2740.456 MB/s (17,695,296 bytes -> 13,271,472 bytes) base64.b64encode: 314.709 MB/s (13,271,472 bytes -> 17,695,296 bytes) base64.b64decode: 121.803 MB/s (17,695,296 bytes -> 13,271,472 bytes)

Changelog

1.2.0

  • Release the GIL
  • Publish CPython 3.10 wheels
  • Drop python 3.5 support

1.1.3

  • GitHub Actions: fix build on tag

1.1.2

  • Add PyPy wheels
  • Add aarch64, ppc64le & s390x manylinux wheels

1.1.1

  • Move CI from TravisCI/AppVeyor to GitHub Actions
  • Fix publication of Linux/macOS wheels

1.1.0

  • Add b64encode_as_string, same as b64encode but returns a str object instead of a bytes object
  • Add b64decode_as_bytearray, same as b64decode but returns a bytarray object instead of a bytes object
  • Speed-Up decoding from UCS1 strings

1.0.2

  • Update base64 library
  • Publish python 3.9 wheels

1.0.1

  • Publish python 3.8 wheels

1.0.0

  • Drop python 3.4 support
  • Drop python 2.7 support

0.5.0

  • Publish python 3.7 wheels
  • Drop python 3.3 support

0.4.0

  • Speed-up decoding when validate==False

0.2.1

  • Fixed invalid results on Windows

0.2.0

  • Added documentation

  • Added subcommands to the main script:

    • help
    • version
    • encode
    • decode
    • benchmark

0.1.2

  • Updated base64 native library
Sours: https://pypi.org/project/pybase64/
Python: Encoding and Decoding base64

— Base16, Base32, Base64, Base85 Data Encodings¶

Source code:Lib/base64.py


This module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data. It provides encoding and decoding functions for the encodings specified in RFC 4648, which defines the Base16, Base32, and Base64 algorithms, and for the de-facto standard Ascii85 and Base85 encodings.

The RFC 4648 encodings are suitable for encoding binary data so that it can be safely sent by email, used as parts of URLs, or included as part of an HTTP POST request. The encoding algorithm is not the same as the uuencode program.

There are two interfaces provided by this module. The modern interface supports encoding bytes-like objects to ASCII , and decoding bytes-like objects or strings containing ASCII to . Both base-64 alphabets defined in RFC 4648 (normal, and URL- and filesystem-safe) are supported.

The legacy interface does not support decoding from strings, but it does provide functions for encoding and decoding to and from file objects. It only supports the Base64 standard alphabet, and it adds newlines every 76 characters as per RFC 2045. Note that if you are looking for RFC 2045 support you probably want to be looking at the package instead.

Changed in version 3.3: ASCII-only Unicode strings are now accepted by the decoding functions of the modern interface.

Changed in version 3.4: Any bytes-like objects are now accepted by all encoding and decoding functions in this module. Ascii85/Base85 support added.

The modern interface provides:

(s, altchars=None

Encode the bytes-like objects using Base64 and return the encoded .

Optional altchars must be a bytes-like object of at least length 2 (additional characters are ignored) which specifies an alternative alphabet for the and characters. This allows an application to e.g. generate URL or filesystem safe Base64 strings. The default is , for which the standard Base64 alphabet is used.

(s, altchars=None, validate=False

Decode the Base64 encoded bytes-like object or ASCII string s and return the decoded .

Optional altchars must be a bytes-like object or ASCII string of at least length 2 (additional characters are ignored) which specifies the alternative alphabet used instead of the and characters.

A exception is raised if s is incorrectly padded.

If validate is (the default), characters that are neither in the normal base-64 alphabet nor the alternative alphabet are discarded prior to the padding check. If validate is , these non-alphabet characters in the input result in a .

(s

Encode bytes-like objects using the standard Base64 alphabet and return the encoded .

(s

Decode bytes-like object or ASCII string s using the standard Base64 alphabet and return the decoded .

(s

Encode bytes-like objects using the URL- and filesystem-safe alphabet, which substitutes instead of and instead of in the standard Base64 alphabet, and return the encoded . The result can still contain .

(s

Decode bytes-like object or ASCII string s using the URL- and filesystem-safe alphabet, which substitutes instead of and instead of in the standard Base64 alphabet, and return the decoded .

(s

Encode the bytes-like objects using Base32 and return the encoded .

(s, casefold=False, map01=None

Decode the Base32 encoded bytes-like object or ASCII string s and return the decoded .

Optional casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is .

RFC 4648 allows for optional mapping of the digit 0 (zero) to the letter O (oh), and for optional mapping of the digit 1 (one) to either the letter I (eye) or letter L (el). The optional argument map01 when not , specifies which letter the digit 1 should be mapped to (when map01 is not , the digit 0 is always mapped to the letter O). For security purposes the default is , so that 0 and 1 are not allowed in the input.

A is raised if s is incorrectly padded or if there are non-alphabet characters present in the input.

(s

Similar to but uses the Extended Hex Alphabet, as defined in RFC 4648.

(s, casefold=False

Similar to but uses the Extended Hex Alphabet, as defined in RFC 4648.

This version does not allow the digit 0 (zero) to the letter O (oh) and digit 1 (one) to either the letter I (eye) or letter L (el) mappings, all these characters are included in the Extended Hex Alphabet and are not interchangeable.

(s

Encode the bytes-like objects using Base16 and return the encoded .

(s, casefold=False

Decode the Base16 encoded bytes-like object or ASCII string s and return the decoded .

Optional casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is .

A is raised if s is incorrectly padded or if there are non-alphabet characters present in the input.

(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False

Encode the bytes-like objectb using Ascii85 and return the encoded .

foldspaces is an optional flag that uses the special short sequence ‘y’ instead of 4 consecutive spaces (ASCII 0x20) as supported by ‘btoa’. This feature is not supported by the “standard” Ascii85 encoding.

wrapcol controls whether the output should have newline () characters added to it. If this is non-zero, each output line will be at most this many characters long.

pad controls whether the input is padded to a multiple of 4 before encoding. Note that the implementation always pads.

adobe controls whether the encoded byte sequence is framed with and , which is used by the Adobe implementation.

(b, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\x0b'

Decode the Ascii85 encoded bytes-like object or ASCII string b and return the decoded .

foldspaces is a flag that specifies whether the ‘y’ short sequence should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20). This feature is not supported by the “standard” Ascii85 encoding.

adobe controls whether the input sequence is in Adobe Ascii85 format (i.e. is framed with <~ and ~>).

ignorechars should be a bytes-like object or ASCII string containing characters to ignore from the input. This should only contain whitespace characters, and by default contains all whitespace characters in ASCII.

(b, pad=False

Encode the bytes-like objectb using base85 (as used in e.g. git-style binary diffs) and return the encoded .

If pad is true, the input is padded with so its length is a multiple of 4 bytes before encoding.

(b

Decode the base85-encoded bytes-like object or ASCII string b and return the decoded . Padding is implicitly removed, if necessary.

The legacy interface:

(input, output

Decode the contents of the binary input file and write the resulting binary data to the output file. input and output must be file objects. input will be read until returns an empty bytes object.

(s

Decode the bytes-like objects, which must contain one or more lines of base64 encoded data, and return the decoded .

(input, output

Encode the contents of the binary input file and write the resulting base64 encoded data to the output file. input and output must be file objects. input will be read until returns an empty bytes object. inserts a newline character () after every 76 bytes of the output, as well as ensuring that the output always ends with a newline, as per RFC 2045 (MIME).

(s

Encode the bytes-like objects, which can contain arbitrary binary data, and return containing the base64-encoded data, with newlines () inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per RFC 2045 (MIME).

An example usage of the module:

>>> importbase64>>> encoded=base64.b64encode(b'data to be encoded')>>> encodedb'ZGF0YSB0byBiZSBlbmNvZGVk'>>> data=base64.b64decode(encoded)>>> datab'data to be encoded'
Sours: https://docs.python.org/3/library/base64.html

Similar news:

base64 – Encode binary data into ASCII characters¶

Purpose:The base64 module contains functions for translating binary data into a subset of ASCII suitable for transmission using plaintext protocols.
Available In:1.4 and later

The base64, base32, and base16 encodings convert 8 bit bytes to values with 6, 5, or 4 bits of useful data per byte, allowing non-ASCII bytes to be encoded as ASCII characters for transmission over protocols that require plain ASCII, such as SMTP. The base values correspond to the length of the alphabet used in each encoding. There are also URL-safe variations of the original encodings that use slightly different results.

Base 64 Encoding¶

A basic example of encoding some text looks like this:

importbase64# Load this source file and strip the header.initial_data=open(__file__,'rt').read().split('#end_pymotw_header')[1]encoded_data=base64.b64encode(initial_data)num_initial=len(initial_data)padding={0:0,1:2,2:1}[num_initial%3]print'%d bytes before encoding'%num_initialprint'Expect %d padding bytes'%paddingprint'%d bytes after encoding'%len(encoded_data)print#print encoded_dataforiinxrange((len(encoded_data)/40)+1):printencoded_data[i*40:(i+1)*40]

The output shows the 558 bytes of the original source expand to 744 bytes after being encoded.

Note

There are no carriage returns in the output produced by the library, so I have broken the encoded data up artificially to make it fit better on the page.

$ python base64_b64encode.py 113 bytes before encoding Expect 1 padding bytes 152 bytes after encoding CgppbXBvcnQgYmFzZTY0CgojIExvYWQgdGhpcyBz b3VyY2UgZmlsZSBhbmQgc3RyaXAgdGhlIGhlYWRl ci4KaW5pdGlhbF9kYXRhID0gb3BlbihfX2ZpbGVf XywgJ3J0JykucmVhZCgpLnNwbGl0KCc=

Base 64 Decoding¶

The encoded string can be converted back to the original form by taking 4 bytes and converting them to the original 3, using a reverse lookup. The function does that for you.

importbase64original_string='This is the data, in the clear.'print'Original:',original_stringencoded_string=base64.b64encode(original_string)print'Encoded :',encoded_stringdecoded_string=base64.b64decode(encoded_string)print'Decoded :',decoded_string

The encoding process looks at each sequence of 24 bits in the input (3 bytes) and encodes those same 24 bits spread over 4 bytes in the output. The last two characters, the , are padding because the number of bits in the original string was not evenly divisible by 24 in this example.

$ python base64_b64decode.py Original: This is the data, in the clear. Encoded : VGhpcyBpcyB0aGUgZGF0YSwgaW4gdGhlIGNsZWFyLg== Decoded : This is the data, in the clear.

URL-safe Variations¶

Because the default base64 alphabet may use and , and those two characters are used in URLs, it became necessary to specify an alternate encoding with substitutes for those characters. The is replaced with a , and is replaced with underscore (). Otherwise, the alphabet is the same.

importbase64fororiginalin[chr(251)+chr(239),chr(255)*2]:print'Original :',repr(original)print'Standard encoding:',base64.standard_b64encode(original)print'URL-safe encoding:',base64.urlsafe_b64encode(original)print
$ python base64_urlsafe.py Original : '\xfb\xef' Standard encoding: ++8= URL-safe encoding: --8= Original : '\xff\xff' Standard encoding: //8= URL-safe encoding: __8=

Other Encodings¶

Besides base 64, the module provides functions for working with base 32 and base 16 (hex) encoded data.

importbase64original_string='This is the data, in the clear.'print'Original:',original_stringencoded_string=base64.b32encode(original_string)print'Encoded :',encoded_stringdecoded_string=base64.b32decode(encoded_string)print'Decoded :',decoded_string
$ python base64_base32.py Original: This is the data, in the clear. Encoded : KRUGS4ZANFZSA5DIMUQGIYLUMEWCA2LOEB2GQZJAMNWGKYLSFY====== Decoded : This is the data, in the clear.

The base 16 functions work with the hexadecimal alphabet.

importbase64original_string='This is the data, in the clear.'print'Original:',original_stringencoded_string=base64.b16encode(original_string)print'Encoded :',encoded_stringdecoded_string=base64.b16decode(encoded_string)print'Decoded :',decoded_string
$ python base64_base16.py Original: This is the data, in the clear. Encoded : 546869732069732074686520646174612C20696E2074686520636C6561722E Decoded : This is the data, in the clear.

See also

base64
The standard library documentation for this module.
RFC 3548
The Base16, Base32, and Base64 Data Encodings
Sours: http://pymotw.com/2/base64/


198 199 200 201 202