0

Upgraded My Python from 2.7 to python 3.6 facing issues while decrypting the password. If I put the encode() method then getting this Error "AttributeError: 'bytes' object has no attribute 'encode'" if I remove the encode() method getting "TypeError: token must be bytes" Below is the Code.

import requests
import json
import pymssql
from pymongo import MongoClient
from datetime import datetime
from cryptography.fernet import Fernet

encrypted_pwd='gAAAAABfcsCIlNNosZ2bWzUDueAVoIPjUFOqjNCOIOTUrOkrf_TK2FaC1L5b0VXo2ZKBz1VYA25jVfXBQQ5Y-vwTZA7339onZw=='

def vantiveDvDBConnection():
    conn = pymssql.connect('abc.def.ad.is.net:5010','itcompl',decrypt_message(encrypted_pwd), 'VN2DV')
    return conn

def decrypt_message(enc_message):
    print("inside decrypt")
    key=load_key()
    print(key)
    f=Fernet(key)
    print(enc_message.encode())
    decrypt_message=f.decrypt(enc_message.encode())
    return decrypt_message
                                                                           

Error : without encode() Method

inside decrypt
b'SV2DdXmRqkVRpK70yZOdEo1jlPx4mYrlUPYU5kpI0ds='
b'gAAAAABfcsCIlNNosZ2bWzUDueAVoIPjUFOqjNCOIOTUrOkrf_TK2FaC1L5b0VXo2ZKBz1VYA25jVfXBQQ5Y-vwTZA7339onZw=='
Traceback (most recent call last):
  File "dearvntaccessreview_report_dev.py", line 11, in <module>
    vantive_conn = utils.vantiveDvDBConnection()
  File "/tmp/CC-3145/commonPythonUtils.py", line 37, in vantiveDvDBConnection
    conn = pymssql.connect('vntdvdb01.savvis.ad.savvis.net:5004', 'itcompl',decrypt_message('gAAAAABfcsCIlNNosZ2bWzUDueAVoIPjUFOqjNCOIOTUrOkrf_TK2FaC1L5b0VXo2ZKBz1VYA25jVfXBQQ5Y-vwTZA7339onZw=='), 'VN2DV')
  File "/tmp/CC-3145/commonPythonUtils.py", line 92, in decrypt_message
    decrypt_message=f.decrypt(enc_message)
  File "/usr/local/lib/python3.6/site-packages/cryptography/fernet.py", line 75, in decrypt
    timestamp, data = Fernet._get_unverified_token_data(token)
  File "/usr/local/lib/python3.6/site-packages/cryptography/fernet.py", line 94, in _get_unverified_token_data
    utils._check_bytes("token", token)
  File "/usr/local/lib/python3.6/site-packages/cryptography/utils.py", line 29, in _check_bytes
    raise TypeError("{} must be bytes".format(name))
TypeError: token must be bytes

With Encode() Method:

inside decrypt
b'SV2DdXmRqkVRpK70yZOdEo1jlPx4mYrlUPYU5kpI0ds='
b'gAAAAABfcsCIlNNosZ2bWzUDueAVoIPjUFOqjNCOIOTUrOkrf_TK2FaC1L5b0VXo2ZKBz1VYA25jVfXBQQ5Y-vwTZA7339onZw=='
Traceback (most recent call last):
  File "dearvntaccessreview_report_dev.py", line 11, in <module>
    vantive_conn = utils.vantiveDvDBConnection()
  File "/tmp/CC-3145/commonPythonUtils.py", line 37, in vantiveDvDBConnection
    conn = pymssql.connect('vntdvdb01.savvis.ad.savvis.net:5004', 'itcompl',decrypt_message('gAAAAABfcsCIlNNosZ2bWzUDueAVoIPjUFOqjNCOIOTUrOkrf_TK2FaC1L5b0VXo2ZKBz1VYA25jVfXBQQ5Y-vwTZA7339onZw=='), 'VN2DV')
  File "src/pymssql.pyx", line 636, in pymssql.connect
  File "src/_mssql.pyx", line 1964, in _mssql.connect
  File "src/_mssql.pyx", line 611, in _mssql.MSSQLConnection.__init__
AttributeError: 'bytes' object has no attribute 'encode'

1 Answer 1

1

I think u need to use decode while decrypting and also u have to first decrypt and then decode:

def decrypt_message(enc_message):
    print("inside decrypt")
    key=load_key()
    print(key)
    f=Fernet(key)
    output = f.decrypt(enc_message)
    decrypt_message= output.decode('utf-8')
    print(enc_message))
    return decrypt_message

I am considering you are using utf-8 encoding format while encrypting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.