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.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Reply
#3
Thanks for the help i really am grateful
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 600 Sep-18-2020, 10:10 PM
Last Post: tienttt
  Problem with using Crypto library reks2004 2 452 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 15,229 Feb-08-2020, 06:58 PM
Last Post: karkas
  Create file archive that contains crypto hash ED209 1 518 May-29-2019, 03:05 AM
Last Post: heiner55
  charmap codec can't decode byte error with gzipped file in python bluethundr 2 1,024 Apr-30-2019, 12:26 PM
Last Post: bluethundr
  Read Byte Array from Zigbee coordinator using python jenkins43 3 984 Mar-26-2019, 03:03 PM
Last Post: micseydel
  crypto import issue saisankalpj 2 3,807 Dec-20-2018, 06:08 AM
Last Post: saisankalpj
  AES encryption - does not match between arduino and python 3 crypto guillaume55 0 1,935 Sep-23-2018, 11:14 AM
Last Post: guillaume55
  4 byte hex byte swap from binary file medievil 7 11,686 May-08-2018, 08:16 AM
Last Post: killerrex
  python charmap codec can't decode byte X in position Y character maps to < undefined> owais 9 27,559 Apr-28-2018, 10:52 PM
Last Post: abadawi

Forum Jump:

User Panel Messages

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