Need a Python code to convert a secret key to base64 encoded

5.4k views Asked by At

I have a secret key generated - A0[Bt"V59.xA-bKO|/A""/Z.!#Y:wfpR , need to convert this to base64 encoded format , Please help with the python code to do this.

1

There are 1 answers

0
Anand S Kumar On

For encoding to base64 use the base64 module and its function b64encode for that.

Example -

>>> import base64
>>> base64.b64encode("Hello")
'SGVsbG8='
>>> s = 'A0[Bt\"V59.xA-bKO|/A\"\"/Z.!#Y:wfpR'
>>> base64.b64encode(s)
'QTBbQnQiVjU5LnhBLWJLT3wvQSIiL1ouISNZOndmcFI='

For decoding , use the b64decode() function -

>>> import base64
>>> base64.b64decode('SGVsbG8=')
'Hello'
>>> base64.b64decode('QTBbQnQiVjU5LnhBLWJLT3wvQSIiL1ouISNZOndmcFI=')
'A0[Bt"V59.xA-bKO|/A""/Z.!#Y:wfpR'