Your IP : 216.73.216.40


Current Path : /var/www/html/venkat/
Upload File :
Current File : /var/www/html/venkat/rsa_attack.ipynb

{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "rsa-attack.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_a2v4MMrsNq-",
        "outputId": "1bc17d8a-56e2-44e4-ee57-6f01f017390b"
      },
      "source": [
        "#https://www.johndcook.com/blog/2019/03/06/rsa-exponent-3/\n",
        "from secrets import randbits, randbelow\n",
        "from sympy import nextprime\n",
        "from sympy.ntheory.modular import crt\n",
        "    \n",
        "def modulus():\n",
        "   p = nextprime(randbits(6))\n",
        "   q = nextprime(randbits(6))\n",
        "   print(\"P,Q =\",p,q)\n",
        "   return p*q\n",
        "    \n",
        "N = [modulus() for _ in range(3)]\n",
        "print(\"N =\", N)\n",
        "m = randbelow(min(N))\n",
        "print(\"m =\",m)\n",
        "c = [pow(m, 3, N[i]) for i in range(3)]\n",
        "x = crt(N, c)[0]\n",
        "print(\"c =\",c)    \n",
        "print(\"x =\",x)\n",
        "print(\"m =\",x**(1./3.))"
      ],
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "P,Q = 53 31\n",
            "P,Q = 29 47\n",
            "P,Q = 23 3\n",
            "N = [1643, 1363, 69]\n",
            "m = 21\n",
            "c = [1046, 1083, 15]\n",
            "x = 9261\n",
            "m = 20.999999999999996\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "J1OmTmMQsU4a",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "f923697a-1592-4af7-ad69-9c11324ecc26"
      },
      "source": [
        "#https://www.di-mgt.com.au/rsa_factorize_n.html\n",
        "import math\n",
        "N = 25777 \n",
        "e =3\n",
        "d=16971 \n",
        "\n",
        "k = (d * e) -1\n",
        "\n",
        "for g in range (2,N-1):\n",
        "  t = k\n",
        "  print(\"g------------\", g)\n",
        "  d = 0;\n",
        "  while (t % 2 == 0):\n",
        "    t = t/2 \n",
        "    print(\"t = \",t)\n",
        "    t = int(t)\n",
        "    x = pow(g,t) % N\n",
        "    print(\"x = \",x)\n",
        "    if(x > 1):\n",
        "      y = math.gcd(x-1,N)\n",
        "      if(y > 1):\n",
        "        print(\"p=\",y)\n",
        "        print(\"y=\",N/y)\n",
        "        print(\"True\")\n",
        "        d = 1;\n",
        "        break     \n",
        "      else:\n",
        "        d = 0;\n",
        "        print(\"False\")\n",
        "  if(d==1):\n",
        "      break\n",
        "  print(\"\\n\")\n"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "g------------ 2\n",
            "t =  25456.0\n",
            "x =  1\n",
            "t =  12728.0\n",
            "x =  1\n",
            "t =  6364.0\n",
            "x =  1\n",
            "t =  3182.0\n",
            "x =  25776\n",
            "False\n",
            "t =  1591.0\n",
            "x =  12709\n",
            "False\n",
            "\n",
            "\n",
            "g------------ 3\n",
            "t =  25456.0\n",
            "x =  1\n",
            "t =  12728.0\n",
            "x =  1\n",
            "t =  6364.0\n",
            "x =  1\n",
            "t =  3182.0\n",
            "x =  25776\n",
            "False\n",
            "t =  1591.0\n",
            "x =  13068\n",
            "False\n",
            "\n",
            "\n",
            "g------------ 4\n",
            "t =  25456.0\n",
            "x =  1\n",
            "t =  12728.0\n",
            "x =  1\n",
            "t =  6364.0\n",
            "x =  1\n",
            "t =  3182.0\n",
            "x =  1\n",
            "t =  1591.0\n",
            "x =  25776\n",
            "False\n",
            "\n",
            "\n",
            "g------------ 5\n",
            "t =  25456.0\n",
            "x =  1\n",
            "t =  12728.0\n",
            "x =  1\n",
            "t =  6364.0\n",
            "x =  1\n",
            "t =  3182.0\n",
            "x =  15050\n",
            "p= 149\n",
            "y= 173.0\n",
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "UpRyYh_9r3Sd",
        "outputId": "52a718a6-149a-4815-816d-77b032d1018c"
      },
      "source": [
        "#hashing from geeksforgeeks\n",
        "import hashlib\n",
        "  \n",
        "# initializing string\n",
        "str = \"GeeksforGeeks\"\n",
        "  \n",
        "result = hashlib.sha256(str.encode())\n",
        "  \n",
        "print(\"The hexadecimal equivalent of SHA256 is : \")\n",
        "print(result.hexdigest())"
      ],
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "The hexadecimal equivalent of SHA256 is : \n",
            "f6071725e7ddeb434fb6b32b8ec4a2b14dd7db0d785347b2fb48f9975126178f\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RoD6lR1tsdha",
        "outputId": "043b1ac3-091c-48b6-aa17-ddea6f0d4b3c"
      },
      "source": [
        "!pip install PyCrypto\n",
        "#import cartopy\n",
        "from Crypto.PublicKey import RSA\n",
        "from Crypto.Cipher import PKCS1_OAEP\n",
        "from Crypto.Signature import PKCS1_v1_5\n",
        "from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5\n",
        "from Crypto import Random\n",
        "from base64 import b64encode, b64decode\n",
        "hash = \"SHA-256\"\n",
        "\n",
        "def newkeys(keysize):\n",
        "   random_generator = Random.new().read\n",
        "   key = RSA.generate(keysize, random_generator)\n",
        "   private, public = key, key.publickey()\n",
        "   return public, private\n",
        "\n",
        "def importKey(externKey):\n",
        "   return RSA.importKey(externKey)\n",
        "\n",
        "def getpublickey(priv_key):\n",
        "   return priv_key.publickey()\n",
        "\n",
        "def decrypt(ciphertext, priv_key):\n",
        "   cipher = PKCS1_OAEP.new(priv_key)\n",
        "   return cipher.decrypt(ciphertext)\n",
        "\n",
        "def encrypt(message, pub_key):\n",
        "   cipher = PKCS1_OAEP.new(pub_key)\n",
        "   return cipher.encrypt(message)\n",
        "\n",
        "public, private = newkeys(1024)\n",
        "print(\"publickey = \", public)\n",
        "print(\"publickey  e= \",private.e)\n",
        "print(\"privatekey d= \",private.d)\n",
        "print(\"N = \", private.n)\n",
        "c = encrypt(b\"hello\",public)\n",
        "print(\"cipher text = \", c)\n",
        "p = decrypt(c,private)\n",
        "print(\"plaintext = \",p)\n",
        "\n"
      ],
      "execution_count": 39,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: PyCrypto in /usr/local/lib/python3.7/dist-packages (2.6.1)\n",
            "publickey =  <_RSAobj @0x7fd042111f50 n(1024),e>\n",
            "publickey  e=  65537\n",
            "privatekey d=  58177119264142305610042127146837107576819333012193647656535716996944363732356343334378431879978582248844883931022489304127875653017269037040735970776368060244452953536175019865694105876207655266671741820912213081795309663119140529607414070763914976280026992314637845563712885411003341940657911574321278069473\n",
            "N =  104933366319364091998495414526551906405999962229809690008707342364902787007773157371822190453231219068186243516923651617576127333749628756860842529703339323249812567797468631158875646153770860481986776046212356637545697706772991399145313898136185193439929683926816582951494228042677550852934831577814540337181\n",
            "cipher text =  b\"?\\x1d\\xc2V\\xbfw3\\xb7Y\\x86W\\x84\\xb5\\x92\\xf3\\xa4\\xbeC\\x10\\xb7D\\xcfG\\xe9\\xa9-\\xb2b\\x10\\xeb\\x04;\\x82\\x1dr\\x15\\x83M1\\xe2\\xe2\\x0e\\x05\\r\\xdaTr\\xff\\x166p\\x98\\x89\\xac\\rP\\xb4\\xae\\xa2\\x9f$\\x0e\\x06$X\\xba5\\x05\\xb4\\x0b h\\x1b\\\\\\xafr\\xd4\\xb8$\\x83\\xdf\\x0b\\x19\\xc0 yd\\n\\xedR\\xc7n\\xaf~\\xdf\\x96\\x82\\x82\\xd9cR\\x1c\\xe1\\xef-\\xc9\\xc1y#\\xc5qp>\\xf75-\\x84B2\\xff\\x0c:'\\xb5\\x1d\\xd3\\xf9\\x0e\"\n",
            "plaintext =  b'hello'\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}