InCTF 2014 - Crypto 100

There are three files, one.txt, one.txt.enc and second.txt.enc. Challenge is to decrypt the second.txt.enc using the key. So we got a message + cipher, so we got a hint that operation done using XOR. So XORing (Message ^ Cipher) = Key.
#!/usr/bin/env python

import hashlib


"""
one.txt
This sentence is encrypted using XOR cipher.
"""

plain_text = open('one.txt','r').read().strip()


"""
one.txt.enc
LAcbGEUKHQEGDgsaHU8bGEUcFgwAEhUNHQtSHhYQFghSMyorWAwbGw0cCkE=
"""

cipher_text = open('one.txt.enc','r').read().decode("base64")

print plain_text
print '---------------------------------------------------------------------'

print cipher_text
print '---------------------------------------------------------------------'

plain_text = [ord(i) for i in plain_text]
cipher_text = [ord(i) for i in cipher_text]

key = ''
for i in range(len(plain_text)):
    c = ((plain_text[i] ^ cipher_text[i]))
    key += chr(c)

print key
print '---------------------------------------------------------------------'
print 'VERFICIATION'
key='xorkey'

key_text = [ord(i) for i in key]
msg = ''
check = len(key_text)
j=0
for i in range(len(cipher_text)):
    if check > j:
        c = ((key_text[j] ^ cipher_text[i]))
        msg+= chr(c)
        if j+1 == check:
            j=0
        else:
            j+=1

print '--------------------------------------------------'
print msg
print '--------------------------------------------------'

print '---------------------------------------------------------------------'
print 'CAPTURE THE FLAG'
key='xorkey'


"""
Second.txt.enc
LAcXSz02Kk8RAhURHR1SAhZZGU8EDhcAWBgXCg5ZGwcdAgYcWAkdGUUYWAwbGw0cCk8TBQFZCwcdHgkdWAEdH0UQFk8CGQQaDAYRDktZLAcXSwMVGQhSDQoLWBsaAhZZFAoEDglZERxSHw0cWAsXCBcACBsbBAtZEwoLSwwNCwoeDUs= 
"""

key_text = [ord(i) for i in key]
cipher_text = open('two.txt.enc','r').read().decode("base64")
cipher_text = [ord(i) for i in cipher_text]


flag = ''
check = len(key_text)
j=0
for i in range(len(cipher_text)):
    if check > j:
        c = ((key_text[j] ^ cipher_text[i]))
        flag+= chr(c)
        if j+1 == check:
            j=0
        else:
            j+=1

print '--------------------------------------------------'
print flag
print '--------------------------------------------------'
The output is

 The XOR cipher is a very weak choice for a cipher and should not in practice. The flag for this level is the decryption key itself.
Flag is {xorkey}

Comments

Popular posts from this blog

Python Speech recognition for Mac OS X

Baby Step Giant Step Algorithm Python Code

Simple Automation using Python - Atomac in Mac OS X