Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

Commit

Permalink
Added Encryption and creadential handling
Browse files Browse the repository at this point in the history
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
Philotheephilix committed May 11, 2023
1 parent 5d1d1ae commit 832bae1
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .env
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
60 changes: 60 additions & 0 deletions Initialize.py
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()
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,32 @@ BotFather is the official bot used to create and manage Telegram bots. Here are
pillow

pypdf (version 2.0 as latest 3.0 has some issues)

cryptography

dotenv

instagrapi

4. Configure email and API tokens

5.Run this script in Python IDLE or in some IDE
5. Run Initialize.py script in Python IDLE or in some IDE first to configure all needed credentials

6. Then, Run multi_bot.py to start the bot



## Security
Our goal is to provide secure environment so we have no unneccessary online communication like analytics it's just standalone on your desk
As we give our E-Mail id to our bot we don't know how secure is this we are working on that to improvise the privacy.
Our goal is to provide secure environment so we have no unneccessary online communication like analytics it's just standalone on your desk

As we give our E-Mail id to our bot we don't know how secure is this we are working on that to improvise the privacy.

We have included environmental variable to store all the credentials which is encrypted while executing initialize.py and will be only decrypted during bot startup

The most important thing is that it is based on rolling codes and the encryption key changes everytime you start the bot and is stored securely

## Feature request
If you guys are into some feature just feel free to contact me through [TELEGRAM](https://t.me/philo_thee_philix)

## Disclaimer
Anyone can download or copy our code to use and redistribute but if you copy the whole code to someother projects kindly don't forget to give credits
Anyone can download or copy our code to use and redistribute but if you copy the whole code to someother projects kindly don't forget to give credits
Empty file added crypt.key
Empty file.
61 changes: 56 additions & 5 deletions multi_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
Empty file added session.json
Empty file.

0 comments on commit 832bae1

Please sign in to comment.