add script and info for RSA encrypt/decrypt

This commit is contained in:
Vicky Steeves 2021-11-22 15:20:51 -05:00
commit 5b7329dc79
4 changed files with 209 additions and 0 deletions

120
.gitignore vendored Normal file
View File

@ -0,0 +1,120 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
encrypted_file.txt
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# PyCharm specific
.idea

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Vicky Rampin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

22
README.md Normal file
View File

@ -0,0 +1,22 @@
# 2021 CSCI-GA3205 Homework 11
Author: Vicky Rampin
## Prompt
Write a program to encrypt and decrypt a message (string) using the RSA algorithm. You can implement the functions you need or use the libraries.
## Installation
This script is made with Python 3.8 and uses the `argparse` and `pycryptodome` libraries. Ensure that you have [Python 3.8+](https://www.python.org/downloads/) and these libraries installed in order to run this script correctly.
## Usage
First, clone this repository to your machine. Open the command line on your machine and navigate to where you cloned this repository. To get help using `cd`, use [this tutorial](https://swcarpentry.github.io/shell-novice/02-filedir/index.html).
Before running the script, you should know it expects one argument:
1. `--msg`: provide a message that you want to encrypt and decrypt
With this in mind, you can run it like so:
```
$ python index.py --msg="helloWORLD"
```
It will encrypt and decrypt the message using a randomly generated public and private key pair (RSA), and print as it goes.

46
index.py Normal file
View File

@ -0,0 +1,46 @@
import argparse
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
def encrypt(text, pub):
public_key = RSA.importKey(pub)
public_key = PKCS1_OAEP.new(public_key)
encrypted_msg = public_key.encrypt(text)
return encrypted_msg
def decrypt(text, priv):
private_key = RSA.importKey(priv)
private_key = PKCS1_OAEP.new(private_key)
decrypted_msg = private_key.decrypt(text)
return decrypted_msg
def main():
# get arguments from command line
parser = argparse.ArgumentParser()
parser.add_argument('--msg', type=str)
args = parser.parse_args()
# generate public and private keys to use for encryption and decryption
key = RSA.generate(3072)
priv = key.export_key('PEM')
pub = key.publickey().exportKey('PEM')
# print original message
original = args.msg
print("Original message: " + original)
# print encrypted message
ciphertext = encrypt(original.encode(), pub)
print("Encrypted message: ")
print(ciphertext)
# print decrypted message
plaintext = decrypt(ciphertext, priv)
print("Decrypted message: " + plaintext.decode())
if __name__ == '__main__':
main()