From 3b8774ada9c9de709a2a96185f20d121c3458d97 Mon Sep 17 00:00:00 2001 From: hardword Date: Fri, 2 Oct 2020 02:36:56 -0700 Subject: [PATCH 1/2] Simple RSA learning notebook - Created using Colaboratory --- RSA_simple.ipynb | 292 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 RSA_simple.ipynb diff --git a/RSA_simple.ipynb b/RSA_simple.ipynb new file mode 100644 index 00000000..f3ed64f4 --- /dev/null +++ b/RSA_simple.ipynb @@ -0,0 +1,292 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "RSA_simple.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python2", + "display_name": "Python 2" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "GlSiAIHM5UaN" + }, + "source": [ + "## RSAaaay (From TAMUctf 2019)\n", + "\n", + "Hey, you're a hacker, right? I think I am too, look at what I made!\n", + "\n", + "---\n", + "```\n", + "(2531257, 43)\n", + "\n", + "My super secret message: \n", + "906851 991083 1780304 2380434 438490 356019 921472 822283 817856 556932 2102538 2501908 2211404 991083 1562919 38268\n", + "```\n", + "---\n", + "\n", + "Problem is, I don't remember how to decrypt it... could you help me out?\n", + "\n", + "*Difficulty: easy*\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "imRBoEE408F-" + }, + "source": [ + "Finding p,q: http://factordb.com/index.php?query=2531257 (Change query param with your N)\n", + "\n", + "Reference: https://github.com/p4-team/ctf/tree/master/2017-02-25-bkp/rsa_buffet" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "T3Zpi9mpJj8H" + }, + "source": [ + "# From https://gist.github.com/JonCooperWorks/5314103\n", + "\n", + "def multiplicative_inverse(e, phi):\n", + " d = 0\n", + " x1 = 0\n", + " x2 = 1\n", + " y1 = 1\n", + " temp_phi = phi\n", + "\n", + " while e > 0:\n", + " temp1 = temp_phi/e\n", + " temp2 = temp_phi - temp1 * e\n", + " temp_phi = e\n", + " e = temp2\n", + "\n", + " x = x2- temp1* x1\n", + " y = d - temp1 * y1\n", + " \n", + " x2 = x1\n", + " x1 = x\n", + " d = y1\n", + " y1 = y\n", + " \n", + " if temp_phi == 1:\n", + " return d + phi\n", + " " + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ktq4TYOZJpwu", + "outputId": "f07f47a3-7b40-4aa8-94f9-9b67dbc7f74c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "p=509\n", + "q=4973\n", + "e=43\n", + "phi=(p-1)*(q-1)\n", + "n=p*q\n", + "d=multiplicative_inverse(e, phi)\n", + "\n", + "print d" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2584515\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "sfDZWjXTJtDz" + }, + "source": [ + "enc='906851 991083 1780304 2380434 438490 356019 921472 822283 817856 556932 2102538 2501908 2211404 991083 1562919 38268'\n", + "enc_list=enc.split()\n", + "dec=[]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "jTfOjnR-Jwrp" + }, + "source": [ + "for _ in enc_list:\n", + "\tmsg = int(_)**d % n\n", + "\tdec.append(msg)\n", + " " + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "zW6gGGQMOInt", + "outputId": "29fc14f3-fbe7-468b-9667-7b784b618724", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "print dec" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[103L, 105103L, 101109L, 12383L, 97118L, 97103L, 10195L, 83105L, 12095L, 70108L, 121105L, 110103L, 9584L, 105103L, 101114L, 115125L]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "S4SgwGUH7TTJ", + "outputId": "215442df-d6e7-4451-c703-b8cb3dff5543", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "dec_new=[103, 105, 103, 101, 109, 123, 83, 97, 118, 97, 103, 101, 95, 83, 105, 120, 95, 70, 108, 121, 105, 110, 103, 95, 84, 105, 103, 101, 114, 115, 125]\n", + "\n", + "dec_str=''\n", + "\n", + "for _ in dec_new:\n", + " dec_str += chr(_)\n", + " \n", + "print dec_str" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "gigem{Savage_Six_Flying_Tigers}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_d-E9Viu2mAY" + }, + "source": [ + "http://www.oxfordmathcenter.com/drupal7/node/206\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "wRLBGdNJVpD-" + }, + "source": [ + "def multiplicative_inverse(e, phi):\n", + " d = 0\n", + " x1 = 0\n", + " x2 = 1\n", + " y1 = 1\n", + " temp_phi = phi\n", + "\n", + " while e > 0:\n", + " temp1 = temp_phi/e\n", + " temp2 = temp_phi - temp1 * e\n", + " temp_phi = e\n", + " e = temp2\n", + "\n", + " x = x2- temp1* x1\n", + " y = d - temp1 * y1\n", + " \n", + " x2 = x1\n", + " x1 = x\n", + " d = y1\n", + " y1 = y\n", + " \n", + " if temp_phi == 1:\n", + " return d + phi" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "nasUAOZXVgpZ", + "outputId": "08f249d5-3e9e-4355-cb24-57065cbd44ba", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "p=45000107\n", + "q=48000089\n", + "e=17\n", + "phi=(p-1)*(q-1)\n", + "n=p*q\n", + "\n", + "print multiplicative_inverse(e, phi) - phi\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "635296778826273\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zzdNpry0gHR3" + }, + "source": [ + "VIRSECCON CTF 2020 - Classic\n", + "\n", + "```\n", + "n = 77627938360345301510724699969247652387657633828943576274039402978346703944383\n", + "\n", + "e = 65537\n", + "\n", + "c = 62899945974090753231979111677615029855602721049941681356856158761811378918268\n", + "```\n", + "\n" + ] + } + ] +} \ No newline at end of file From 877f11b47a9df8495e199b5152f6b4c6614b05e9 Mon Sep 17 00:00:00 2001 From: Howard Shin Date: Fri, 2 Oct 2020 02:39:41 -0700 Subject: [PATCH 2/2] move a file to the right place --- RSA_simple.ipynb => Cryptography/src/RSA/RSA_simple.ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename RSA_simple.ipynb => Cryptography/src/RSA/RSA_simple.ipynb (100%) diff --git a/RSA_simple.ipynb b/Cryptography/src/RSA/RSA_simple.ipynb similarity index 100% rename from RSA_simple.ipynb rename to Cryptography/src/RSA/RSA_simple.ipynb