-
Notifications
You must be signed in to change notification settings - Fork 738
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from adityaarakeri/master
adding file-encrypt-decrypt
- Loading branch information
Showing
6 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 🗝 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |