{"id":19797249,"url":"https://github.com/abund4nt/rsa-implementation","last_synced_at":"2025-05-01T03:31:35.349Z","repository":{"id":244158139,"uuid":"814323490","full_name":"abund4nt/RSA-Implementation","owner":"abund4nt","description":"RSA cryptosystem implementation in Python.","archived":false,"fork":false,"pushed_at":"2024-06-13T02:45:21.000Z","size":56,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T08:26:11.469Z","etag":null,"topics":["algorithm","cryptography","mathematics","python","rsa"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/abund4nt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-12T19:30:00.000Z","updated_at":"2024-07-29T23:27:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"b07e4b6c-dd58-4d87-99c8-19781385831b","html_url":"https://github.com/abund4nt/RSA-Implementation","commit_stats":null,"previous_names":["abund4nt/rsa-implementation"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abund4nt%2FRSA-Implementation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abund4nt%2FRSA-Implementation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abund4nt%2FRSA-Implementation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abund4nt%2FRSA-Implementation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abund4nt","download_url":"https://codeload.github.com/abund4nt/RSA-Implementation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251818226,"owners_count":21648858,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","cryptography","mathematics","python","rsa"],"created_at":"2024-11-12T07:24:28.167Z","updated_at":"2025-05-01T03:31:35.342Z","avatar_url":"https://github.com/abund4nt.png","language":"Python","readme":"# RSA cryptosystem implementation in Python\n\n\u003e [!WARNING]\n\u003e This RSA implementation could be insecure, the repository is created for the purpose of learning about asymmetric cryptography, do not use this tool for sensitive things.\n\nSimple script written in Python that implements the RSA cryptosystem. This tool allows us to encrypt/decrypt messages securely using RSA.\n\n## Functionalities of the tool\n\nWhen connecting to the server or using the tool locally, you will encounter the following interface\n\n``` shell\nWelcome to RSA Server\n\n\nSelect Option:\n(1) Generate keys (private and public)\n(2) Encrypt message\n(3) Decrypt message\n(4) Exit\n```\n\nThis tool has 4 options, the first one allows us to generate a public and private key to encrypt/decrypt messages. Our public key can be disclosed through an insecure channel for a third party to encrypt data with it. Our private key should not be given to a third party for any reason, as it will be used to decrypt our messages. (For more information read the following [article](https://es.wikipedia.org/wiki/Criptograf%C3%ADa_asim%C3%A9trica)). The second one allows us to encrypt a message using a public key (send it in base64). The third one allows us to decrypt a message using a private key and in the fourth option we exit the tool.\n\n![](https://i.imgur.com/bh740T4.png)\n\n## Example of encrypting and decrypting a message\n\nFirst of all we select option 1 and generate a public and private key, then we store both keys securely.\n\n``` shell\nYour private key:\n-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBAAKCAQEAu7L5kbU4NC+7RqQiVgMBfOx/w4cuniNLAsrZ9HPAACvHszSh\nxS+HTvypNL7j+PqDKV0olvAwyswG6+kaH4Rm031/pT1831XmjijFGO1t8B//GjIw\nVDguGvKB3ri1IA.....\n-----END RSA PRIVATE KEY-----\n\nIn base64: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLR....\n\nYour public key:\n-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu7L5kbU4NC+7RqQiVgMB\nf.....\n-----END PUBLIC KEY-----\n\nIn base64: LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3M...\n```\n\nWith both values saved, we can give our public key to a sender to encrypt a message with it. For this we select option 2.\n\n``` shell\nEnter the message to encrypt: Mi password is securepassword123\n\nEnter the public key that will encrypt the message (base64): LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0F....\n\nYour encrypted message is: 14746634659051772345090619160847739346107822025878135056179431840383544183296437967909270354886735311180345743247212097706211101979451086028894835680585248834437560595077643176436038963195768876269911488174549424136154017029571441970067895516962286416050993934884906175304494142739460796136689018735564315035027718985716888453925282184178223539865834082853819602735459279117003460750949613734854620605171680712533091450949344077259036144276006252259608802572121747059525003621833160828635385601464134106144846772874481867511654254510137807977887643414711944912069376356633491565114517263965263150144656557937396321386\n```\n\nWe can see that it delivers the message encrypted correctly, in this step the sender delivers the encrypted message through an insecure channel, and we can decrypt it using our public key.\n\n``` shell\nSelect Option: 3\nEnter the encrypted message: 14746634659051772345090619160847739346107822025878135056179431840383544183296437967909270354886735311180345743247212097706211101979451086028894835680585248834437560595077643176436038963195768876269911488174549424136154017029571441970067895516962286416050993934884906175304494142739460796136689018735564315035027718985716888453925282184178223539865834082853819602735459279117003460750949613734854620605171680712533091450949344077259036144276006252259608802572121747059525003621833160828635385601464134106144846772874481867511654254510137807977887643414711944912069376356633491565114517263965263150144656557937396321386\n\nEnter the private key that will encrypt the message (base64): LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFb2dJQkFBS0NBUUVBdTdMNWtiVTROQys3UnFRaVZnTUJmT3gvdzRjdW5pT......\n\nDecrypted message: b'Mi password is securepassword123'\n```\n## Brief explanation of RSA.\n\n### Key generation.\n\nTo generate the keys in RSA, two different prime numbers $p$ and $q$ are chosen, these numbers are random and must have a similar bit length. With both numbers $n = pq$ is calculated. N is the modulus for the public and private keys.\n\nUsing euler's function we calculate $\\phi = (p - 1)(q - 1)$. Then find a positive integer $e$ that is less than $\\phi$ to use as a public exponent.\n\nWith these values, d is determined to be equal to $e d 1.\n\nThe public key is $n$ and $e$ and the private key is $n$ and $d$.\n\n### Encryption\n\nWith $M$ being the plaintext message and $C$ being the encrypted message, Alice sends her public key to Bob over an insecure channel while keeping her private key secret. Bob uses the following formula to encrypt the message.\n\n$c \\equiv M^{e} \\pmod n$\n\nBasic example in Python.\n\n``` python\nfrom Crypto.Util.number import *\n\ne = 0x10001\n\np, q = getPrime(1024), getPrime(1024)\nn = p * \nmessage = bytes_to_long(input('Enter your message: ').encode())\nenc_message = pow(message, e, n)\n```\n\n### Deciphered.\n\nAlice can recover $M$ from $C$ using $d$.\n\n$m \\equiv c^{d} \\pmod n$.\n\nBasic Python example:\n\n``` python\nfrom Crypto.Util.number import *\n\np, q = getPrime(1024), getPrime(1024)\nn = p * q\nphi = (p - 1) * (q - 1)\ne = 0x10001\nd = inverse(e, phi)  # Calcular la clave privada d\n\nenc_message = int(input('Enter the encrypted message: '))\ndec_message = pow(enc_message, d, n)\n\noriginal_message = long_to_bytes(dec_message)\n\nprint(original_message)\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabund4nt%2Frsa-implementation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabund4nt%2Frsa-implementation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabund4nt%2Frsa-implementation/lists"}