Python Forum
Python crypto byte plaintext to hexint
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python crypto byte plaintext to hexint
#1
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"

This is a 32 byte plaintext from a file where each byte are encoded and represents 2 hexadecimals and i want to convert it to hexadecimal integers like this: 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31

i have been struggling for a while with this and given hits about using functions: hex(), open(), file.read(), string.decode(), map(), ord()

This is how for i gotten and would appreciate any help

key = open('testKey')
getKey = key.read()

print(getKey)
integerKey = int(getKey,16)
hexKey = hex(integerKey)
for i in hexKey:
    print(ord(i), end=" , ")
# test = map(integerKey, hexKey)
print(getKey)
print(integerKey)
# print(list(test))
Reply
#2
I believe you're overcomplicating things a bit:
with open("testKey") as f:
    while chars := f.read(2):
        print(int(chars, 16))
I use a context manager (you can Google this term if you're unfamiliar with it) for the file management, which is a recommended best-practice. I use the "walrus operator" (Googleable as well). And lastly, I use the fact that f.read(2 will return an empty string, which will terminate the while loop if the file is empty.
Reply
#3
Thanks for the help i really am grateful
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a TG crypto Bot_ problem when trying to with bot.polling p1ner0 1 1,383 Apr-27-2022, 03:43 AM
Last Post: p1ner0
  how to manage crypto trading flooding data from exchange servers Mikeardy 0 1,209 Dec-26-2021, 08:31 PM
Last Post: Mikeardy
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 11,344 Sep-18-2020, 10:10 PM
Last Post: tienttt
  Problem with using Crypto library reks2004 2 2,371 Mar-27-2020, 05:38 AM
Last Post: buran
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 31,469 Feb-08-2020, 06:58 PM
Last Post: karkas
  Create file archive that contains crypto hash ED209 1 2,006 May-29-2019, 03:05 AM
Last Post: heiner55
  charmap codec can't decode byte error with gzipped file in python bluethundr 2 3,665 Apr-30-2019, 12:26 PM
Last Post: bluethundr
  Read Byte Array from Zigbee coordinator using python jenkins43 3 3,815 Mar-26-2019, 03:03 PM
Last Post: micseydel
  crypto import issue saisankalpj 2 7,781 Dec-20-2018, 06:08 AM
Last Post: saisankalpj
  AES encryption - does not match between arduino and python 3 crypto guillaume55 0 3,989 Sep-23-2018, 11:14 AM
Last Post: guillaume55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020