This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Encryption and creadential handling
1. Separated Credentials from main code to improve security 2. Added initialization python file for easy setup of non technical users 3. Added rolling key encryption to improve credential encryption
- Loading branch information
1 parent
5d1d1ae
commit 832bae1
Showing
6 changed files
with
141 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
E-mail_for_checking_email=nono | ||
Password_for_checking_mail=none | ||
Instagram_username=none | ||
Instagram_password=none | ||
TELE_API_KEY=none | ||
Open_Weather_API=none |
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,60 @@ | ||
import os | ||
from cryptography.fernet import Fernet | ||
with open('crypt.key', 'wb') as filekey: | ||
filekey.write(key) | ||
def init_var(): | ||
mod={ | ||
"E-mail_for_checking_email":"", | ||
"Password_for_checking_mail":"", | ||
"Instagram_username":"", | ||
"Instagram_password":"", | ||
"Telegram_bot_API":"", | ||
"Open_Weather_API":"" | ||
} | ||
data=open("kets.env","r") | ||
dada=data.readlines() | ||
for val in dada: | ||
print(val) | ||
k,v=val.strip().split(":",1) | ||
if k in mod: | ||
new_value = input(f"Enter a new value for {k}: ") | ||
mod[k] = new_value | ||
data.close() | ||
data=open("kets.env","w") | ||
for key, value in mod.items(): | ||
data.write(f"{key}:{value}\n") | ||
data.close() | ||
data=open("kets.env","r") | ||
dada=data.read() | ||
print(dada) | ||
def encrypt(): | ||
key = Fernet.generate_key() | ||
with open('crypt.key', 'wb') as filekey: | ||
filekey.write(key) | ||
print("new key gen") | ||
with open('crypt.key', 'rb') as filekey: | ||
key = filekey.read() | ||
fernet = Fernet(key) | ||
with open('kets.env', 'rb') as file: | ||
original = file.read() | ||
encrypted = fernet.encrypt(original) | ||
with open('kets.env', 'wb+') as encrypted_file: | ||
encrypted_file.write(encrypted) | ||
enc=encrypted_file.read() | ||
print(enc) | ||
def decrypt(): | ||
try: | ||
with open('crypt.key', 'rb') as filekey: | ||
key = filekey.read() | ||
fernet = Fernet(key) | ||
with open('.env', 'rb') as file: | ||
original = file.read() | ||
decrypted = fernet.decrypt(original) | ||
with open('.env', 'wb+') as decrypted_file: | ||
decrypted_file.write(decrypted) | ||
print("decrypted") | ||
except: | ||
print("Invalid Key or Already decrypted") | ||
decrypt() | ||
init_var() | ||
encrypt() |
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 |
---|---|---|
|
@@ -10,21 +10,74 @@ | |
import re | ||
from instagrapi import Client | ||
from instagrapi.exceptions import LoginRequired | ||
from dotenv import load_dotenv | ||
# | ||
# | ||
# INITIALIZE ALL REQUIRED KEYS AND CREDENTIALS BELOW FOR PROPER WORKING OF BOT | ||
# | ||
# | ||
OW_API = "<Enter Open Weather API KEY>" | ||
TELE_API_KEY = "<Enter telegram API KEY>" | ||
USERNAME,PASSWORD="<Enter your insta username>", "<Enter your Insta passwd>" | ||
from cryptography.fernet import Fernet | ||
#Variable initializing | ||
|
||
is_weather="0" | ||
sos_active="0" | ||
reels_active="0" | ||
string=" " | ||
cl = Client() | ||
tempdir=os.getcwd() | ||
Credentials={} | ||
|
||
|
||
#Code for handling credential encryption and decryption | ||
a=1 | ||
if a==1: | ||
try: | ||
with open('crypt.key', 'rb') as filekey: | ||
key = filekey.read() | ||
|
||
fernet = Fernet(key) | ||
with open('.env', 'rb') as file: | ||
original = file.read() | ||
decrypted = fernet.decrypt(original) | ||
with open('.env', 'wb+') as decrypted_file: | ||
decrypted_file.write(decrypted) | ||
dnc=decrypted_file.read() | ||
print("decrypted") | ||
except : | ||
print("Invalid Key or Already decrypted") | ||
|
||
#loading credentials and keys from env file | ||
load_dotenv(tempdir+"\.env") | ||
username=os.getenv("E-mail_for_checking_email") | ||
password=os.getenv("Password_for_checking_mail") | ||
TELE_API_KEY = os.getenv("TELE_API_KEY") | ||
print(TELE_API_KEY) | ||
OW_API = os.getenv("Open_Weather_API") | ||
USERNAME = os.getenv("Instagram_username") | ||
PASSWORD = os.getenv("Instagram_password") | ||
with open(".env","r") as credentials: | ||
Credentials=credentials.readlines() | ||
print(Credentials) | ||
key = Fernet.generate_key() | ||
with open('crypt.key', 'wb') as filekey: | ||
filekey.write(key) | ||
print("new key gen") | ||
try: | ||
with open('crypt.key', 'rb') as filekey: | ||
key = filekey.read() | ||
fernet = Fernet(key) | ||
with open('.env', 'rb') as file: | ||
original = file.read() | ||
encrypted = fernet.encrypt(original) | ||
with open('.env', 'wb+') as encrypted_file: | ||
encrypted_file.write(encrypted) | ||
enc=encrypted_file.read() | ||
print("encrypted") | ||
except: | ||
print("encrypt failed") | ||
|
||
print(Credentials) | ||
|
||
# Directory creation and verification | ||
def init_dirs(): | ||
req_dir=("reels","temppdf","merged","tempimg") | ||
|
@@ -108,8 +161,6 @@ def junk_removal(): | |
@bot.message_handler(commands=["check_email"]) | ||
def check_email(message): | ||
mail = imaplib.IMAP4_SSL("imap.gmail.com") | ||
username = "[email protected]" | ||
password = "456@Icam" | ||
mail.login(username, password) | ||
mail.select("inbox") | ||
result, data = mail.search(None, "UNSEEN") | ||
|
Empty file.