0% found this document useful (0 votes)
238 views21 pages

Cryptography 1

This document outlines 12 cryptography challenges that involve running Python scripts and entering values. The challenges cover topics like RSA encryption, hash collisions, and AES encryption. Solutions involve writing Python code to decrypt, find collisions, or compute responses using cryptographic algorithms and values provided in each challenge.

Uploaded by

jjoseph3703
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views21 pages

Cryptography 1

This document outlines 12 cryptography challenges that involve running Python scripts and entering values. The challenges cover topics like RSA encryption, hash collisions, and AES encryption. Solutions involve writing Python code to decrypt, find collisions, or compute responses using cryptographic algorithms and values provided in each challenge.

Uploaded by

jjoseph3703
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CSE 365 - Cryptography

Challenge 1)
1. Run the challenge using

/challenge/run

2. Open a python file and run the following file with your information in the quotes

import base64
print(base64.b64decode(""))

python <filename>.py

Challenge 2)
1. Run the challenge using

/challenge/run

2. Run the python script with your values

import base64

key = base64.b64decode("")
ct = base64.b64decode("")

for i in range(len(key)):
print(chr(ct[i] ^ key[i]), end="")

bytes([a ^ b for a, b in zip(key, ct)])

CSE 365 - Cryptography 1


python <filename>.py

Challenge 3)
1. Run the challenge using

/challenge/run

2. Copy the secret ciphertext value into the plaintext prompt

3. Press ctrl + z

4. Use the following command and in the quotes place the value you got in the
previous step

echo -n "" | base64 --decode

Challenge 4)
1. Run the challenge using

CSE 365 - Cryptography 2


/challenge/run

2. Use the following python script to solve this challenge in the quotes place the 2
values that the challenge gives you

from Crypto.Cipher import AES


import base64
key = base64.b64decode(b"")
cipher = AES.new(key, AES.MODE_ECB)
ct = base64.b64decode("")
plaintext = cipher.decrypt(ct)
print(plaintext)

python <filename>.py

Challenge 5)
1. DO NOT RUN THE CHALLENGE

2. Run the following python script to solve this challenge

import pwn
import base64
chall= pwn.process("/challenge/run")
chall.readuntil(b"secret ciphertext (b64): ")
secret_ciphertext= chall.readline().decode()
ciphertext_decoded=base64.b64decode(secret_ciphertext)

def findNextChar(currFlag, numA):


chall.sendline(base64.b64encode(b"A"*numA))
chall.readuntil("ciphertext (b64): ")
new_line=chall.readline().decode()
new_encryption=base64.b64decode(new_line)

for i in range(32,128):
chall.sendline(base64.b64encode(b"A"*numA+ bytes(currFlag, 'ascii')+ bytes([i])))
chall.readuntil("ciphertext (b64): ")
line=chall.readline().decode()
encryption=base64.b64decode(line)
if(new_encryption[:64] == encryption[:64]
return chr(i)

CSE 365 - Cryptography 3


flag = ""

for i in range(63, 7, -1):


flag = flag + findNextChar(flag, i)

print(flag)

python <filename>.py

Challenge 6)
1. Run the challenge using

/challenge/run

2. Use the value for p and copy it into the B prompt

3. Copy the secret cipher text it outputs into the following python script and run it

import base64
print(base64.b64decode(""))

python <filename>.py

CSE 365 - Cryptography 4


Challenge 7)
1. Run the challenge using

/challenge/run

2. Use the following python script and enter the values from the challenge into the
corresponding variables

CSE 365 - Cryptography 5


import base64

def decrypt_rsa(ciphertext, d, n):


decrypted = pow(ciphertext, d, n)
return decrypted.to_bytes((decrypted.bit_length() + 7) // 8, byteorder="little")

e_hex = '0x10001'
d_hex = '' #put your value for d here
n_hex = '' #put your value for n here

e = int(e_hex, 16)
d = int(d_hex, 16)
n = int(n_hex, 16)

ciphertext = "" #put your value for cipher here

ciphertext_bytes = base64.b64decode(ciphertext)
ciphertext_int = int.from_bytes(ciphertext_bytes, byteorder="little")
plaintext_bytes = decrypt_rsa(ciphertext_int, d, n)

print(plaintext_bytes.decode('utf-8'))

Challenge 8)
1. Run the challenge using

/challenge/run

CSE 365 - Cryptography 6


2. Similar to the last challenge use the values your challenge provides you to enter
into the corresponding variables

import base64
from Crypto.PublicKey import RSA
from Crypto.Util.number import inverse

def decrypt_rsa(ciphertext, d, n):


decrypted = pow(ciphertext, d, n)
return decrypted.to_bytes((decrypted.bit_length() + 7) // 8, byteorder="little")

e = int('0x10001', 16)
p = int('', 16) #put your value for p here
q = int('', 16) #put your value for q here

phi = (p - 1) * (q - 1)
d = inverse(e, phi)
n = p * q

ciphertext = "" #put your value for ciphertext here


ciphertext_bytes = base64.b64decode(ciphertext)
ciphertext_int = int.from_bytes(ciphertext_bytes, byteorder="little")
plaintext_bytes = decrypt_rsa(ciphertext_int, d, n)

print(plaintext_bytes.decode('utf-8'))

python <filename>.py

CSE 365 - Cryptography 7


Challenge 9)
1. Run the challenge using

/challenge/run

2. Run the python script with the prefix your challenge provides you

import hashlib
import os
import base64
from Crypto.Hash import SHA256 as SHA256Hash

def find_collision(secret_hash_prefix):
while True:
data = os.urandom(32)
hash = hashlib.sha256(data).digest()
if hash[:2] == secret_hash_prefix:
return data

secret_sha256_prefix_b64 = "" #enter yours here


secret_sha256_prefix = base64.b64decode(secret_sha256_prefix_b64)

collision = find_collision(secret_sha256_prefix)
collision_b64 = base64.b64encode(collision).decode('utf-8')

print("Collision (b64):", collision_b64)

3. Run and enter the result of the python script into the prompt in the challenge in a
second terminal using the split icon (looks like 2 boxes) in the terminal area

CSE 365 - Cryptography 8


Challenge 10)
1. Run the challenge using

/challenge/run

2. Use the following python script and enter your values into the corresponding
variables

import base64
import hashlib
import itertools
import string

def find_response(challenge, difficulty):


for length in itertools.count(1):
for chars in itertools.product(string.ascii_letters + string.digits, repeat=lengt
h):
response = ''.join(chars)
hashed = hashlib.sha256(challenge + response.encode()).digest()
if hashed[:difficulty] == b'\0' * difficulty:
return response

challenge_b64 = "" #insert yours here


challenge = base64.b64decode(challenge_b64)
difficulty = 2

CSE 365 - Cryptography 9


response = find_response(challenge, difficulty)
response_b64 = base64.b64encode(response.encode()).decode()
print(f"Response (b64): {response_b64}")

3. Run the python script and enter it’s output into the prompt from the challenge

python <filename>.py

Challenge 11)
1. Run the challenge using

/challenge/run

2. Use the following script and enter your values into the corresponding variables

def compute_response(d, n, challenge):


response = pow(challenge, d, n)
return response
e = '0x10001'
d_hex = '' #put your value here
n_hex = '' #put your value here
challenge_hex = '' #put your value here
d = int(d_hex, 16)

CSE 365 - Cryptography 10


n = int(n_hex, 16)
challenge = int(challenge_hex, 16)

response = compute_response(d, n, challenge)


print(f"response: {response:x}")

3. Run the python script using the following command and enter the response into the
challenge’s prompt

python <filename>.py

Challenge 12)
1. Run the challenge using

/challenge/run

2. We will run multiple ipython commands and some bash commands in this challenge

3. The first ipython commands are as follows it will error as it is supposed to

Run ipython using

CSE 365 - Cryptography 11


ipython

Each of the following should be entered one by one

from Crypto.PublicKey import RSA


key = RSA.generate(512, e=0x0001)
key = generate(513, e=0x10001)

4. When that script errors we will run the following command in a split terminal

cp <filepath_from_error> <name_of_python_script_you_just_ran>.py

5. At the end of the file you just copied to append the following lines (you might have
to close your file and reopen it for the changes to take effect)

key = generate(513, e=0x10001)


import IPython; IPython.embed()

This is what you should have so far

CSE 365 - Cryptography 12


6. Delete the following lines from your python file it is on line ~457

if bits < 1024:


raise ValueError("RSA modulus length must be >= 1024")

7. Run the following commands in ipython

Run ipython using

ipython <filename_where_you_copied_file>.py

Again each of these should be run individually and you should put the output into the
corresponding prompt in the challenge

import base64
hex(key.e)
hex(key.n)
hex(pow(,key.d,key.n))
binascii.unhexlify("0"+hex(pow(int(binascii.hexlify(base64.b64decode("")[::-1]), 16),key.
d,key.n))[2:])[::-1]

CSE 365 - Cryptography 13


Challenge 13)
1. Run the challenge using

/challenge/run

2. In a split terminal run ipython with the file you created in challenge 12

key
cert = {"name": "fish", "key": {"e": key.e, "n":key.n}, "signer": "root"}
import json
import base64
import hashlib
base64.b64encode(json.dumps(cert).encode("ascii"))
d = #enter your root d value from the challenge here

3. Create another split terminal so now you will have 3 from left to right they should be
the challenge, ipython <filename>.py, and now this new one. In this new one you
will run ipython by itself then the following commands

ipython
import base64
base64.b64decode("") #insert your your root certificate (b64)

CSE 365 - Cryptography 14


4. Return to the second terminal and run the following commands

n = #enter the result from the 3rd terminal


base64.b64encode(pow(int.from_bytes(hashlib.sha256(json.dumps(cert).encode("ascii")).diges
t(), "little"),d,n).to_bytes(256,"little"))
pow(int.from_bytes(base64.b64decode(""),"little"),key.d,key.n).to_bytes(256,"little")

5. Here are some screenshots of each step

CSE 365 - Cryptography 15


Challenge 14)
1. Run the following python script

import os

import sys

import base64

import json

import pwn

from Crypto.PublicKey import RSA

from Crypto.Hash.SHA256 import SHA256Hash

from Crypto.Cipher import AES

from Crypto.Util.Padding import pad, unpad

CSE 365 - Cryptography 16


pwn.context.update(encoding="latin")

process = pwn.process("/challenge/run")

## diffie-hellman related values ##

process.readuntil("p: ")

p = int(process.readline(), 16)

process.readuntil("g: ")

g = int(process.readline(), 16)

## root certificate related values ##

process.readuntil("root key d: ")

root_private_key = int(process.readline(), 16)

process.readuntil("root certificate (b64): ")

root_certificate_binary = base64.b64decode(process.readline())

root_certificate = json.loads(root_certificate_binary)

process.readuntil("root certificate signature (b64): ")

root_certificate_signature = base64.b64decode(process.readline())

## user cerficiate related values ##

CSE 365 - Cryptography 17


process.readuntil("name: ")

# remove last letter which is \n

name = process.readline().decode().replace('\n', '')

## diffie-hellman related values ##

process.readuntil("A: ")

A = int(process.readline(), 16)

b = int.from_bytes(os.urandom(256), 'little')

B = pow(g, b, p)

process.readuntil("B: ")

process.writeline(hex(B).encode())

s = pow(A, b, p)

## AES-related values (new in level 14) ##

aes_key = SHA256Hash(s.to_bytes(256, "little")).digest()[:16]

cipher_decrypt = AES.new(key=aes_key, mode=AES.MODE_CBC, iv=b"\0"*16)

cipher_encrypt = AES.new(key=aes_key, mode=AES.MODE_CBC, iv=b"\0"*16)

CSE 365 - Cryptography 18


## root certificate related values ##

key = RSA.generate(1024)

user_certificate = {

"name": name,

"key": {

'e': key.e,

'n': key.n

},

"signer": "root"

user_certificate_binary = json.dumps(user_certificate).encode()

user_certificate_encrypted = cipher_encrypt.encrypt(pad(user_certificate_binary, cipher_en


crypt.block_size))

user_certificate_encrypted_b64 = base64.b64encode(user_certificate_encrypted)

user_certificate_hash_binary = SHA256Hash(user_certificate_binary).digest()

# signature must be signed by root

user_certificate_signature_binary = pow(

int.from_bytes(user_certificate_hash_binary, "little"),

root_private_key,

root_certificate['key']['n']

).to_bytes(256, "little")

CSE 365 - Cryptography 19


user_certificate_signature_encrypted = cipher_encrypt.encrypt(pad(user_certificate_signatu
re_binary, cipher_encrypt.block_size))

user_certificate_signature_encrypted_b64 = base64.b64encode(user_certificate_signature_enc
rypted)

process.readuntil("user certificate (b64): ")

process.writeline(user_certificate_encrypted_b64)

process.readuntil("user certificate signature (b64): ")

process.writeline(user_certificate_signature_encrypted_b64)

## random data signed by user certificate ##

user_signature_binary = (

name.encode().ljust(256, b"\0") +

A.to_bytes(256, "little") +

B.to_bytes(256, "little")

user_signature_hash_binary = SHA256Hash(user_signature_binary).digest()

# signature must be signed by user

user_signature_binary = pow(

int.from_bytes(user_signature_hash_binary, "little"),

key.d,

user_certificate['key']['n']

).to_bytes(256, "little")

CSE 365 - Cryptography 20


user_signature_encrypted = cipher_encrypt.encrypt(pad(user_signature_binary, cipher_encryp
t.block_size))

user_signature_encrypted_b64 = base64.b64encode(user_signature_encrypted)

process.readuntil("user signature (b64): ")

process.writeline(user_signature_encrypted_b64)

process.readuntil("secret ciphertext (b64): ")

flag_cipher_text = base64.b64decode(process.readline())

flag = cipher_decrypt.decrypt(flag_cipher_text)

flag = unpad(flag, cipher_decrypt.block_size)

print(flag)

CSE 365 - Cryptography 21

You might also like