Skip to content

Instantly share code, notes, and snippets.

@chakpongchung
Created October 8, 2015 20:22
Show Gist options
  • Save chakpongchung/fb020e39d8a93519dec9 to your computer and use it in GitHub Desktop.
Save chakpongchung/fb020e39d8a93519dec9 to your computer and use it in GitHub Desktop.
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": "code",
"execution_count": 484,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"000100010000FFFF\n",
"FFFF0000000000010001000100010001\n"
]
}
],
"source": [
"# input='7fa9 1c37 ffb3 df05'.replace(' ','')\n",
"# key='5a14 fb3e 021c 79e0 6081 46a0 117b ff03'.replace(' ','')\n",
"# 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",
"# Answer: 0002 FFFD FFFD 0003\n",
"input='0001 0001 0000 FFFF'.replace(' ','')\n",
"key='FFFF 0000 0000 0001 0001 0001 0001 0001'.replace(' ','')\n",
"\n",
"\n",
"print input\n",
"print key\n"
]
},
{
"cell_type": "code",
"execution_count": 485,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['FFFF', '0000', '0000', '0001', '0001', '0001']\n",
"16\n",
"['1111111111111111', '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": 486,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['0001', '0001', '0000', 'FFFF']\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": 487,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['0000000000000001', '0000000000000001', '0000000000000000', '1111111111111111']\n",
"['1111111111111111', '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\n",
"\n",
"# print \"{0:0416b}\".format(int(x[0], 2))+\"{0:016b}\".format(int(x[1], 2))\n",
"\n",
"# xa= \"{0:016b}\".format(int('7fa9', 16))\n",
"# print xa\n"
]
},
{
"cell_type": "code",
"execution_count": 488,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1111111111111111\n",
"0000000000000001\n",
"\n",
"\n",
"65535 1 0 65535 65535 65534 65535 65533 65533 65532 2 65533 65533 3\n",
"0x2 0xfffd 0xfffd 0x3\n",
"16\n"
]
}
],
"source": [
"print z[0]\n",
"print x[0]\n",
"print \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 \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)\n",
"\n",
"print len('1111111111111101')"
]
},
{
"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