<?xml version="1.0"?>
<!--Copyright Brian Starkey 2013-->
<page title="Can You Find It?" dir="misc/gchq" filename="gchq2" type="windowpage" 
    root="/xml_95" date="23rd October 2013">
<window title="GCHQ: Can You Find It? (2)">
	<menu>
		<menu-item name="File">
			<!--<pdf-subitem name="Get PDF"/>-->
			<home-subitem name="Home" href="{/page/@root}"/>
			<menu-subitem name="XML" href="gchq2.xml"/>
		</menu-item>
		<!--include common menu items (dynamically generated)-->
        <autogen type='category' category='all'/>
	</menu>
	<substance>
	<locations name="{/page/@title}">
            <folder name="Challenge 1" href="./1"/>
            <folder name="Challenge 2" href="./2">
                <!--<leaf class="pdfleaf" name="proj.pdf" href="proj.pdf" />-->
                <!-- img="/images/pdf_icon.gif" -->
                <leaf class="sectionleaf" name="RSA Encryption" 
                    href="#RSA%20Encryption"/>
                <leaf class="sectionleaf" name="A Solution" 
                    href="#A%20Solution%20(Base64)"/>
            </folder>
            <folder name="Challenge 3" href="./3"/>
            <folder name="Challenge 4" href="./4"/>
            <folder name="Challenge 5" href="./5"/>
            <folder name="Files">
                <!--<leaf class="pdfleaf" name="proj.pdf" href="proj.pdf" />-->
                <!-- img="/images/pdf_icon.gif" -->
                <leaf class="codeleaf" name="challenge1.c" 
                    href="files/challenge1.c"/>
                <leaf class="otherleaf" name="comp1.key" 
                    href="files/comp1.key"/>
                <leaf class="codeleaf" name="challenge2" 
                    href="files/challenge2"/>
                <leaf class="codeleaf" name="challenge3" 
                    href="files/challenge3"/>
                <leaf class="codeleaf" name="rsa.py" 
                        href="files/rsa.py"/>
                <leaf class="codeleaf" name="challenge4" 
                    href="files/challenge4"/>
                <leaf class="imgleaf" name="comp3.jpg" 
                    href="files/comp3.jpg"/>
            </folder>
	</locations>
	<window-contents>
        <row>
	    <section-title name="Challenge 2"/>
            <image style="float: none; width: 80%; max-width: 650px;
                margin-left: auto; margin-right: auto;"
                src="images/challenge2.png">The second challenge</image>
            <section-content>
            <p>
            This is what you get for challenge 2 - a directory listing
            containing a single file. The file is an 
            <a href="http://en.wikipedia.org/wiki/RSA_(algorithm)">RSA</a> 
            private key (I guess not that private any more):
            <pre>
-----BEGIN RSA PRIVATE KEY-----
MIIC2gIBAAKBgDfABK8+joDLdbFTDJ+y3PTTzkqCi1L2qEjgxdg1iyZshJTeKUck
SYVyKBeOBtB3FwwqXVa6iNEHJeLFewFE6ulEOIcatVp11Zg0ibMfnqTivbd6t8/z
3KzqrFksg9xQiicMactmTqFkm8ro5ODc2NTQzMjEwLy4tLCslBOCOVHxAgMBAAEC
gYATW12FB2BtQbecmSxh6rWjYENZRZhgdvoZS8oF9xlYfwdNtRF5/RR1/BwFia++
BAuBktgTu/KzORsjcNPzrd0uTCbTG6hW8YPK2ROVOOeAMHek8Nl3+SW5wdePKuWw
MdjDDjqxXDns+ZC1d2Cpz5V+x+2znOYL0bsEKei0sWl7LQKBgDfABK8+joDLdbFT
DJ+y3PTTzkqCi1L2qEjgxdg1iyZshJTeKUckSYVyKBeOBtB3FwwqXVa6iNEHJeLF
ewFE6uhVSior5HGPArFhsOQ0v9ob1NCV7P8M99qN4XplmX/xs05HgQCVh9aMWtio
pKCcmJSQjIiEgHx4dHBsU9JB+TvkAkB3dy53aHRzaXNpbGd1b2VjdHNyZWhzcmku
ZW9jdS4va2xidGVoY2VsIHkgICAgICAgICAgICAgICAgICAgIAuPAkATpSSd/C5S
IEAbUPk+ZYAdt7OYVzay7ViAiaukhkt+/sJG+m8GmHnAKyLf9ohx3/aIcd/2iHHf
9ohx3/ayirJPAkAIefJYEpdAoRjJQCHPGUpOVjLiyQMyPcnsutG+ctAGGU8lZTDU
yUim9V7iwqTE4sKkxOLCpMTiwqTE4sKkxOFNAoGAFInzTsAOkauW3crd1XfxMhxi
tUkapdQqlwvFhZuouNIybfEOfW6WkjtghBDyqf50cEFWXMJ7Vk8mr6cwTosPvYKU
VXKUCblretLTeU95TlbkprizPky++5b7pQuSi3mpLMi+6VgvcjTthfXPYNg2JjJp
gmteC4felYL/2FTAmT8=
-----END RSA PRIVATE KEY-----
            </pre>
            Naively I tried <a href="http://rsa.metro.co.uk">
                rsa.metro.co.uk</a> but of course it wouldn't be that
            easy!
            </p>
            <sub-title name="RSA Encryption"/>
            <p>
            I can't pretend to be even knowledgable about encryption,
            let alone an expert. Everything I know about RSA I learnt
            trying to crack these codes. Basically it relies on the fact
            that factorising <b>very</b> big numbers is rather  slow to do
            on current computers.
            </p>
            <p>
            The first thing I tried was to decrypt the ciphertext from challenge
            1 using this key:<br/>
            </p>
            <p>
            <pre style="word-wrap: break-word;">
$ openssl rsautl -decrypt -in cipher1 -out plaintext -inkey comp1.key 
RSA operation error
139938077894464:error:0406506C:rsa routines:RSA_EAY_PRIVATE_DECRYPT:data greater than mod len:rsa_eay.c:584:
            </pre>
            This made absolutely no sense to me, so I did some research. I also
            tried generating a public key from the private key, but that didn't 
            work either. Can you tell I have no idea what I'm doing?
            </p>
            <p>
            It turns out that data encrypted by RSA ends up as a chunk of data of
            a specfic length - and the length must be no greater than the length
            of the "modulo" parameter of the RSA key. I encrypted some random
            strings to see what happened:<br/>
            </p>
            <p>
            <a name="128B"/>
            <pre style="word-wrap: break-word;">
$ openssl rsautl -encrypt -inkey comp1.key > encrypted.txt
hello[^D]
$ wc -c encrypted.txt 
128 encrypted.txt
            </pre>
            The <icode>wc</icode> command gives you a <b>w</b>ord <b>c</b>ount,
            though in this case I've asked it to tell me how many bytes are in
            the encrypted file - 128. This means that our <b>encrypted data should
            be 128 bytes long</b>. Remember this for later!
            </p>
            <p>
            I noticed something fishy when I tried to decrypt the test files I
            had encrypted - <icode>openssl</icode> was throwing a wobbly. 
            A little googling 
            indicated that maybe the key was bad so I decided the RSA was a 
            red herring and started looking at the key file itself.
            </p>
            <sub-title name="A Solution (Base64)"/>
            <p>
            The key file is encoded as 
            <a href="http://en.wikipedia.org/wiki/Base64">Base64</a>
            - a way to encode binary data as a
            printable string, so that you can stick it in an email or something
            similar. This is standard for RSA keys, but the "=" sign at the end
            is a dead giveaway.
            </p>
            <p>
                There's a linux tool for decoding base64, handily called 
                <icode>base64</icode>. If you strip the header and footer off the
                RSA key and pass it to <icode>base64</icode> to decode, you get
                a bunch of binary garbage (which kills your terminal, btw), 
                but you'll also spot some human readable (ish)
                text at the end. I ran the output through <icode>hexdump</icode>
                and <icode>grep</icode>ped out
                readable things. Here's some commands to do it:<br/>
            <pre style="word-wrap: break-word;">
$ sed '1d' comp1.key | sed '$d' > tmp.b64
$ base64 -d tmp.b64 > tmp.out
$ hexdump -C tmp.out | grep -B3 -A3 --color=always --binary-file=text -E "([a-z]){3}"
            </pre>
            Which gives some output which looks a little like this:
            <image style="float: none; width: 80%; max-width: 650px;
                margin-left: auto; margin-right: auto;"
                src="images/challenge2_hex.png">Almost readable output</image>
            </p>
            <p>
            The bit that should catch your eye here is (mostly) in red. Notice 
            "ww.w" at the start? Kinda looks like a web address. If we correct for 
            <a href="http://en.wikipedia.org/wiki/Endianness">Endianness</a>
            we get a lovely little URL:
            <a href="http://www.thisisgloucestershire.co.uk/bletchley">
                www.thisisgloucestershire.co.uk/bletchley</a>. Yet again, a
            Turing vibe.
            </p>
            <p>
            So there we have it, second challenge solved, second answer found
            (<b>bletchley</b>) and third clue located. 
            On to <a href="./3">Challenge 3</a>!
            <image style="float: none; width: 80%; max-width: 650px;
                margin-left: auto; margin-right: auto;"
                src="images/2down.png"/>
            </p>
            </section-content>
        </row>
    <comments/>

	</window-contents>
	</substance>
</window>

</page>


