Skip to content

Instantly share code, notes, and snippets.

@chakpongchung
Last active October 10, 2015 20:44
Show Gist options
  • Save chakpongchung/88d880bb69b676ec8a07 to your computer and use it in GitHub Desktop.
Save chakpongchung/88d880bb69b676ec8a07 to your computer and use it in GitHub Desktop.
Assignment 2 of Network Security ,taught by Dr.Hussein Abdel-Wahab
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This code implements the first round of IDEA.\n",
"\n",
"This python implementation follows the code notation of\n",
"1.\n",
"Dr.Wahab's lecture\n",
"and\n",
"2.\n",
"[Rhee_Y] Internet_Security_Cryptographic_Principle\n",
"[Bruce Schneier] Applied Cryptography\n",
"Protocols, Algorithms, and Source Code in C\n",
"\n",
"Personally I think Bruce Schneier's method in his book # Chapter 13.9 \n",
" simplifies the idea so that I can reuse the result computed before without violating anything in Dr.Wahab's notation.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 565,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0001000000000001\n",
"00010000000000010001000111100111\n"
]
}
],
"source": [
"# test input and key from fall 2014\n",
"# The input data block is: 0001 0001 0000 FFFF &\n",
"# The encryption key is: FFFF 0000 0000 0001 0001 0001 0001 0001\n",
"# input='0001 0001 0000 FFFF'.replace(' ','')\n",
"# key='FFFF 0000 0000 0001 0001 0001 0001 0001'.replace(' ','')\n",
"# Answer: 0002 FFFD FFFD 0003\n",
"\n",
"# test input and key from fall 2015\n",
"input='0001 0000 0000 0001'.replace(' ','')\n",
"key='0001 0000 0000 0001 0001 0001 1110 0111'.replace(' ','')\n",
"print input\n",
"print key\n"
]
},
{
"cell_type": "code",
"execution_count": 566,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['0001', '0000', '0000', '0001', '0001', '0001']\n",
"16\n",
"['0000000000000001', '0000000000000000', '0000000000000000', '0000000000000001', '0000000000000001', '0000000000000001']\n"
]
}
],
"source": [
"# # key\n",
"# tokenize the key into 6 groups,each 4 hex digits\n",
"z=[]\n",
"for i in range(6):\n",
" z.append(key[4*i:4*i+4])\n",
"print z\n",
"# convert the 4 hex digits to 16 bit binary string\n",
"for i in range(6):\n",
" z[i]=\"{0:016b}\".format(int(z[i], 16))\n",
"# z[i]=bin(int(z[i], 16))\n",
"print len(z[0])\n",
"print z\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 567,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['0001', '0000', '0000', '0001']\n"
]
}
],
"source": [
"# plain text\n",
"x=[]\n",
"x.append(input[0:4])\n",
"x.append(input[4:8])\n",
"x.append(input[8:12])\n",
"x.append(input[12:16])\n",
"print x\n"
]
},
{
"cell_type": "code",
"execution_count": 568,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['0000000000000001', '0000000000000000', '0000000000000000', '0000000000000001']\n",
"['0000000000000001', '0000000000000000', '0000000000000000', '0000000000000001', '0000000000000001', '0000000000000001']\n"
]
}
],
"source": [
"# hex to binary\n",
"\n",
"for i in range(len(x)):\n",
"# x[i]=\"{0:016b}\".format(int(x[i], 16))\n",
"# hex to int, \n",
"# then int to binary\n",
"# bstr=\"{0:016b}\".format(int(x[i], 16))\n",
"# x[i]=bin(int(bstr, 2))\n",
" x[i]=\"{0:016b}\".format(int(x[i], 16))\n",
"print x\n",
"print z"
]
},
{
"cell_type": "code",
"execution_count": 569,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0000000000000001\n",
"0000000000000001\n",
"\n",
"the final answer given the input \n",
"0001000000000001\n",
"and key \n",
"00010000000000010001000111100111\n",
" is: \n",
"0x3 0x2 0x3 0x2\n"
]
}
],
"source": [
"print z[0]\n",
"print x[0]\n",
"print \n",
"# Bruce Schneier's method in his book Chapter 13.9 \n",
"r1=( int(x[0],2)*int(z[0],2) )%(2**16+1)\n",
"r2=( int(x[1],2)+int(z[1],2) )%(2**16)\n",
"r3=( int(x[2],2)+int(z[2],2) )%(2**16)\n",
"r4=( int(x[3],2)*int(z[3],2) )%(2**16+1)\n",
"r5=r1^r3\n",
"r6=r2^r4\n",
"r7=(r5* int(z[4],2))%(2**16+1)\n",
"r8= (r6+r7)%(2**16)\n",
"r9=r8*int(z[5],2)%(2**16+1)\n",
"r10= (r7+r9)%(2**16)\n",
"r11= r1^r9\n",
"r12= r3^r9\n",
"r13= r2^r10\n",
"r14= r4^r10\n",
"print 'the final answer given the input '\n",
"print input\n",
"print 'and key '\n",
"print key\n",
"print ' is: '\n",
"# print r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14\n",
"# Answer for Fall2014: 0002 FFFD FFFD 0003\n",
"#below is the answer for the given input\n",
"# choose the input to be the one for FALL2014,I have the same answer\n",
"# So I believe my answer is correct for FALL2015 input\n",
"print hex(r11),hex(r12),hex(r13),hex(r14)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment