Fixed escape character issue. Generation of IV is random. Shared keys are encoded

This commit is contained in:
saizsassin 2018-06-25 03:55:49 +05:30 committed by GitHub
parent 2e456d10cc
commit 50b7627293
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,10 +15,14 @@ def splitSecret(secret,threshold,splits):
#Formatting key inorder to convert bytes of string using base64 #Formatting key inorder to convert bytes of string using base64
secret = base64.b64encode(secret).decode('utf-8') secret = base64.b64encode(secret).decode('utf-8')
shares = PlaintextToHexSecretSharer.split_secret(secret, threshold, splits) shares = PlaintextToHexSecretSharer.split_secret(secret, threshold, splits)
for i in range(splits):
shares[i]=base64.b64encode(shares[i].encode('utf-8')).decode('utf-8')
return shares return shares
# This function recovers the secret using the list of shares and returns the reconstructed secret # This function recovers the secret using the list of shares and returns the reconstructed secret
def recoverSecret(shares): def recoverSecret(shares):
for i in range(len(shares)):
shares[i] = (base64.b64decode(shares[i])).decode('utf-8')
secret = PlaintextToHexSecretSharer.recover_secret(shares) secret = PlaintextToHexSecretSharer.recover_secret(shares)
#Converting recovered_key to bytes using base64 module #Converting recovered_key to bytes using base64 module
secret=base64.b64decode(secret) secret=base64.b64decode(secret)
@ -40,22 +44,26 @@ def keyGen():
def encryptMsg(plaintext, key): def encryptMsg(plaintext, key):
# Initialization vector in AES should be 16 bytes # Genarating Initialization vector for AES (16 bytes)
IV = 16 * '\x00' IV = os.urandom(16)
# Creation of encryptor and decryptor object using above details # Encrypting The plaintext
cipher = AES.new(key, AES.MODE_CBC, IV) cipher = AES.new(key, AES.MODE_CBC, IV)
plaintext=base64.b64encode(plaintext.encode('utf-8')).decode('utf-8')
ciphertext = cipher.encrypt(pad(plaintext)) ciphertext = cipher.encrypt(pad(plaintext))
ciphertext = base64.b64encode(ciphertext).decode('utf-8') # Append IV and Ciphertext
ciphertext = base64.b64encode(IV).decode('utf-8') + base64.b64encode(ciphertext).decode('utf-8')
return ciphertext return ciphertext
def decryptMsg(ciphertext, key): def decryptMsg(ciphertext, key):
# Initialization vector in AES should be 16 bytes # Initialization vector in AES should be 16 bytes
IV = 16 * '\x00' IV = base64.b64decode(ciphertext[:24])
ciphertext=base64.b64decode(ciphertext[24:])
# Creation of encryptor and decryptor object using above details # Creation of encryptor and decryptor object using above details
cipher = AES.new(key, AES.MODE_CBC, IV) cipher = AES.new(key, AES.MODE_CBC, IV)
ciphertext=base64.b64decode(ciphertext) plaintext=unpad(cipher.decrypt(ciphertext));
return unpad(cipher.decrypt(ciphertext)); plaintext = (base64.b64decode(plaintext)).decode('utf-8')
return plaintext
def writeUnitToBlockchain(text,receiver): def writeUnitToBlockchain(text,receiver):
txid = subprocess.check_output(["flo-cli","--testnet", "sendtoaddress",receiver,"0.01",'""','""',"true","false","10",'UNSET',str(text)]) txid = subprocess.check_output(["flo-cli","--testnet", "sendtoaddress",receiver,"0.01",'""','""',"true","false","10",'UNSET',str(text)])
@ -75,9 +83,9 @@ def readUnitFromBlockchain(txid):
def writeDatatoBlockchain(text): def writeDatatoBlockchain(text):
n_splits = len(text)//350 + 1 #number of splits to be created n_splits = len(text)//350 + 1 #number of splits to be created
splits = list(sliced(text, 350)) #create a sliced list of strings splits = list(sliced(text, 350)) #create a sliced list of strings
for split in splits: #for split in splits:
print(split) # print(split)
print('no.of splits: '+str(n_splits)) #print('no.of splits: '+str(n_splits))
tail = writeUnitToBlockchain(splits[n_splits-1],'oV9ZoREBSV5gFcZTBEJ7hdbCrDLSb4g96i') #create a transaction which will act as a tail for the data tail = writeUnitToBlockchain(splits[n_splits-1],'oV9ZoREBSV5gFcZTBEJ7hdbCrDLSb4g96i') #create a transaction which will act as a tail for the data
cursor = tail cursor = tail
if n_splits == 1: if n_splits == 1:
@ -86,7 +94,7 @@ def writeDatatoBlockchain(text):
#for each string in the list create a transaction with txid of previous string #for each string in the list create a transaction with txid of previous string
for i in range(n_splits-2,-1,-1): for i in range(n_splits-2,-1,-1):
splits[i] = 'next:'+cursor+" "+splits[i] splits[i] = 'next:'+cursor+" "+splits[i]
print(splits[i]) #print(splits[i])
cursor = writeUnitToBlockchain(splits[i],'oV9ZoREBSV5gFcZTBEJ7hdbCrDLSb4g96i') cursor = writeUnitToBlockchain(splits[i],'oV9ZoREBSV5gFcZTBEJ7hdbCrDLSb4g96i')
return cursor return cursor
@ -95,11 +103,11 @@ def readDatafromBlockchain(cursor):
cursor_data = readUnitFromBlockchain(cursor) cursor_data = readUnitFromBlockchain(cursor)
while(cursor_data[:5]=='next:'): while(cursor_data[:5]=='next:'):
cursor = cursor_data[5:69] cursor = cursor_data[5:69]
print("fetching this transaction->>"+cursor) #print("fetching this transaction->>"+cursor)
text.append(cursor_data[70:]) text.append(cursor_data[70:])
cursor_data = readUnitFromBlockchain(cursor) cursor_data = readUnitFromBlockchain(cursor)
text.append(cursor_data) text.append(cursor_data)
print(text) #print(text)
text=('').join(text) text=('').join(text)
return text return text
@ -172,8 +180,8 @@ class GUI:
def Encryption(self): def Encryption(self):
splits = int(self.PE1.get()) splits = int(self.PE1.get())
threshold = int(self.PE2.get()) threshold = int(self.PE2.get())
if (threshold > splits) : if (threshold > splits or threshold<2) :
messagebox.showwarning("Invalid", "Total-Shares should be greater than or equal to Minimum-Shares-Required") messagebox.showwarning("Invalid!", "Minimum-Shares-Required should be greater than 2 and lesser than or equal to Total-Shares ")
return return
self.PE1.config(state='disabled') self.PE1.config(state='disabled')
self.PE2.config(state='disabled') self.PE2.config(state='disabled')
@ -181,13 +189,16 @@ class GUI:
key = keyGen() key = keyGen()
plaintext = self.PTextBox.get("1.0",'end-1c') plaintext = self.PTextBox.get("1.0",'end-1c')
shared_key = splitSecret(key,threshold,splits) shared_key = splitSecret(key,threshold,splits)
print("Shared Keys="+str(shared_key))
ciphertext = encryptMsg(plaintext,key) ciphertext = encryptMsg(plaintext,key)
print("Encrypted Text : " + ciphertext) try:
txid = writeDatatoBlockchain(ciphertext) txid = writeDatatoBlockchain(ciphertext)
except:
messagebox.showerror("Connection Failed!", "Please run the node(Flo-Core)!")
return
print("Shared Keys="+str(shared_key))
print('txid: ',txid) print('txid: ',txid)
self.PNextButton.destroy() self.PNextButton.destroy()
messagebox.showinfo("Successful", "Your message is successfully encrypted!!!") messagebox.showinfo("Successful!", "Your data is successfully encrypted and stored in the FLO Blockchain!")
@ -199,7 +210,7 @@ class GUI:
GL1.grid(row=1,column=1) GL1.grid(row=1,column=1)
self.GE1 = Spinbox(self.GetFrame, from_ = 2, to = 1000, validate="key", validatecommand=self.vcmd) self.GE1 = Spinbox(self.GetFrame, from_ = 2, to = 1000, validate="key", validatecommand=self.vcmd)
self.GE1.grid(row=1,column=2) self.GE1.grid(row=1,column=2)
GL2 = Label(self.GetFrame,text="Enter Transaction id : ") GL2 = Label(self.GetFrame,text="Enter Transaction hash : ")
GL2.grid(row=2,column=1) GL2.grid(row=2,column=1)
self.GE2 = Entry(self.GetFrame) self.GE2 = Entry(self.GetFrame)
self.GE2.grid(row=2,column=2) self.GE2.grid(row=2,column=2)
@ -210,6 +221,12 @@ class GUI:
self.GBackButton.grid(row=3,column=1) self.GBackButton.grid(row=3,column=1)
def GetSharedKey(self): def GetSharedKey(self):
try:
txid = self.GE2.get()
self.ciphertext = readDatafromBlockchain(txid)
except:
messagebox.showerror("Data retrieval Failed!", "Please enter valid transaction hash!\nAlso run the node(Flo-Core)!")
return
self.numOfShares = int(self.GE1.get()) self.numOfShares = int(self.GE1.get())
self.GFindButton.destroy() self.GFindButton.destroy()
self.GBackButton.destroy() self.GBackButton.destroy()
@ -228,16 +245,14 @@ class GUI:
self.GDecryptButton.grid(column=2) self.GDecryptButton.grid(column=2)
def DecryptMsg(self): def DecryptMsg(self):
txid = self.GE2.get()
ciphertext = readDatafromBlockchain(txid)
shares = [None] * self.numOfShares shares = [None] * self.numOfShares
for i in range(self.numOfShares): for i in range(self.numOfShares):
shares[i] = self.GEArray[i].get() shares[i] = self.GEArray[i].get()
try: try:
key=recoverSecret(shares) key=recoverSecret(shares)
plaintext = decryptMsg(ciphertext,key) plaintext = decryptMsg(self.ciphertext,key)
except: except:
messagebox.showerror("Error", "Decryption Failed!!! Please insert the correct shared keys!") messagebox.showerror("Decryption Failed!", "Please enter the correct shared keys!")
return return
for i in range(self.numOfShares): for i in range(self.numOfShares):
shares[i] = self.GEArray[i].config(state='disabled') shares[i] = self.GEArray[i].config(state='disabled')
@ -246,18 +261,16 @@ class GUI:
GL3 = Label(self.GetFrame, text="Found Secret Message") GL3 = Label(self.GetFrame, text="Found Secret Message")
GL3.grid(column=1, columnspan=2) GL3.grid(column=1, columnspan=2)
GTextFrame = Frame(self.GetFrame) GTextFrame = Frame(self.GetFrame)
GLMsg = Text(GTextFrame,height=10,width=50) self.GLMsg = Text(GTextFrame,height=10,width=50)
GLMsg.insert(INSERT, plaintext) self.GLMsg.insert(END, plaintext)
GLMsg.config(state='disabled') self.GLMsg.config(state='disabled')
GScroll = Scrollbar(GTextFrame) GScroll = Scrollbar(GTextFrame)
GScroll.config( command = GLMsg.yview ) GScroll.config( command = self.GLMsg.yview )
GLMsg.pack(side = LEFT) self.GLMsg.pack(side = LEFT)
GScroll.pack(side = RIGHT,fill = Y) GScroll.pack(side = RIGHT,fill = Y)
GTextFrame.grid(column=1,columnspan=2) GTextFrame.grid(column=1,columnspan=2)
self.GBackButton=Button(self.GetFrame,text="Back",command=self.Main) self.GBackButton=Button(self.GetFrame,text="Back",command=self.Main)
self.GBackButton.grid(column=1) self.GBackButton.grid(column=1)
root = Tk() root = Tk()
root.title("FloSecret") root.title("FloSecret")