Last active
December 20, 2020 22:03
-
-
Save pavelpetrcz/58bca38328536d2b86d71af6cdf9a6db to your computer and use it in GitHub Desktop.
singWithRsa.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Generovaní podpisu\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"https://www.pycryptodome.org/en/latest/src/signature/pkcs1_v1_5.html?highlight=sign#pkcs-1-v1-5-rsa" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from Cryptodome.PublicKey import RSA\n", | |
"from Cryptodome.Hash import SHA512\n", | |
"from Cryptodome.Signature import pkcs1_15\n", | |
"\n", | |
"import json\n", | |
"import base64" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Vstupy" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"načtu si to co chci podepsat - r+b - read as a binary file" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Ja bych ty internety zakazala.\n" | |
] | |
} | |
], | |
"source": [ | |
"message = \"Ja bych ty internety zakazala.\"\n", | |
"public_key = open('sign_public.pem').read()\n", | |
"private_key = open('sign_private.pem').read()\n", | |
"\n", | |
"print(message)\n", | |
"#print(private_key)\n", | |
"#print(public_key)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Výpočet" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"spočtu SHA512 zprávy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"h = SHA512.new(message)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"importuju klíč" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"key = RSA.importKey(private_key, \"aaaa\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"podepíšu hash zprávy ve schématu PKCS#1 v1.5 (RSA) - https://tools.ietf.org/html/rfc8017\n", | |
"a vyleze mi podpis v bytes" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"b'1\\x19a%\\xa5\\xde\\xa4\\xbfp\\xb8\\xc53\\xd6$\\x96@U\\xfd\\x9e\\xc9\\xab\\x89\\xe9\\xe0\\xf7P\\xa2\\xdc\\xab@\\x0f\\xd7\\xaf\\x91\\xca~&\\xc9\\x04Z\\x99B\\xd1\\xd8\\r\\xfe\\x9e\\xbb\\xdb\\xa8\\xa2\\xad\\x99}T\\xf8\\xe1\\\\\\xacu\\xb7\\xed\\xda\\xe8\\xcf \\xbc{\\xf4v\\xffF\\xf7\\x93\\x1b2\\xdbP\\x1a\\xfbNX\\xfe\\xd0\\xda\\xa1\\xd8\\xf4\\xa5\\x0en\\x89\\x98{\\x9a\\xee\\x07c\\x9b\\xdaX*\\xe5\\xee\\xbd\\xbd{\\xb8O9\\x0f\\xf5\\xaf\\xd8\\xf0\\xc7va\\x1c\\x1cb\\xa6\\x84\\x7f\\x99\\xd2i\\xe1\\xa4\\x9f\\xf2u\\xf2ML:\\xf3v\\xd5wS\\xc1\\xa3\\xd9@\\xa0[\\x1dzr\\xf9\\xce\\xb8d\\xed\\xfdQ\\xda\\xdfp\\x07\\x85\\x8d\\x9a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4\\x9b\\x95\\xd1\\x7f]\\x87\\xe9\\x1a\\xb0l'\n" | |
] | |
} | |
], | |
"source": [ | |
"sig = pkcs1_15.new(key).sign(h)\n", | |
"print(sig)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'31 19 61 25 a5 de a4 bf 70 b8 c5 33 d6 24 96 40 55 fd 9e c9 ab 89 e9 e0 f7 50 a2 dc ab 40 0f d7 af 91 ca 7e 26 c9 04 5a 99 42 d1 d8 0d fe 9e bb db a8 a2 ad 99 7d 54 f8 e1 5c ac 75 b7 ed da e8 cf 20 bc 7b f4 76 ff 46 f7 93 1b 32 db 50 1a fb 4e 58 fe d0 da a1 d8 f4 a5 0e 6e 89 98 7b 9a ee 07 63 9b da 58 2a e5 ee bd bd 7b b8 4f 39 0f f5 af d8 f0 c7 76 61 1c 1c 62 a6 84 7f 99 d2 69 e1 a4 9f f2 75 f2 4d 4c 3a f3 76 d5 77 53 c1 a3 d9 40 a0 5b 1d 7a 72 f9 ce b8 64 ed fd 51 da df 70 07 85 8d 9a d6 45 75 6d bb 25 1b 98 e4 35 09 e1 9d d8 de 2a 87 d9 b4 76 70 ff 22 08 af 7c 72 06 57 58 90 46 b2 6c df 94 90 d5 3d 25 7a 4c 09 46 f1 41 f5 b8 3d 2e 22 cb 3d ff 41 50 d6 a9 f1 50 72 cb 60 80 25 90 8d e9 6b 22 dd b6 e5 37 a9 79 02 81 17 2b f6 9a 41 d5 1b f5 17 29 29 64 0b 65 2c 67 a8 d5 18 60 80 2f ee ff f4 b6 b7 80 99 d2 dc 20 5b 2d 0f 74 67 9b 31 8a c0 25 b3 08 cc da 35 20 7e 9f 76 2a b1 c0 2b 63 65 9e 4a f7 4a 73 b9 90 a6 19 c6 6b 56 51 66 3a d8 65 db c4 3b 24 b2 bc 8f 3e 40 6c c4 1e 21 18 13 61 d8 92 c6 0f 52 76 a4 e4 7a bc 16 65 51 d5 aa 0d e5 03 89 27 5d 57 46 fe 8f 08 5a 4c 3f 22 51 04 86 82 7e 84 89 31 22 d3 91 e2 41 85 9c d7 2b 01 29 60 48 8a 6a c4 62 95 9d 9a d4 d7 44 5a 4b e2 b3 b7 af 2d 84 08 49 9b eb 1d 2a 69 d0 9f d2 7b 25 b7 88 46 21 c8 f1 7b c1 5c 86 46 23 8b d1 bc 85 6c 30 c6 a7 56 6f 4e 20 c6 2a c7 2c 24 f9 35 75 1e 84 25 0e bd d6 0f e1 84 ea cf 90 ec 65 f1 6a 1e 6c 42 30 99 71 fc 4d 8b 9c 48 46 49 90 f2 22 1a 59 66 83 b5 b6 77 8a 57 4f ff 3d 64 5d a1 09 ff 42 29 09 b1 49 57 2b c4 9b 95 d1 7f 5d 87 e9 1a b0 6c'" | |
] | |
}, | |
"execution_count": 30, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sig.hex(\" \")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"b'MRlhJaXepL9wuMUz1iSWQFX9nsmrieng91Ci3KtAD9evkcp+JskEWplC0dgN/p6726iirZl9VPjhXKx1t+3a6M8gvHv0dv9G95MbMttQGvtOWP7Q2qHY9KUObomYe5ruB2Ob2lgq5e69vXu4TzkP9a/Y8Md2YRwcYqaEf5nSaeGkn/J18k1MOvN21XdTwaPZQKBbHXpy+c64ZO39UdrfcAeFjZrWRXVtuyUbmOQ1CeGd2N4qh9m0dnD/IgivfHIGV1iQRrJs35SQ1T0lekwJRvFB9bg9LiLLPf9BUNap8VByy2CAJZCN6Wsi3bblN6l5AoEXK/aaQdUb9RcpKWQLZSxnqNUYYIAv7v/0treAmdLcIFstD3RnmzGKwCWzCMzaNSB+n3YqscArY2WeSvdKc7mQphnGa1ZRZjrYZdvEOySyvI8+QGzEHiEYE2HYksYPUnak5Hq8FmVR1aoN5QOJJ11XRv6PCFpMPyJRBIaCfoSJMSLTkeJBhZzXKwEpYEiKasRilZ2a1NdEWkvis7evLYQISZvrHSpp0J/SeyW3iEYhyPF7wVyGRiOL0byFbDDGp1ZvTiDGKscsJPk1dR6EJQ691g/hhOrPkOxl8WoebEIwmXH8TYucSEZJkPIiGllmg7W2d4pXT/89ZF2hCf9CKQmxSVcrxJuV0X9dh+kasGw='\n" | |
] | |
} | |
], | |
"source": [ | |
"sigB64 = base64.b64encode(sig)\n", | |
"print(sigB64)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Verifikování podpisu" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"načtu veřejný klíč\n", | |
"spočtu hash \n", | |
"a porovnám" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The signature is valid.\n" | |
] | |
} | |
], | |
"source": [ | |
"key = RSA.import_key(public_key)\n", | |
"h = SHA512.new(message)\n", | |
"try:\n", | |
" pkcs1_15.new(key).verify(h, sig)\n", | |
" print(\"The signature is valid.\")\n", | |
"except (ValueError, TypeError):\n", | |
" print (\"The signature is not valid.\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"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.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment