Skip to content

Commit

Permalink
Merge pull request #70 from adityaarakeri/master
Browse files Browse the repository at this point in the history
adding file-encrypt-decrypt
  • Loading branch information
hastagAB authored Oct 5, 2019
2 parents b3d2ab0 + 63cb0ba commit f2c858d
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ So far, the following projects have been integrated to this repo:

| Project Name | Contributors |
|--|--|
|[File Encrypt Decrypt](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/file-encrypt-decrypt)|[Aditya Arakeri](https://github.com/adityaarakeri)|
| [Address locator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Location_Of_Adress) | [Chris]() |
|[AI chatbot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Artificial-intelligence_bot) |[umar abdullahi](https://github.com/umarbrowser) |
|[Asymmetric Encryption](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/asymmetric_cryptography) |[victor matheus](https://github.com/victormatheusc) |
Expand Down
1 change: 1 addition & 0 deletions file-encrypt-decrypt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
13 changes: 13 additions & 0 deletions file-encrypt-decrypt/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
cryptography = "*"
pytest = "*"

[requires]
python_version = "3.7"
175 changes: 175 additions & 0 deletions file-encrypt-decrypt/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions file-encrypt-decrypt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 🗝 crypt

A command line python script which can Encrypt a given file and also decrypt the encrypted file



#### Pre-requisites
* install pipenv
```sh
$ brew install pipenv
```

* install dependencies
```sh
$ pipenv install
```

#### Usage
* Encrypt file
```sh
$ pipenv run python crypt -e file.txt
```
* Decrypt file
```sh
$ pipenv run python crypt -d file.enc
```
**note**
- `file.enc` will be created if you pass in `file.txt`
- Do not loose the Encryption 🗝
74 changes: 74 additions & 0 deletions file-encrypt-decrypt/crypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import argparse

from cryptography.fernet import Fernet

class Crypt:

def __init__(self):

# can be generated Fernet.generate_key()
# if generated, save it below
self.key = b'oBa5LeeJt1r4BmNyJXb6FHd1U21GMshH9Pqu_J-HzNQ='
self.fernet = Fernet(self.key)

def encrypt(self, input_file_path):
"""
Encrypt a file
"""

# split the file and take only the file name
base_name = os.path.basename(input_file_path).split('.')[0].split('-')[-1]

# creates a file name with extension .enc
output_file = f"{base_name}.enc"
if os.path.exists(output_file):
print(f'Encrypted File already exists')
else:
with open(input_file_path, 'rb') as i:
input_data = i.read()

encrypted = self.fernet.encrypt(input_data)

with open(output_file, 'wb') as o:
o.write(encrypted)
print(f'Encrypted file: {output_file}\n')


def decrypt(self, input_file_path, output_file_ext='txt'):
"""
Decrypt an already encrypted file
"""

# split the file and take only the file name
base_name = os.path.basename(input_file_path).split('.')[0].split('-')[-1]
output_file = f'{base_name}.{output_file_ext}'
with open(input_file_path, 'rb') as f:
file_data = f.read()

decrypted = str(self.fernet.decrypt(file_data), 'utf-8')

with open(output_file, 'w') as o:
o.write(decrypted)
print(f'Decrypted file: {output_file}\n')


if __name__ == '__main__':
crypt = Crypt()

parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-e', '--encrypt',
help='Encrpyt the file')
parser.add_argument('-d', '--decrypt',
help='Decrypt the file')

args = parser.parse_args()

if args.encrypt:
print(f'Input file: {args.encrypt}')
crypt.encrypt(args.encrypt)
elif args.decrypt:
print(f'Input file: {args.decrypt}')
crypt.decrypt(args.decrypt)

0 comments on commit f2c858d

Please sign in to comment.