Commit fc05a777 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
[
{"application_settings": 1},
{"blog": 1},
{"cli": 1},
{"communication": 1},
{"application_settings": []},
{"blog": []},
{"cli": []},
{"communication": []},
{"encryption":[
{
"tutorials": 0
"tutorials": []
},
{
"tools": [
......@@ -27,9 +27,9 @@
}
]
},
{"how_to_use": 1},
{"profile_settings": 1},
{"theme_settings": 1},
{"vcs": 1},
{"way_ahead": 1}
{"how_to_use": []},
{"profile_settings": []},
{"theme_settings": []},
{"vcs": []},
{"way_ahead": []}
]
'''
Modules containing Encryption classes
'''
from copy import copy as cp
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
......@@ -37,7 +35,8 @@ class Encryption(BoxLayout):
self.ids[option_to_enable + '_box'].canvas.before.children[0].rgba = [1, 1, 1, 1]
self.ids[option_to_enable].color = (0, 0, 0, 1)
all_options_items_copy = cp(self.all_options_items)
# removing enabled option from the list in order to disable all other options
all_options_items_copy = self.all_options_items[:]
all_options_items_copy.remove(option_to_enable)
for option in all_options_items_copy:
......
......@@ -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,134 @@
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 NoKeyPresentDecrypt(BoxLayout):
'''
NoKeyPresentDecrypt will be displayed in case if no key is present
'''
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
'''
if all_private_keys.items():
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)
else:
self.ids['scroll_view_box'].clear_widgets()
self.ids['scroll_view_box'].add_widget(NoKeyPresentDecrypt())
@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,139 @@
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 NoKeyPresent(BoxLayout):
'''
NoKeyPresent will be displayed in case if no key is present
'''
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
'''
if all_public_keys.items():
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)
# Spliting fingerprint to multiple lines to make it better on the UI
fingerprint_length_by_half = len(fingerprint) // 2
fingerprint_text = fingerprint[:fingerprint_length_by_half] + '\n' +\
fingerprint[fingerprint_length_by_half:]
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)
else:
self.ids['scroll_view_box'].clear_widgets()
self.ids['scroll_view_box'].add_widget(NoKeyPresent())
@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,119 @@
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 NoKeyPresentEncrypt(BoxLayout):
'''
NoKeyPresentEncrypt will be displayed in case if no key is present
'''
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
'''
if all_public_keys.items():
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)
# Spliting fingerprint to multiple lines to make it better on the UI
fingerprint_length_by_half = len(fingerprint) // 2
fingerprint_text = fingerprint[:fingerprint_length_by_half] + '\n' +\
fingerprint[fingerprint_length_by_half:]
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)
else:
self.ids['scroll_view_box'].clear_widgets()
self.ids['scroll_view_box'].add_widget(NoKeyPresentEncrypt())
@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()
......@@ -38,8 +38,7 @@ def create_key_pair(name, email, comment, expire_date, passphrase=''):
key = gpg.gen_key(input_data)
if key.fingerprint:
return key.fingerprint
else:
raise GPGError(message=getattr(key, 'stderr'))
raise GPGError(message=getattr(key, 'stderr'))
def list_all_keys():
......@@ -70,13 +69,10 @@ 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:
raise GPGError(message=encrypted_message_object.status)
raise GPGError(message=encrypted_message_object.status)
def decrypt_message(message, passphrase):
......@@ -84,9 +80,7 @@ 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)
else:
raise GPGError(message=decrypted_message_object.status)
raise GPGError(message=decrypted_message_object.status)
'''
Utility functions for encryption.tools module
'''
def clean_email(user_email):
'''
clean_email removes unnecessary spaces from Full Name
......@@ -14,3 +16,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(' ')
name = clean_name(name)
email = uid.split('<')[1].split('>')[0]
email = clean_email(email)
comment = uid.split('(')[1].split(')')[0]
return {
'name': name,
'email': email,
'comment': comment,
}
<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