How to decode a byte array to string array in Python - python

Hello I used encryption algorithm called fernet to encrypt values in the database. I want to retrieve those values decrypted. The array which is being returned is a byte array, but I would like to decode it. It doesn't seem to be working. I am getting this error:
Traceback (most recent call last):
File "C:\Users\d\OneDrive\Bureaublad\Assignment8\CDMS.py", line 51, in <module>
get_clients()
File "C:\Users\d\OneDrive\Bureaublad\Assignment8\CDMS.py", line 41, in get_clients
print(arr[i].decode())
TypeError: list indices must be integers or slices, not bytes
This is the array which im trying to decode:
[(b'gAAAAABg5EBDnm6LZdHNzzjnDlFcA80SeS1s-wXhKx_opwOm0vSNsfcxheZC4abQ-qvifmxxbCT8gGy2O1dz-76gx3_ALVmeZQ==', b'gAAAAABg5EBD1k2kpmYaAI0PNfjHondg5mB9RwxoIxVqAkfZcAeYrK-H4oqyYjV9WcHFN0CF7Xf0dN2rAXKrkAq-myyfNUplSg==', b'gAAAAABg5EBDJrUQazdiAdf4yejxIwzZHSln-hAcYLkh27-7HO5gE8XjHZjUz3Y2-lHoDIINSAzUt10J93eZ2EeCNUGA1mVrgw==', b'gAAAAABg5EBDp5znqWT87IMASAjDuhMtB7uMV2rezOEQzy_O4ka8R8Dn0fCkZhajVBIQ3S0-MQiM2Mq0qS8JbKCD_rZhbaFPcA==', b'gAAAAABg5EBDZ3YdqhKFRZWQeKP3VUvaVWp-pmSub2aZtQGIpirZ6Pi9zplxdqCindAOHJIQrlcjAr_FG_vnH8U89_bx7FjX2Q==', b'gAAAAABg5EBDjIcwfpyMYXxoEK9XZjJEDyFM3sWfpM61hgAIOf4cNTj1etS1nCMsLIZtKIrF5wSRkmzP_tiXyDolisVkjr6uuA=='), ('gAAAAABg5EDjNQV_tNHuRRkZvdLDfnI20iGZ0Cpzv--1Xct0brLjCOlqnt0ZOU0AYe-tbQ390yIF2B-xlDGuP1-isAtgkWtGTQ==', 'gAAAAABg5EDjC84Zs7HPm1zPe9O2sWNX_HSf3pZeyivmqaPC-VYu5qely818-srsNOtVYtNoGCineThtqQuEC7vjteYV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AAAAABg5EQjeHuoLttV3wRj9DgIVseVvdMmYmWX3-D-UIckrjrizKM6N6OSpq98rPkk9DCutqgW9Jup2KD4bshhCtr8ipO1-g=='), ('gAAAAABg5EVhxeWhA7-oBZPT93nSe3p6vvxqcysxOjdyGyVNQdBwdtM2heHrhNnNSes8yang0Cx9vgBIsU7Yjrfdgz8tOWpK_A==', 'gAAAAABg5EVhdtXop2FNWsZSndOxQ3iEuJQyfI1flL0swLpcJKKTjBlEZhWpwoB3XTO2STYRVrimyEljZs751aVgveF0-rpEpA==', 'gAAAAABg5EVhyWQRkpFOd6Y-_OYqDJX5FVfpYXmd_2SKMZd-EPFYqOu5s47C7lw23zs4eHFVeEF0tSnSpr58rW6hWnxlOnjhnQ==', 'gAAAAABg5EVhCmgEjitk1cutyI5Nn5qYFsh0LRd3-uBJekPrZFoJb6S7hODtwNP-cQoV8R6u5eRLdUOeqffwCKIbBPYA9_lyTQ==', 'gAAAAABg5EVhKZggbtmupc2FqzfbSREmsws-MFYTEX-kGwfw-JJmBOh_7RJnTa-23HWJQtfChSWWuXzsxULJ5vI289c0BVIqrw==', 'gAAAAABg5EVhy8aowZUiuX6M2IPW35XFZNBueXDd_JFal39ViFmg6avpbkvs0mhVzpqVvXjNcMmr3QbNeXf3daLCDa7_KdCmmg==')]
This is the code where im trying to decode it:
def get_clients():
arr = []
retrieved_vals = ""
c.execute("SELECT * FROM client")
arr = c.fetchall()
i = 0
for i in arr[i]:
print(arr[i].decode())
What can i do?

Arr is a tuple within a list with encoded strings.
so your for needs to look like this: for i in arr[0]: print(i.decode())

Related

error while receiving data and converting it into f64

I am trying to send data over a TCP connection from rust to python, however while receiving the data in python I am getting the following error when trying to convert it from bytes to f64.
Traceback (most recent call last):
File "server.py", line 36, in <module>
[x] = struct.unpack('f', data)
struct.error: unpack requires a buffer of 4 bytes
I am using the follwoing method to convert the data from bytes to f64,
[x] = struct.unpack('f', data)
print(x)
my data looks like this, which I am sending over a tcp
x: 0.011399809271097183 (f64 from rust)
and getting something like
b'?t=*\x00\x00\x00\x00?s\xbd\xc6\x80\x00\x00\x00?q\xd5s\x00\x00\x00\x00?|\xae\x85\x80\x00\x00\x00?e\xb5\xc3\x00\x00\x00\x00?yp;\x80\x00\x00\x00?p\x7f\x98\x00\x00\x00\x00?hG|\x00\x00\x00\x00?o\x8d&\x00\x00\x00\x00?cv[\x00\x00\x00\x00?s\xdf\x97\x80\x00\x00\x00?{\x0e\xde\x80\x00\x00\x00?n\xec\xbf\x00\x00\x00\x00?n\xd8E\x00\x00\x00\x00?y+\xdd\x80\x00\x00\x00?r\xd90\x80\x00\x00\x00?r\xc2\x89\x00\x00\x00\x00?q\xc2i\x00\x00\x00\x00?kq"\x00\x00\x00\x00?t5\xec\x80\x00\x00\x00?|\xaak\x80\x00\x00\x00?z\x10\x9d\x00\x00\x00\x00?o\xeb\xde\x00\x00\x00\x01?m6\xfc\x00\x00\x00\x00'
It appears 24 8-byte floats (double type in C) were sent in big-endian format. At least the data appears as something reasonable, although none of them match the single value listed in the question:
>>> data = b'?t=*\x00\x00\x00\x00?s\xbd\xc6\x80\x00\x00\x00?q\xd5s\x00\x00\x00\x00?|\xae\x85\x80\x00\x00\x00?e\xb5\xc3\x00\x00\x00\x00?yp;\x80\x00\x00\x00?p\x7f\x98\x00\x00\x00\x00?hG|\x00\x00\x00\x00?o\x8d&\x00\x00\x00\x00?cv[\x00\x00\x00\x00?s\xdf\x97\x80\x00\x00\x00?{\x0e\xde\x80\x00\x00\x00?n\xec\xbf\x00\x00\x00\x00?n\xd8E\x00\x00\x00\x00?y+\xdd\x80\x00\x00\x00?r\xd90\x80\x00\x00\x00?r\xc2\x89\x00\x00\x00\x00?q\xc2i\x00\x00\x00\x00?kq"\x00\x00\x00\x00?t5\xec\x80\x00\x00\x00?|\xaak\x80\x00\x00\x00?z\x10\x9d\x00\x00\x00\x00?o\xeb\xde\x00\x00\x00\x01?m6\xfc\x00\x00\x00\x00'
>>> s
b'?t=*\x00\x00\x00\x00?s\xbd\xc6\x80\x00\x00\x00?q\xd5s\x00\x00\x00\x00?|\xae\x85\x80\x00\x00\x00?e\xb5\xc3\x00\x00\x00\x00?yp;\x80\x00\x00\x00?p\x7f\x98\x00\x00\x00\x00?hG|\x00\x00\x00\x00?o\x8d&\x00\x00\x00\x00?cv[\x00\x00\x00\x00?s\xdf\x97\x80\x00\x00\x00?{\x0e\xde\x80\x00\x00\x00?n\xec\xbf\x00\x00\x00\x00?n\xd8E\x00\x00\x00\x00?y+\xdd\x80\x00\x00\x00?r\xd90\x80\x00\x00\x00?r\xc2\x89\x00\x00\x00\x00?q\xc2i\x00\x00\x00\x00?kq"\x00\x00\x00\x00?t5\xec\x80\x00\x00\x00?|\xaak\x80\x00\x00\x00?z\x10\x9d\x00\x00\x00\x00?o\xeb\xde\x00\x00\x00\x01?m6\xfc\x00\x00\x00\x00'
>>> len(data)
192
>>> len(s)/8
24.0
>>> import struct
>>> struct.unpack('>24d',s)
(0.004941143095493317, 0.004819655790925026, 0.004353951662778854, 0.007002374157309532, 0.0026501473039388657, 0.0062105488032102585, 0.00402793288230896, 0.0029637739062309265, 0.0038514845073223114, 0.0023757722228765488, 0.004851905629038811, 0.006605977192521095, 0.0037749987095594406, 0.003765234723687172, 0.006145348772406578, 0.004601659253239632, 0.004580054432153702, 0.004335794597864151, 0.003349844366312027, 0.0049342382699251175, 0.006998462602496147, 0.0063634999096393585, 0.003896649926900864, 0.003566257655620575)

How do i convert a modified list of The Alphabet into a number?

i want to convert the sentence from variable (salam) into numbers. The conversion table is like a modified alphabet just like in (char2).
My expected output is a 3x3 matrix, inside is the converted number from(salam) using (char2)
salam = "APAKABARBROOOOO"
salam = salam.lower
output = []
char2 = [' ','a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z','.',',']
i = 0
while i <= 15:
np.array(char2.index(salam[i]))
i = i+1
and the output is
Traceback (most recent call last):
File "C:\Users\dragg\testing funtction losaot[sn[ga\main.py", line 12, in <module>
np.array(char2.index(salam[i]))
TypeError: 'builtin_function_or_method' object is not subscriptable
here is the image for clarity
The problem is from salam.lower. It should be salam.lower().
Without the () you are just referencing the .lower object.

Issue appending info from networkx dict to a numpy array

Trying to take strings from my networkx dict and append them to a certain place within an array:
def sameSports(node1, node2, G):
A = [G.node[node1]['sport1'], G.node[node1]['sport2'], G.node[node1]['sport3']]
B = [G.node[node2]['sport1'], G.node[node2]['sport2'], G.node[node2]['sport3']]
sharedSports = np.intersect1d(A,B)
sharedSports = np.delete(sharedSports, np.where(sharedSports == 'N/A'))
return sharedSports
def meetingDay(sharedSports,rightAxis):
meetingDays = [[] for i in range (len(sharedSports))]
columns=['Monday','Tuesday','Wednesday','Thursday','Friday']
for i in range (0,5):
for t in sharedSports:
n=np.where(sharedSports==t)
meetingDays[n].append(t)
if t in rightAxis[i]:
meetingDays[n].append(columns[i])
return meetingDays
The G.node[n]['sport'] dicts return single letter strings 'A'-'W', and the rightAxis array is a 5-section array containing some of the same letters. Getting this error:
Traceback (most recent call last):
File ".\main.py", line 18, in
a,b=add_sport_to_edges(G , rightAxis)
File "C:\Users\rjtkr\OneDrive\Documents\Work\Summer 2020 Research\epidemic\Athletics Network\sportsnetwork.py", line 99, in add_sport_to_edges
temp = meetingDay(sharedSports, rightAxis)
File "C:\Users\rjtkr\OneDrive\Documents\Work\Summer 2020 Research\epidemic\Athletics Network\sportsnetwork.py", line 90, in meetingDay
meetingDays[n].append(t)
TypeError: list indices must be integers or slices, not tuple
Unsure if it's 'n' or 't' that it doesn't like, but fairly sure that neither is a tuple. Hopefully I included all relevant code. The other lines showing up in the error come after the attached code, and the error remains the same when they are removed.

a bytes-like object is required, not 'str' XTEA Algorithm

I have a big problem and I need your help.
I have to encrypt my data with the xtea algorithm. I'm a completely beginner in python so I don't know any further.
I put my two hashed passwords and some data together. Because the my xtea algorithm don't want to have any strings in it I encoded it. (Encode 'utf-8' doesnt work here)
datas = macpassword_hashed_versch + tea_password + txt_neu
data_endi = str.encode(datas)
normales_password = normales_password.encode('utf-8')
data_en = xtea_algo(data_endi, normales_password)
Here is my xtea:
def xtea_algo(message, p):
key = p
text = message * 8
x = new(key, mode=MODE_CFB, IV="12345678")
c = x.encrypt(text)
return c
After this it is giving me this error:
Traceback (most recent call last):
Steganographie.py", line 77, in
data_en = xtea_algo(data_endi, normales_password)
Steganographie.py", line 23, in xtea_algo
c = x.encrypt(text)
lib\site-packages\pep272_encryption__init__.py", line 188, in
encrypt
encrypted_iv = self.encrypt_block(self.key, self._status)
lib\site-packages\xtea__init__.py", line 230, in encrypt_block
struct.unpack(self.endian + "2L", block),
TypeError: a bytes-like object is required, not 'str'
I controlled the types in my giving parameters and every object type is "bytes"
:( Do you guys have any ideas?

XOR'ing bytes in a memory dump to check for existance of a key

Reading in a binary file and XORing bytes to find out if there is an AES 128 key present.
with open("FakeMemDump1.bin","rb") as memorydump:
mem = memorydump.read(memorybytes)
for i in mem:
w = mem[i]^mem[i+15]
x = mem[i+4]^mem[i+19]
y = mem[i+8]^mem[i+23]
z = mem[i+12]^mem[i+27]
if w==mem[i+16] and x==mem[i+20] and y==mem[i+24] and z==mem[i+28]:
keySched = mem[i-1:i+175]
print ("Key Schedule : "%keySched)
Traceback (most recent call last):
File "AES128Keyfind.py", line 8, in <module>
w = mem[i]^mem[i+15]
TypeError: string indices must be integers, not str
Above is the error I am receiving, is the byte being called passed in as a string even though the file is read as a binary file?

Categories