{"id":27292868,"url":"https://github.com/okqsna/laboratory-work-2-encryption","last_synced_at":"2026-02-23T07:03:30.428Z","repository":{"id":286161718,"uuid":"959143136","full_name":"okqsna/laboratory-work-2-encryption","owner":"okqsna","description":"Laboratory work №2 on Discrete Math. Implementation of secure transmission of messages.","archived":false,"fork":false,"pushed_at":"2025-04-18T14:32:32.000Z","size":42,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-19T01:04:32.581Z","etag":null,"topics":["discrete-mathematics","python-cryptography","rsa-cryptography","socket"],"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/okqsna.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,"zenodo":null}},"created_at":"2025-04-02T10:36:38.000Z","updated_at":"2025-04-18T14:32:35.000Z","dependencies_parsed_at":"2025-04-11T22:18:25.893Z","dependency_job_id":null,"html_url":"https://github.com/okqsna/laboratory-work-2-encryption","commit_stats":null,"previous_names":["okqsna/laboratory-work-2-encryption"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/okqsna/laboratory-work-2-encryption","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okqsna%2Flaboratory-work-2-encryption","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okqsna%2Flaboratory-work-2-encryption/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okqsna%2Flaboratory-work-2-encryption/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okqsna%2Flaboratory-work-2-encryption/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/okqsna","download_url":"https://codeload.github.com/okqsna/laboratory-work-2-encryption/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okqsna%2Flaboratory-work-2-encryption/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29739024,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-23T04:51:08.365Z","status":"ssl_error","status_checked_at":"2026-02-23T04:49:15.865Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["discrete-mathematics","python-cryptography","rsa-cryptography","socket"],"created_at":"2025-04-11T22:18:25.227Z","updated_at":"2026-02-23T07:03:30.422Z","avatar_url":"https://github.com/okqsna.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# laboratory-work-2-encryption\n\u003e Implementation of secure transmission of messages by using the RSA algorithm and \u003cbr\u003e\nfast exponentiation algorithm in modular arithmetic.\n\n### RSA algorithm \u003cbr\u003e\n`rsa_algorithm.py`\nIn the implementation of the algorithm, we used such libraries:\n- \u003cb\u003eMath\u003c/b\u003e - for calculations\n- \u003cb\u003eSecrets\u003c/b\u003e - for generation of 2048-bits prime numbers\n- \u003cb\u003eSumpy\u003c/b\u003e - for generation of 2048-bits prime numbers\n- \u003cb\u003eHashlib\u003c/b\u003e - for generation of hash of a messages\n\n1. The algorithm starts by generating two 2048-bit prime numbers using the libraries.\n2. Then we calculate the first part of the public key by multiplying the previously found prime numbers.\n3. The next step is finding a small odd number whose gcd with (p-1)(q-1), where p and q are 1024-bit prime numbers, equals 1.\n4. And the last step is the calculation of the private key, which is an integer, the reciprocal of a small number modulo (p-1)(q-1)\n\n`rsa_algorithm` function returns a dictionary with all data needed for later encryption \u0026 decryption of messages.\n### Encryption \u0026 Decryption\nFor encryption and decryption, we used the  algorithm provided in lecture 8.2 - `block cipher` \u003cbr\u003e\nThe main idea of this cipher is to calculate a new block by the formula:\n$$C = M^e modn$$ \u003cbr\u003e\nDecryption happens by this formula: $$M = C^d modn$$\n\nIn the ``encrypt`` function:\n1. We find the maximum length of a block of symbols for the public key\n2. Then we transform each symbol to a numeric value by the ord function, adding zeros to match the length of a block\n3. Next, we divide the message into blocks of max length - numeric values to encrypt\n4. The last step is the encryption of the numeric value we got from the formula\n\nIn the ``decrypt`` function:\n1. We find the maximum length of a block of symbols for the public key\n2. Next is decryption by the formula and filling it with zeros to match the length of a block\n3. The last step is to loop through the message by breaking it into small blocks of max length\u003cbr\u003e\nand convert it to a symbol using the built-in function chr\n\n### User-server communication\nIn this part, we worked according to the \u003cb\u003esocket.io\u003c/b\u003e library and instructions in Client \u0026 Server files.\n\n- `Client`\u003cbr\u003e\nThe core functions of the class are read_handler and write_handler. \u003cbr\u003e\n`read_handler` \u003cbr\u003e\nClient receives an encrypted message and a hash from the server, decrypts it, and if the hash value is the same\nprints it out; else, it shows the user that an error has occurred.\n`write_handler` \u003cbr\u003e\nThe client is asked to write a message. If it does not consist of whitespaces only, the message gets encrypted, and the hash value is created\nto be sent to a server.\n\n\n- `Server`\u003cbr\u003e\nThe core functions of the class are start, handle_client, and broadcast. \u003cbr\u003e\n`start`\u003cbr\u003e\nIn this function, the server begins to run, receives the keys from client(s), keeps track of connected users, and does all the manual connection job.\n`handle client`\u003cbr\u003e\nIn this function, the server receives the message from a client(s), decrypts it, and compares the hash values as well.\n`broadcast`\u003cbr\u003e\nIn this function, the server sends the message from a client(s), encrypts it, and creates the hash value.\n\n### Hash of messages\nFor the implementation of this checking, we used ``hashlib`` library.\u003cbr\u003e\n\u003ci\u003e Example: \u003c/i\u003e\u003cbr\u003e\nWhen a user sends a message, he sends a hash of this message as well, generated in ``hash_message`` function.\nLater, when the server decrypts it, it generates a hash of the decrypted message and compares those hashes with ``check_message_integrity`` function.\n\n\n### Contributors\n- [Oksana Moskviak](https://github.com/okqsna) \n- [Anastasiia Yablunovska](https://github.com/ystacy-ab)\n\nThe laboratory work was a collaborative effort between the two of us.\n\u003cbr\u003e\n\u003cI\u003e@ Created by CS UCU students, April 2025\u003c/i\u003e \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fokqsna%2Flaboratory-work-2-encryption","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fokqsna%2Flaboratory-work-2-encryption","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fokqsna%2Flaboratory-work-2-encryption/lists"}