Skip to content

Commit a5df67b

Browse files
author
Grc Algoritmos
committed
.
1 parent 52517cc commit a5df67b

4 files changed

Lines changed: 150 additions & 53 deletions

File tree

.DS_Store

6 KB
Binary file not shown.

LICENSE renamed to LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Renne
3+
Copyright (c) 2021 Renne C. (GRC Algoritmos)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

__init__.py

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,79 @@
1-
import crypter
21
import ast
32
import json
4-
3+
from os.path import exists
4+
from .crypter import (encrypt256, encrypt, encode64,
5+
decrypt, decode64,
6+
get_content, get_sequence5, get_sequence6, get_string,
7+
open_file)
58

69
class Encrypt:
710

811
def encryptjson(decrypted_string, password="", sha256=True):
912
if sha256:
10-
password = crypter.encrypt256(password)
13+
password = encrypt256(password)
1114
# RETURN AS A STRING
12-
return crypter.encrypt(decrypted_string, password)
15+
return encrypt(decrypted_string, password)
1316

14-
def jsonfile(filename, password="", sha256=True, save_file=False, output="json"):
15-
json_crypt_document = Encrypt.encryptjson(crypter.open_file(filename), password, sha256)
17+
def jsonfile(filename, password="", sha256=True, save_file=False):
18+
try:
19+
json_crypt_document = Encrypt.encryptjson(open_file(filename), password=password, sha256=sha256)
20+
except:
21+
return None
1622
if save_file:
1723
# SAVE INTO FILE AND RETURN A BOOLEAN TRUE
18-
with open(filename, "w", encoding="utf-8") as json_file_encrypted :
19-
json_file_encrypted.write(json_crypt_document)
20-
return True
24+
try:
25+
with open(filename, "w", encoding="utf-8") as json_file_encrypted :
26+
json_file_encrypted.write(json_crypt_document)
27+
return True
28+
except:
29+
return False
2130
else:
22-
if output == "json":
23-
# RETURN AS A DICTIONARY
24-
return json.loads(json_crypt_document)
31+
# RETURN AS A STRING
32+
return json_crypt_document
33+
34+
def jsonstring(string_to_encrypt, password="", sha256=True, save_file=False, filename=None, overwrite=False):
35+
if save_file and filename is not None:
36+
if not exists(filename) or exists(filename) and overwrite:
37+
try:
38+
with open(filename, "w", encoding="utf-8") as string_encrypted:
39+
string_encrypted.write(Encrypt.encryptjson(string_to_encrypt, password=password, sha256=sha256))
40+
return True
41+
except:
42+
return False
2543
else:
26-
# RETURN AS A STRING
27-
return json_crypt_document
44+
return False
45+
else:
46+
return Encrypt.encryptjson(string_to_encrypt, password=password, sha256=sha256)
2847

29-
def jsonstring(string_to_encrypt, password="", sha256=True):
30-
return Encrypt.encryptjson(string_to_encrypt, password, sha256)
48+
def dictionary(dictionary_to_encrypt, password="", sha256=True, save_file=False, filename=None, overwrite=False):
49+
if save_file and filename is not None:
50+
if not exists(filename) or exists(filename) and overwrite:
51+
try:
52+
with open(filename, "w", encoding="utf-8") as dictionary_encrypted:
53+
dictionary_encrypted.write(Encrypt.encryptjson(str(dictionary_to_encrypt), password=password, sha256=sha256))
54+
return True
55+
except:
56+
return False
57+
else:
58+
return False
59+
else:
60+
return Encrypt.encryptjson(str(dictionary_to_encrypt), password=password, sha256=sha256)
3161

3262

3363
class Decrypt:
3464

3565
def decryptjson(encrypted_document, password="", sha256=True, ignore_verification=False, indent=4):
3666
try:
37-
original_document_hash = crypter.encrypt256(encrypted_document[0])
67+
original_document_hash = encrypt256(encrypted_document[0])
3868
if sha256:
39-
password = crypter.encrypt256(password)
69+
password = encrypt256(password)
4070
# CHECK IF FILE IS ENCRYPTED OR IF WAS MODIFIED
4171
if encrypted_document[6] == original_document_hash or ignore_verification:
42-
crypt_key1 = crypter.get_sequence5(0, 5, password, encrypted_document[1])
43-
crypt_key2 = crypter.get_sequence5(5, 10, password, encrypted_document[2])
44-
crypt_key3 = crypter.get_sequence5(10, 15, password, encrypted_document[3])
45-
crypt_key4 = crypter.get_sequence5(15, 20, password, encrypted_document[4])
46-
crypt_key5 = crypter.get_sequence6(20, 26, password, encrypted_document[5])
72+
crypt_key1 = get_sequence5(0, 5, password, encrypted_document[1])
73+
crypt_key2 = get_sequence5(5, 10, password, encrypted_document[2])
74+
crypt_key3 = get_sequence5(10, 15, password, encrypted_document[3])
75+
crypt_key4 = get_sequence5(15, 20, password, encrypted_document[4])
76+
crypt_key5 = get_sequence6(20, 26, password, encrypted_document[5])
4777
if crypt_key1 is not None and crypt_key2 is not None and \
4878
crypt_key3 is not None and crypt_key4 is not None and \
4979
crypt_key5 is not None:
@@ -58,8 +88,8 @@ def decryptjson(encrypted_document, password="", sha256=True, ignore_verificatio
5888
crypt_content.append(key_sequence)
5989
for key_sequence in crypt_key5:
6090
crypt_content.append(key_sequence)
61-
decrypt = crypter.decrypt(encrypted_document[0], crypt_content)
62-
return json.dumps(ast.literal_eval(decrypt), indent=indent, ensure_ascii=False)
91+
decrypted_string = decrypt(encrypted_document[0], crypt_content)
92+
return json.dumps(ast.literal_eval(decrypted_string), indent=indent, ensure_ascii=False)
6393
else:
6494
return None
6595
else:
@@ -68,12 +98,19 @@ def decryptjson(encrypted_document, password="", sha256=True, ignore_verificatio
6898
return False
6999

70100
def jsonstring(string_to_decrypt, password="", sha256=True, ignore_verification=False, indent=4):
71-
encrypted_document = crypter.get_string(string_to_decrypt)
72-
return Decrypt.decryptjson(encrypted_document, password, sha256, ignore_verification, indent)
101+
encrypted_document = get_string(string_to_decrypt)
102+
return Decrypt.decryptjson(encrypted_document, password=password, sha256=sha256,
103+
ignore_verification=ignore_verification, indent=indent)
104+
105+
def dictionary(dictionary_to_decrypt, password="", sha256=True, ignore_verification=False, indent=4):
106+
encrypted_document = get_string(dictionary_to_decrypt)
107+
return json.loads(Decrypt.decryptjson(encrypted_document, password=password, sha256=sha256,
108+
ignore_verification=ignore_verification, indent=indent))
73109

74110
def jsonfile(file_to_decrypt, password="", sha256=True, ignore_verification=False, indent=4, save_file=False, output="json"):
75-
encrypted_document = crypter.get_content(file_to_decrypt)
76-
decrypted_string = Decrypt.decryptjson(encrypted_document, password, sha256, ignore_verification, indent)
111+
encrypted_document = get_content(file_to_decrypt)
112+
decrypted_string = Decrypt.decryptjson(encrypted_document, password=password, sha256=sha256,
113+
ignore_verification=ignore_verification, indent=indent)
77114
if decrypted_string:
78115
if save_file:
79116
with open(file_to_decrypt, "w", encoding="utf-8") as output_file:

example.py

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,95 @@
1-
import jsoncrypt
1+
from jsoncrypt import Encrypt, Decrypt
22

3-
# TO ENCRYPT A JSON FILE - FOLLOW EXAMPLE
4-
filename = "example.json"
3+
# FORMULAS TO ENCRYPT AND DECRYPT
4+
# Encrypt.jsonfile(json_filename_to_encrypt)
5+
# Encrypt.jsonstring(json_string_to_encrypt)
6+
# Encrypt.dictionary(a_dictionary_to_encrypt)
7+
# Decrypt.jsonfile(filename_encrypted)
8+
# Decrypt.jsonstring(json_string_encrypted)
9+
# Decrypt.dictionary(string_of_a_dictionary_encrypted)
10+
# FOLLOWING SOME EXAMPLES
511

6-
# TO ENCRYPT AND SAVE FILE
7-
if jsoncrypt.Encrypt.jsonfile(filename, save_file=True):
8-
with open(filename, "r") as file:
9-
encrypted_json = file.read()
10-
print(encrypted_json)
12+
# SELECT A JSON FILE
13+
filename = "./example.json"
1114

12-
print("encrypted_json type is -> {}".format(type(encrypted_json)))
13-
input("Press any key to see decrypted version working with a string")
15+
# TO ENCRYPT FROM A FILE AND JUST GET AS A STRING WITHOUT SAVING IT
16+
Encrypt.jsonfile(filename)
17+
# IT WILL RETURN A STRING IF WAS A JSON FILE AND SUCCESS TO ENCRYPT OR
18+
# IT WILL RETURN A NONE IF WASN'T A JSON FILE AND FAILED TO ENCRYPT
19+
# FINALLY TO CATCH NOT JSON FILES USE
20+
string_encrypted = Encrypt.jsonfile(filename)
21+
if string_encrypted:
22+
print("Encrypted successfully")
23+
print(string_encrypted)
24+
else:
25+
print("Not possible to encrypt non json files")
26+
27+
# TO ENCRYPT FROM A FILE AND SAVE FILE USE
28+
# Encrypt.jsonfile(filename, save_file=True)
29+
# IT WILL RETURN BOOLEAN TRUE IF WAS SUCCESS OR BOOLEAN FALSE IF FAILED TO SAVE
30+
# FINALLY USE FORMULA TO CATCH NON JSON FILES OR NO PERMISSIONS TO SAVE ON DISK
31+
if Encrypt.jsonfile(filename, save_file=True):
32+
print("File {} was successfully encrypted and has been saved!".format(filename))
33+
else:
34+
print("Failed to encrypt file")
1435

15-
# TO USE DECRYPTION AS A STRING
16-
decrypted_json = jsoncrypt.Decrypt.jsonstring(encrypted_json)
17-
print(decrypted_json)
36+
# DECRYPT INTO MEMORY TO USE DATA AS NECESSARY (HERE FILE USED MUST BE ENCRYPTED TO BE ABLE TO DECRYPT RIGHT!?)
37+
# IF FILE IS NOT ENCRYPTED IT WILL RETURN A BOOLEAN FALSE
38+
# TO DECRYPT AND GET AS DICTIONARY USE
39+
dictionary = Decrypt.jsonfile(filename)
40+
# TO DECRYPT AND GET AS STRING USE
41+
string_decrypted = Decrypt.jsonfile(filename, output="string")
42+
# TO DECRYPT AND GET AS STRING AND CHANGE INDENT USE (DEFAULT INDENT IS 4)
43+
string_indented = Decrypt.jsonfile(filename, output="string", indent=2)
44+
45+
# TO DECRYPT FROM A FILE AND SAVE IT
46+
# IT WILL RETURN BOOLEAN TRUE IF WAS SUCCESSFULLY DECRYPTED OR BOOLEAN FALSE IF FAILED TO SAVE
47+
# FINALLY USE FORMULA TO CATCH NON ENCRYPTED FILES OR NO PERMISSIONS TO SAVE ON DISK
48+
if Decrypt.jsonfile(filename, save_file=True):
49+
print("File {} was successfully decrypted and has been saved!".format(filename))
1850
else:
19-
print("Something went wrong!")
51+
print("File is not encrypted or it was modified")
2052

21-
input("Press any key to decrypt file and save it")
53+
# IT IS POSSIBLE ALSO TO ENCRYPT AND DECRYPT FROM A STRING
54+
# IMPORTANT! STRING MUST BE ON A JSON TYPE FORMAT
55+
my_json_string_encrypted = Encrypt.jsonfile(filename)
56+
print("ENCRYPTED FROM A FILE INTO A STRING")
57+
print(my_json_string_encrypted)
58+
# TO DECRYPT A STRING ON FORMAT OF JSON USE
59+
decrypted_jsonstring = Decrypt.jsonstring(my_json_string_encrypted)
60+
if decrypted_jsonstring:
61+
print("DECRYPTED FROM A STRING INTO ANOTHER STRING")
62+
print(decrypted_jsonstring)
63+
else:
64+
print("String is not encrypted or not in a json format")
2265

23-
# TO DECRYPT AND SAVE FILE
24-
if jsoncrypt.Decrypt.jsonfile(filename, save_file=True):
25-
with open(filename, "r") as file:
26-
decrypted_json = file.read()
27-
print(decrypted_json)
66+
# IS POSSIBLE TO ENCRYPT ALSO A DICTONARY TYPE
67+
# ON ENCRYPT IT RETURN A STRING AND ON DECRYPT IT RETURN A DICTIONARY
68+
mydict = {"Name": "GRC Algoritmos"}
69+
encrypted_dict = Encrypt.dictionary(mydict)
70+
# LET's SEE:
71+
print("Encrypted Dictionary:")
72+
print(encrypted_dict)
73+
print(type(encrypted_dict))
74+
print("Decrypted Dictionary:")
75+
# DECRYPTED AS A DICTIONARY
76+
decrypted_dict = Decrypt.dictionary(encrypted_dict)
77+
print(decrypted_dict)
78+
print(type(decrypted_dict))
79+
# DECRYPTED AS A STRING
80+
decrypted_string = Decrypt.jsonstring(encrypted_dict)
81+
print(decrypted_string)
82+
print(type(decrypted_string))
2883

29-
input("Press any key to encrypt from a file into memory")
84+
# IS POSSIBLE TO ENCRYPT A DICTONARY AND SAVE IN A FILE
85+
# IT WILL RETURN A BOOLEAN TRUE IF WAS SUCCESSFULL SAVED OR BOOLEAN FALSE IF FAILED
86+
# IF FILE EXISTS AS DEFAULT IT WILL RETURN FALSE BUT IS POSSIBLE TO ALLOW OVERWRITE JUST SET overwrite=True
87+
mydict = {"Name": "GRC Algoritmos", "File": "Saved"}
88+
if Encrypt.dictionary(mydict, save_file=True, filename="grc.json", overwrite=True):
89+
print("Successfully saved!")
90+
else:
91+
print("Not possible to save")
3092

31-
# TO ENCRYPT WITHOUT SAVING IN A FILE
32-
print(jsoncrypt.Encrypt.jsonfile(filename, save_file=False, output="json"))
3393

3494
# OPTIONS TO ENCRYPT AND DECRYPT A JSON FILE
3595
# save_file="Boolean" -> Saves the result into the same file

0 commit comments

Comments
 (0)