Single-Byte XOR Cipher (python)

2024/10/18 15:06:20

This is for a modern cryptography class that I am currently taking.

The challenge is the cryptopals challenge 3: Single-Byte XOR Cipher, and I am trying to use python 3 to help complete this.

I know that I am supposed to XOR the string and converted to English. The hex string is "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736" which converts to "806748453371902409051174291875458592743800337585421566549206796642836053682239286" in decimal form.

I have XOR'd this against multiple hex byte combinations (2 hex digits), but I do not know how to convert this into English. Is it just brute force and educated guessing at this point?

I know about ETAOIN SHRDLU, but this hasn't really been that helpful.

Thank you for your time and help.


ADDED: Additionally, I tried Challenge #4 but this code does not seem to work. But it did work for Challenge #3 so I am confused.

Challenge #3 Challenge #4

Answer

You can use binascii.hexlify, binascii.unhexlify to convert byte strings to hexadecimals or vice versa:

>>> import binascii
>>> binascii.hexlify(b'HELLO')  # to Hex
b'48454c4c4f'
>>> binascii.unhexlify('48454c4c4f')  # from Hex
b'HELLO'

Using str.isprintable, you can filter out non-printable candidates:

>>> 'abcd'.isprintable()
True
>>> '\x00'.isprintable()
False
>>> '\x7f'.isprintable()
False

import binasciiencoded = binascii.unhexlify('1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736')
for xor_key in range(256):decoded = ''.join(chr(b ^ xor_key) for b in encoded)if decoded.isprintable():print(xor_key, decoded)
https://en.xdnf.cn/q/73102.html

Related Q&A

Basemap Heat error / empty map

I am trying to plot a scattered heat map on a defined geo location. I can very well plot a normal scattered map with no background but I want to combine it with a given lat and lon. I get the following…

Keras custom loss function per tensor group

I am writing a custom loss function that requires calculating ratios of predicted values per group. As a simplified example, here is what my Data and model code looks like: def main():df = pd.DataFrame…

How does numpy.linalg.inv calculate the inverse of an orthogonal matrix?

Im implementing a LinearTransformation class, which inherits from numpy.matrix and uses numpy.matrix.I to calculate the inverse of the transformation matrix.Does anyone know whether numpy checks for or…

pandas: Using color in a scatter plot

I have a pandas dataframe:-------------------------------------- | field_0 | field_1 | field_2 | -------------------------------------- | 0 | 1.5 | 2.9 | -------------------…

Framing Errors in Celery 3.0.1

I recently upgraded to Celery 3.0.1 from 2.3.0 and all the tasks run fine. Unfortunately. Im getting a "Framing Error" exception pretty frequently. Im also running supervisor to restart the t…

decorator() got an unexpected keyword argument

I have this error on Django view:TypeError at /web/host/1/ decorator() got an unexpected keyword argument host_id Request Method: GET Request URL: http://127.0.0.1:8000/web/host/1/edit Django Versio…

Conflict between sys.stdin and input() - EOFError: EOF when reading a line

I cant get the following script to work without throwing an EOFError exception:#!/usr/bin/env python3import json import sys# usage: # echo [{"testname": "testval"}] | python3 test.p…

Requests - inability to handle two cookies with same name, different domain

I am writing a Python 2.7 script using Requests to automate access to a website that sets two cookies with the same name, but different domains, E.g. Name mycookie, Domain www.example.com and subdomain…

Python logging from multiple processes

I have a possibly long running program that currently has 4 processes, but could be configured to have more. I have researched logging from multiple processes using pythons logging and am using the So…

Error while fetching Tweets with Tweepy

I have a Python script that fetch tweets. In the script i use the libary Tweepy . I use a valid authentication parameters. After running this script some tweets are stored in my MongoDB and some are r…