Commit 258044b5 authored by Shashank Kumar's avatar Shashank Kumar

Encryption tools added

- Create key pair
- Display and manage key pair
- Encrypt a message
- Decrypt a message
parent 67647ae4
......@@ -4,11 +4,9 @@ This modules helps display and manage key pairs
import logging
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.popup import Popup
from modules.encryption.tools.all_tools.exceptions import GPGError
from modules.encryption.tools.all_tools.services import create_key_pair as ckp
......@@ -23,6 +21,13 @@ from modules.encryption.tools.all_tools.validations import (
Builder.load_file('./ui/encryption/all_tools/create_key_pair.kv')
class CreateKeyPairPopup(Popup):
'''
CreateKeyPairPopup displays popup to inform about key creation progress
'''
pass
class CreateKeyPair(BoxLayout):
'''
CreateKeyPair helps integrate UI with helper functions
......@@ -48,7 +53,7 @@ class CreateKeyPair(BoxLayout):
args[0]
)
self.ids[label].text = original_text
self.ids[label].color = [1, 1, 1, 1]
self.ids[label].color = [0, 0, 0, 1]
Clock.schedule_once(replace_label, 2)
def validate(self):
......@@ -103,21 +108,13 @@ class CreateKeyPair(BoxLayout):
'''
Processing user input to create key pair
'''
content_for_popup = BoxLayout(size_hint=(1, 0.8))
content_for_popup.add_widget(Label(text='Key creation in progress'))
popup = Popup(title='Test popup', content=content_for_popup,
auto_dismiss=False, size_hint=(None, None), size=(400, 300))
popup.children[0].children[0].orientation = 'vertical'
popup.children[0].children[0].add_widget(BoxLayout(size_hint=(1, 0.2)))
creating_key_pair_popup = CreateKeyPairPopup()
def add_button(*args):
'''
Replace original popup text and add button
Add Key fingerprint to label and button
'''
popup.children[0].children[0].children[1].children[0].text = args[0]
popup.children[0].children[0].children[0].add_widget(
Button(text='Okay', on_press=popup.dismiss)
)
creating_key_pair_popup.ids['create_key_pair_popup_label'].text = args[0]
if self.validate():
name = self.ids['input_name'].text
......@@ -126,7 +123,7 @@ class CreateKeyPair(BoxLayout):
expire_date = self.ids['input_expire_date'].text
try:
popup.open()
creating_key_pair_popup.open()
key_fingerprint = ckp(
name=name,
email=email,
......
......@@ -2,14 +2,124 @@
This modules helps decrypt a message using private key
'''
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.lang import Builder
from modules.encryption.tools.all_tools.services import list_all_keys, decrypt_message
from modules.encryption.tools.all_tools.utils import extract_key_uid_info
from modules.encryption.tools.all_tools.exceptions import GPGError
Builder.load_file('./ui/encryption/all_tools/decrypt_a_message.kv')
class KeyColumnBox(GridLayout):
'''
KeyColumnBox helps contain widgets and display them at the center of the box
'''
class DecryptKeyBox(BoxLayout):
'''
DecryptKeyBox helps contain widgets and display them at the center of the box
'''
class KeyInfoLabel(Label):
'''
KeyInfoLabel helps display key information
'''
class DisplayAndManageKeyPair(BoxLayout):
'''
DisplayAndManageKeyPair helps integrate UI with helper functions
'''
class DecryptMessagePopup(Popup):
'''
DecryptMessagePopup helps take encrypted message and passphrase and display
decrypted message on a popup
'''
class DecryptAMessage(BoxLayout):
'''
DecryptAMessage integrated UI with helper functions
'''
pass
def __init__(self, **kwargs):
super(DecryptAMessage, self).__init__(**kwargs)
all_keys = list_all_keys()
self.populate_with_keys(all_keys['all_private_keys'])
def populate_with_keys(self, all_private_keys):
'''
populate_with_keys displays all the available private keys that can be
used to decrypt message
'''
for fingerprint, key_info in all_private_keys.items():
user_info = extract_key_uid_info(key_info['uids'][0])
key_column_box = KeyColumnBox()
key_box = DecryptKeyBox()
key_box.ids['Key_button'].id = fingerprint
key_box.ids['Key_button'].bind(
on_press=self.decrypt_message_popup
)
key_column_box.add_widget(key_box)
key_info_label = KeyInfoLabel(
text='\n'.join(user_info.values())
)
key_column_box.add_widget(key_info_label)
fingerprint_text = fingerprint[:len(fingerprint) // 2] + '\n' +\
fingerprint[len(fingerprint) // 2:]
key_info_label = KeyInfoLabel(text=fingerprint_text)
key_column_box.add_widget(key_info_label)
self.ids['scroll_view_box'].add_widget(key_column_box)
@staticmethod
def decrypt_message_popup(*args):
'''
decrypt_message_popup allows user to decrypt a message using passphrase
and encrypted message
'''
decrypt_message_popup_object = DecryptMessagePopup()
def get_decrypted_message(*args):
'''
get_decrypted_message receives all the data from input box in order to
fetch decrypted message
'''
if args:
encrypted_message = decrypt_message_popup_object.ids['encrypted_message'].text
passphrase = decrypt_message_popup_object.ids['passphrase_input'].text
decrypt_message_object = decrypt_message_popup_object.ids['decrypted_message']
if not passphrase:
decrypt_message_object.text = 'Enter Passphrase'
elif encrypted_message:
try:
decrypted_message = decrypt_message(encrypted_message, passphrase)
if decrypted_message:
decrypt_message_object.text = decrypted_message
else:
decrypt_message_object.text = 'Incorrect Passphrase'
except GPGError as error:
decrypt_message_object.text = error.message
else:
decrypt_message_object.text = ''
decrypt_message_popup_object.ids['encrypted_message'].bind(
text=get_decrypted_message
)
decrypt_message_popup_object.ids['passphrase_input'].bind(
text=get_decrypted_message
)
if args:
decrypt_message_popup_object.open()
......@@ -2,14 +2,126 @@
This modules helps display and manage key pairs
'''
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.lang import Builder
from modules.encryption.tools.all_tools.services import list_all_keys, export_single_key
from modules.encryption.tools.all_tools.utils import extract_key_uid_info
Builder.load_file('./ui/encryption/all_tools/display_and_manage_key_pair.kv')
class KeyColumnBox(GridLayout):
'''
KeyColumnBox helps contain widgets and display them at the center of the box
'''
class KeyBox(BoxLayout):
'''
KeyBox helps contain widgets and display them at the center of the box
'''
class KeyInfoLabel(Label):
'''
KeyInfoLabel helps display key information
'''
class PublicKeyPopup(Popup):
'''
PublicKeyPopup helps display Public Key in UTF-8 inside a popup
'''
class PrivateKeyPopup(Popup):
'''
PrivateKeyPopup helps display Private Key in UTF-8 inside a popup
'''
class DisplayAndManageKeyPair(BoxLayout):
'''
DisplayAndManageKeyPair helps integrate UI with helper functions
'''
pass
def __init__(self, **kwargs):
super(DisplayAndManageKeyPair, self).__init__(**kwargs)
all_keys = list_all_keys()
self.populate_with_keys(
all_keys['all_public_keys'],
all_keys['all_private_keys']
)
def populate_with_keys(self, all_public_keys, all_private_keys):
'''
populate_with_keys displays all the available public and private keys
'''
for fingerprint, key_info in all_public_keys.items():
user_info = extract_key_uid_info(key_info['uids'][0])
key_column_box = KeyColumnBox()
key_info_label = KeyInfoLabel(
text='\n'.join(user_info.values())
)
key_column_box.add_widget(key_info_label)
fingerprint_text = fingerprint[:len(fingerprint) // 2] + '\n' +\
fingerprint[len(fingerprint) // 2:]
key_info_label = KeyInfoLabel(text=fingerprint_text)
key_column_box.add_widget(key_info_label)
key_box = KeyBox()
key_box.ids['Key_button'].id = fingerprint
key_box.ids['Key_button'].bind(
on_press=self.display_public_key
)
key_column_box.add_widget(key_box)
if fingerprint in all_private_keys.keys():
key_box = KeyBox()
key_box.ids['Key_button'].id = fingerprint
key_box.ids['Key_button'].bind(
on_press=self.display_private_key
)
key_column_box.add_widget(key_box)
self.ids['scroll_view_box'].add_widget(key_column_box)
@staticmethod
def display_public_key(*args):
'''
display_public_key helps display public key in plain text to the correspoding
key fingerprint
'''
fingerprint = args[0].id
public_key_popup = PublicKeyPopup()
public_key = export_single_key(fingerprint)
public_key_popup.ids['public_key_text_display'].text = public_key
public_key_popup.open()
@staticmethod
def display_private_key(*args):
'''
display_private_key helps display private key in plain text to the correspoding
key fingerprint and passphrase
'''
fingerprint = args[0].id
def get_private_key(*args):
'''
get_private_key calls helper function to get private key in plain text
'''
passphrase = args[1]
if passphrase:
private_key = export_single_key(fingerprint, passphrase)
if private_key:
private_key_popup.ids['private_key_text_display'].text = private_key
else:
private_key_popup.ids['private_key_text_display'].text = 'Incorrect Passphrase'
private_key_popup = PrivateKeyPopup()
private_key_popup.ids['private_key_passphrase_input'].bind(
text=get_private_key
)
private_key_popup.open()
......@@ -2,14 +2,107 @@
This module helps encrypt a message using public key
'''
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.lang import Builder
from modules.encryption.tools.all_tools.services import list_all_keys, encrypt_message
from modules.encryption.tools.all_tools.utils import extract_key_uid_info
Builder.load_file('./ui/encryption/all_tools/encrypt_a_message.kv')
class KeyColumnBox(GridLayout):
'''
KeyColumnBox helps contain widgets and display them at the center of the box
'''
class EncryptKeyBox(BoxLayout):
'''
EncryptKeyBox helps contain widgets and display them at the center of the box
'''
class KeyInfoLabel(Label):
'''
KeyInfoLabel helps display key information
'''
class DisplayAndManageKeyPair(BoxLayout):
'''
DisplayAndManageKeyPair helps integrate UI with helper functions
'''
class EncryptMessagePopup(Popup):
'''
EncryptMessagePopup helps encrypt plain text into encrypted message
'''
class EncryptAMessage(BoxLayout):
'''
DEncryptAMessage integrated UI with helper functions
EncryptAMessage integrated UI with helper functions
'''
pass
def __init__(self, **kwargs):
super(EncryptAMessage, self).__init__(**kwargs)
all_keys = list_all_keys()
self.populate_with_keys(all_keys['all_public_keys'])
def populate_with_keys(self, all_public_keys):
'''
populate_with_keys displays all available public keys
'''
for fingerprint, key_info in all_public_keys.items():
user_info = extract_key_uid_info(key_info['uids'][0])
key_column_box = KeyColumnBox()
key_box = EncryptKeyBox()
key_box.ids['Key_button'].id = fingerprint
key_box.ids['Key_button'].bind(
on_press=self.encrypt_message_popup
)
key_column_box.add_widget(key_box)
key_info_label = KeyInfoLabel(
text='\n'.join(user_info.values())
)
key_column_box.add_widget(key_info_label)
fingerprint_text = fingerprint[:len(fingerprint) // 2] + '\n' +\
fingerprint[len(fingerprint) // 2:]
key_info_label = KeyInfoLabel(text=fingerprint_text)
key_column_box.add_widget(key_info_label)
self.ids['scroll_view_box'].add_widget(key_column_box)
@staticmethod
def encrypt_message_popup(*args):
'''
encrypt_message_popup allows user to encrypt a message from plain text
'''
fingerprint = args[0].id
encrypt_message_popup_object = EncryptMessagePopup()
def get_encrypted_message(*args):
'''
get_encrypted_message receives all the data from input box in order to
fetch encrypted message
'''
plain_message = args[1]
if plain_message:
encrypted_message = encrypt_message(fingerprint, plain_message)
encrypt_message_popup_object.ids['encrypted_message'].text = encrypted_message
else:
encrypt_message_popup_object.ids['encrypted_message'].text = ''
encrypt_message_popup_object.ids['plain_message'].bind(
text=get_encrypted_message
)
encrypt_message_popup_object.open()
......@@ -70,9 +70,7 @@ def encrypt_message(keyid, message):
Encrypting a message using a public key
'''
gpg = create_gnupg_object()
encrypted_message_object = gpg.encrypt(message, keyid)
if encrypted_message_object.ok:
return str(encrypted_message_object)
else:
......@@ -84,7 +82,6 @@ def decrypt_message(message, passphrase):
Decrypting a message using private key and passphrase
'''
gpg = create_gnupg_object()
decrypted_message_object = gpg.decrypt(message, passphrase=passphrase)
if decrypted_message_object.ok:
return str(decrypted_message_object)
......
'''
Utility functions for encryption.tools module
'''
import re
def clean_email(user_email):
'''
clean_email removes unnecessary spaces from Full Name
......@@ -14,3 +17,22 @@ def clean_name(name):
'''
name = ' '.join(name.split())
return name.strip('\t\n\r ')
def extract_key_uid_info(uid):
'''
extract_key_uid_info returns Name, Email and Comment from the key uid
'''
name = uid.split('(')[0].strip(' ')
email_pattern = re.compile(r'\<(.)*\>')
email = email_pattern.search(uid).group().strip('<> ')
comment_pattern = re.compile(r'\([\w\s]*\)')
comment = comment_pattern.search(uid).group().strip('() ')
return {
'name': name,
'email': email,
'comment': comment,
}
......@@ -105,6 +105,7 @@ class SignUp(BoxLayout, Screen):
Prompting error message to the user otherwise
'''
app_object = args[0]
app_object.switch_screen_to_dashboard()
if self.validate():
email = self.ids['user_email'].text
password = self.ids['first_pass'].text
......
<CreateKeyPair>:
<CreateKeyPair>
size_hint: 1, 0.9
orientation: 'vertical'
InputBox:
InputLabel:
InputBox
InputLabel
text: 'Name'
id: label_name
InputField:
InputField
id: input_name
InputBox:
InputLabel:
InputBox
InputLabel
text: 'Email'
id: label_email
InputField:
InputField
id: input_email
InputBox:
InputLabel:
InputBox
InputLabel
text: 'Comment'
id: label_comment
InputField:
InputField
id: input_comment
InputBox:
InputLabel:
InputBox
InputLabel
text: 'Expire Date'
id: label_expire_date
InputField:
InputField
id: input_expire_date
hint_text: 'YYYY-MM-DD'
BoxLayout:
BoxLayout
size_hint: 1, 2
padding: 0, 0, 0, 20
FloatLayout:
Button:
FloatLayout
Button
text_size: self.size
size: self.texture_size
size_hint: 0.7, 0.5
......@@ -38,18 +38,18 @@
background_normal: ''
background_color: 0, 0, 0, 1
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
font_size: 20
font_size: dp(20)
halign: 'center'
valign: 'middle'
on_press: root.create_key_pair()
FloatLayout:
FloatLayout
<InputBox@BoxLayout>:
<InputBox@BoxLayout>
padding: 0, 0, 50, 10
spacing: 10
size_hint: 1, 2
<InputLabel@Label>:
<InputLabel@Label>
size_hint: 0.3, 1
text_size: self.size
size: self.texture_size
......@@ -57,11 +57,23 @@
halign: 'right'
padding_y: 10
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
font_size: 20
font_size: dp(20)
color: 0, 0, 0, 1
<InputField@TextInput>:
<InputField@TextInput>
size_hint: 0.7, 0.5
multiline: False
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
font_size: 18
font_size: dp(18)
halign: 'right'
<CreateKeyPairPopup>
title: 'Creating Key Pair'
size_hint: (None, None)
size: (450, 300)
BoxLayout
orientation: 'vertical'
Label
id: create_key_pair_popup_label
text: 'Key creation in progress'
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
<DecryptAMessage>:
Label:
text: 'Decrypt A Message'
\ No newline at end of file
<DecryptAMessage>
size_hint: 1, 0.9
ScrollView
size_hint: None, None
size: root.size
GridLayout
id: scroll_view_box
padding: dp(0)
spacing: dp(0)
cols: 1
size_hint: 1, None
height: self.minimum_height
orientation: 'vertical'
KeyColumnBox
padding: dp(0)
spacing: dp(0)
DecryptMessageHeaderLabel
text: 'Select Key To Decrypt'
DecryptMessageHeaderLabel
text: 'UID'
DecryptMessageHeaderLabel
text: 'Fingerprint'
<KeyColumnBox>
orientation: 'vertical'
padding: (10, 25, 10, 25)
size_hint_y: None
cols: 4
<DecryptKeyBox>
FloatLayout
DecryptMessageButton
id: Key_button
text: 'Decrypt Using This Private Key'
FloatLayout
<DecryptMessageHeaderLabel@Label>
color: 0, 0, 0, 1
font_size: dp(20)
markup: True
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
<KeyInfoLabel>
color: 0, 0, 0, 1
font_size: dp(12)
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
<DecryptMessageButton@Button>
size_hint_x: None
width: 300
<DecryptMessagePopup>
title: 'Decrypt Message Using Public Key'
size_hint: (None, None)
size: (700, 450)
BoxLayout
orientation: 'vertical'
BoxLayout
size_hint: (1, 0.1)
Label
text: 'Enter Passphrase'
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
font_size: dp(20)
TextInput
id: passphrase_input
password: True
BoxLayout
padding: [0, 20, 0, 0]
BoxLayout
size_hint: (0.5, 1)
orientation: 'vertical'
BoxLayout
size_hint: (1, 0.1)
Label
text: 'Enter Your Message'
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
font_size: dp(20)
BoxLayout
size_hint: (1, 0.9)
TextInput
id: encrypted_message
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
BoxLayout
size_hint: (0.5, 1)
orientation: 'vertical'
BoxLayout
size_hint: (1, 0.1)
Label
text: 'Decrypted Message'
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
font_size: dp(20)
BoxLayout
size_hint: (1, 0.9)
TextInput
id: decrypted_message
font_name: 'ui/assets/fonts/VarelaRound-Regular.ttf'
readonly: True
<DisplayAndManageKeyPair>:
<DisplayAndManageKeyPair>
size_hint: 1, 0.9
Label:
text: 'Display and manage key pair'
ScrollView
size_hint: None, None
size: root.size
GridLayout
id: scroll_view_box
padding: dp(0)
spacing: dp(0)
cols: 1
size_hint: 1, None
height: self.minimum_height
orientation: 'vertical'
KeyColumnBox
padding: dp(0)
spacing: dp(0)
KeyHeaderLabel
text: 'UID'
KeyHeaderLabel
text: 'Fingerprint'
KeyHeaderLabel
text: 'Public Key'
KeyHeaderLabel
text: 'Private Key'