decoding base64 guid in python

python uuid
decode base64 string python
python base64 decode utf-8
python base64 decode incorrect padding
convert base64 to image python
python base64 decode file
python3 base64 decode
python base64 encode string

I am trying to convert a base64 string back to a GUID style hex number in python and having issues.

Base64 encoded string is: bNVDIrkNbEySjZ90ypCLew==

And I need to get it back to: 2243d56c-0db9-4c6c-928d-9f74ca908b7b

I can do it with the following PHP code but can't work out how to to it in Python

function Base64ToGUID($guid_b64) {
    $guid_bin = base64_decode($guid_b64);

    return join('-', array(
      bin2hex(strrev(substr($guid_bin, 0, 4))),
      bin2hex(strrev(substr($guid_bin, 4, 2))),
      bin2hex(strrev(substr($guid_bin, 6, 2))),
      bin2hex(substr($guid_bin, 8, 2)),
      bin2hex(substr($guid_bin, 10, 6))

Here is the GUIDtoBase64 version:

function GUIDToBase64($guid) {
    $guid_b64 = '';
    $guid_parts = explode('-', $guid);

    foreach ($guid_parts as $k => $part) {
      if ($k < 3)
        $part = join('', array_reverse(str_split($part, 2)));

      $guid_b64 .= pack('H*', $part);

    return base64_encode($guid_b64);

Here are some of the results using some of the obvious and not so obvious options:

import base64
import binascii
>>> base64.b64decode("bNVDIrkNbEySjZ90ypCLew==")
>>> binascii.hexlify(base64.b64decode("bNVDIrkNbEySjZ90ypCLew=="))

Python port of the existing function (bitstring required)

import bitstring, base64

def base64ToGUID(b64str):
  s = bitstring.BitArray(bytes=base64.b64decode(b64str)).hex
  def rev2(s_):
    def chunks(n):
      for i in xrange(0, len(s_), n):
          yield s_[i:i+n]
    return "".join(list(chunks(2))[::-1])
  return "-".join([rev2(s[:8]),rev2(s[8:][:4]),rev2(s[12:][:4]),s[16:][:4],s[20:]])

assert base64ToGUID("bNVDIrkNbEySjZ90ypCLew==") == "2243d56c-0db9-4c6c-928d-9f74ca908b7b"

Encoding and Decoding Base64 Strings in Python, We will then use Python to Base64 encode and decode both text and binary data. What Is Base64 Encoding? How Does Base64 Encoding Work? Why Use� 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.

First off, the b64 string and the resultant GUID doesn't match if we decode properly.

>>> import uuid
>>> import base64
>>> u = uuid.UUID("2243d56c-0db9-4c6c-928d-9f74ca908b7b")
>>> u
>>> u.bytes
>>> base64.b64encode(u.bytes)
>>> b = base64.b64decode('bNVDIrkNbEySjZ90ypCLew==')
>>> u2 = uuid.UUID(bytes=b)
>>> print u2

The base64 encoded version of the resultant GUID you posted is wrong. I'm not sure I understand the way you're encoding the GUID in the first place.

Python has in its arsenal all the tools required for you to be able to answer this problem. However, here's the rough scratching I did in a python terminal:

import uuid
import base64

base64_guid = "bNVDIrkNbEySjZ90ypCLew=="
bin_guid = base64.b64decode(base64_guid)
guid = uuid.UUID(bytes=bin_guid)
print guid

This code should give you enough of a hint to build your own functions. Don't forget, the python shell gives you a powerful tool to test and play with code and ideas. I would investigate using something like IPython notebooks.

Generate URL-safe UUID/GUIDs in Python. � GitHub, Generate URL-safe UUID/GUIDs in Python, e.g.. #. # ob9G9Ju_Re6SRgxacdUzhw. # k0CWKgThQq-9b2ZcmpVXXA. #. # base64 has an urlsafe encoding. Base64 Decoding in Python. Rajeev Singh1mins. Python’s base64 moduleprovides functions to perform Base64 encoding and decoding as described in RFC 3548. This article contains examples that demonstrate how to decode any Base64 encoded data back to binary data.

I needed to do this to decode a BASE64 UUID that had been dumped from Mongodb. Originally the field had been created by Mongoose. The code I used, based on the code by @tpatja is here:

def base64ToGUID(b64str):
  except Exception as e:
    print("Can't decode base64 ", e)
  s = bitstring.BitArray(bytes).hex
  return "-".join([s[:8],s[8:][:4],s[12:][:4],s[16:][:4],s[20:]])

Base64 Encoding and Decoding Using Python, Base64 encoding is the process of converting binary data into a limited Learn Python with our complete python tutorial guide, whether you're� Decoding Binary Data with Python. Base64 decoding binary is similar to Base64 decoding text data. The key difference is that after we Base64 decode the string, we save the data as a binary file instead of a string. Let's see how to Base64 decode binary data in practice by creating a new file called Type the following code into the Python file:

decoding base64 guid in python, I am trying to convert a base64 string back to a GUID style hex number in python and having issues. Base64 encoded string is: bNVDIrkNbEySjZ90ypCLew==� We can achieve the same thing with a Python script like this: #!/usr/bin/env python import base64 # Replace the quoted text with the code you wish to decrypt. coded_string = 'SG9va2VkIG9uIHBob25pY3Mgd29ya2VkIGZvciBtZQo=' # Decrypt the code string. code_dump = base64.b64decode(coded_string) # Print the decryption output to the screen.

20.15. uuid — UUID objects according to RFC 4122 — Python 2.7 , This module provides immutable UUID objects (the UUID class) and the functions uuid1() , uuid3() , uuid4() , uuid5() for generating version 1, 3,� To decode an image using Python, we simply use the base64.decodestring(s) function. Python mentions the following regarding this function: Decode the string s, which must contain one or more lines of base64 encoded data, and return a string containing the resulting binary data.

Base64 Encoding and Decoding, Base64 is also called as Privacy enhanced Electronic mail (PEM) and is primarily used in email encryption process. Python includes a module called BASE64� Then in python decode the string (also add '=' until the length is divisible by 4) def decode_base64(data): data += '=' * (len(data) % 4) return base64.urlsafe_b64decode(data) Further if you want to use the image in openCV.

  • Have you tried? Feel free to look at base64.
  • Yes I have tried most of the base64.b64decode options etc. Let me update with the results
  • Well you can see that your base64 encoded data does not correspond to what you expect it to be.
  • Why not post the function thats encoding the B64 GUIDs?
  • @Andrew where did the original php code originate?
  • Problem is I am replicating another system where this is working e.g. output from the above PHP code php guid2b64.php 2243d56c-0db9-4c6c-928d-9f74ca908b7b using <?= Base64ToGUID("bNVDIrkNbEySjZ90ypCLew==");?>
  • Right so there must be some form of algorithm it's using. Post the GUIDToBase64() function. This is clearly not a straight encode/decode of a GUID.