1- import crypter
21import ast
32import 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
69class 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
3363class 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 :
0 commit comments