{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cryptographie TD 1 - Exercice 4\n",
    "\n",
    "On recopie les deux listes ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "u=['99', 'f0', '11', '31', '3f', 'f6', '9d', '52']\n",
    "v=['89', 'fa', '1f', '34', '38', 'eb', '8c', '59']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On convertit les chaînes hexadécimales en entiers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "u = [int(x,16) for x in u]\n",
    "v = [int(x,16) for x in v]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Et on calcule le XOR des deux cryptogrammes, qui est aussi celui des deux textes clairs :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "xx = [u[i]^v[i] for i in range(len(u))]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On récupère la liste de mots anglais de 8 lettres en minuscules :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('/usr/share/dict/words','r') as dico:\n",
    "    dd = [w.rstrip().lower() for w in dico]\n",
    "dd8=[list(map(ord,w)) for w in dd if len(w)==8]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Et on va calculer le XOR de chacun d'eux avec `xx`. Si le résultat est dans le dictionnaire, on a probablement trouvé un des mots."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def f(w):\n",
    "    return [w[i]^xx[i] for i in range(8)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "res = [w for w in dd8 if f(w) in dd8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "computer\n",
      "security\n"
     ]
    }
   ],
   "source": [
    "print (len(res))\n",
    "print (''.join(map(chr,res[0])))\n",
    "print (''.join(map(chr,res[1])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
