mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-23 17:38:39 +00:00
159 lines
3.9 KiB
Plaintext
159 lines
3.9 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "1057a613",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<style>.container{width:100%}</style>\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"%%HTML\n",
|
||
|
"<style>.container{width:100%}</style>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "040e1454",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Key generation for DES for 16 rounds"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"id": "7bd02a30",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"['1B02EFFC7072',\n",
|
||
|
" '79AED9DBC9E5',\n",
|
||
|
" '55FC8A42CF99',\n",
|
||
|
" '72ADD6DB351D',\n",
|
||
|
" '7CEC07EB53A8',\n",
|
||
|
" '63A53E507B2F',\n",
|
||
|
" 'EC84B7F618BC',\n",
|
||
|
" 'F78A3AC13BFB',\n",
|
||
|
" 'E0DBEBEDE781',\n",
|
||
|
" 'B1F347BA464F',\n",
|
||
|
" '215FD3DED386',\n",
|
||
|
" '7571F59467E9',\n",
|
||
|
" '97C5D1FABA41',\n",
|
||
|
" '5F43B7F2E73A',\n",
|
||
|
" 'BF918D3D3F0A',\n",
|
||
|
" 'CB3D8B0E17F5']"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import random\n",
|
||
|
"def r64():\n",
|
||
|
" r64 = ''\n",
|
||
|
" for i in range(8):\n",
|
||
|
" r = str(bin(random.randint(0,2**8)))[2:]\n",
|
||
|
" if(len(r)<8):\n",
|
||
|
" r = '0'*(8-len(r)) + r\n",
|
||
|
" r64 +=r\n",
|
||
|
" return r64\n",
|
||
|
"def hex2bin(hexa):\n",
|
||
|
" binstr = bin(int(hexa,16))[2:]\n",
|
||
|
" binstr = binstr.zfill(len(hexa)*4)\n",
|
||
|
" return binstr\n",
|
||
|
"\n",
|
||
|
"def bin2hex(binary):\n",
|
||
|
" binary = binary.zfill(len(binary) +( 4-len(binary)%4)%4)\n",
|
||
|
" hexa = hex(int(binary,2))[2:].upper()\n",
|
||
|
" return hexa\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"def key_gen(bit64):\n",
|
||
|
" bit64 = '#'+bit64\n",
|
||
|
" PC_1 = [57,49,41,33, 25,17,9,1,\n",
|
||
|
" 58,50,42,34, 26,18,10, \n",
|
||
|
" 2,59,51,43, 35,27,19,11,\n",
|
||
|
" 3,60,52,44, 36,63,55,47,\n",
|
||
|
" 39,31,23,15, 7,62,54,46,\n",
|
||
|
" 38,30,22,14, 6,61,53,45,\n",
|
||
|
" 37,29,21,13, 5,28,20,12,4]\n",
|
||
|
" \n",
|
||
|
" PC_2 = [14,17,11,24, 1,5,3,28,\n",
|
||
|
" 15,6,21,10, 23,19,12,4,\n",
|
||
|
" 26,8,16,7, 27,20,13,2, 41,52,31,37, 47,55,30,40,51,45,33,48, 44,49,39,56,34,53,46,42, 50,36,29,32]\n",
|
||
|
" bit56 = ''\n",
|
||
|
" for i in PC_1:\n",
|
||
|
" bit56 +=bit64[i]\n",
|
||
|
" L,R = bit56[:28],bit56[28:]\n",
|
||
|
" round_keys = []\n",
|
||
|
" ones = [1,2,9,16]\n",
|
||
|
" for i in range(1,17):\n",
|
||
|
" if(i in ones):\n",
|
||
|
" l = L[1:]+L[:1]\n",
|
||
|
" r = R[1:]+R[:1]\n",
|
||
|
" else:\n",
|
||
|
" l = L[2:]+L[:2]\n",
|
||
|
" r = R[2:]+R[:2]\n",
|
||
|
" k = '#'+l+r\n",
|
||
|
" sub_key = ''\n",
|
||
|
" for i in PC_2:\n",
|
||
|
" sub_key += k[i]\n",
|
||
|
" L,R = l,r\n",
|
||
|
"# print(len(k),len(sub_key))\n",
|
||
|
" round_keys.append(sub_key)\n",
|
||
|
" return round_keys\n",
|
||
|
"\n",
|
||
|
"test = \"133457799BBCDFF1\"\n",
|
||
|
"subkeys = key_gen(hex2bin(test))\n",
|
||
|
"[bin2hex(i) for i in subkeys]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "64b7d8da",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"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.12.3"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|