Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
5x committed Mar 25, 2020
1 parent afcd02c commit a65539c
Show file tree
Hide file tree
Showing 28 changed files with 1,492 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Type '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. Windows 10]
- Python/Tk Version

**Additional context**
Add any other context about the problem here.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
123 changes: 123 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 5x

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# cryptography-gui-app

An application for encrypting/decrypting text with few symmetric/asymmetric crypto algorithms.

Features:
- Multi alphabet crypt support (English, Ukraine, Russian);
- Symmetric/Asymmetric mode;
- Decrypt brute force iterator;
- Native Tk GUI.

Ciphers:
- Caesar cipher;
- Trithemius cipher (Linear equation/Not linear equation/ slogan);
- Gamma cipher (PRNG based);
- DES (mode CFB);
- Asymmetric cipher (public/private key realization).

## Build Windows Application

To create runnable execute application use next:
```bash
pyinstaller --onefile --windowed --name cryptography-gui-app app.py
```
[More info about PyInstaller](https://www.pyinstaller.org/)

## Requirements

* Python 3.4 or later
* pycryptodome
* pyinstaller

## Application screenshots
![Application screenshot](assets/screenshots.png)

## Support & Contributing
Anyone is welcome to contribute. If you decide to get involved, please take a moment and check out the following:

* [Bug reports](.github/ISSUE_TEMPLATE/bug_report.md)
* [Feature requests](.github/ISSUE_TEMPLATE/feature_request.md)

## License

The code is available under the [MIT license](LICENSE).
3 changes: 3 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/python3,4
# -*- coding: utf-8 -*-
"""cryptography-gui-app"""
5 changes: 5 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from gui import MainWindow

if __name__ == "__main__":
main = MainWindow()
main.mainloop()
Binary file added assets/screenshots.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions crypt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from .caesar_cipher import CaesarCipher
from .decrypt_iter import DecryptIter
from .trithemius_cipher import TrithemiusCipher, TrithemiusLinearEquation,\
TrithemiusAssocReplace, LinearEquationException
from .gamma_cipher import GammaCipher
from .des_cipher import DESCipher
from .asymmetric import AsymmetricCipher


__all__ = [
"CaesarCipher",
"DecryptIter",
"TrithemiusCipher",
"TrithemiusLinearEquation",
"TrithemiusAssocReplace",
"LinearEquationException",
"GammaCipher",
"DESCipher",
"AsymmetricCipher",
]
16 changes: 16 additions & 0 deletions crypt/alphabet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from string import ascii_lowercase, ascii_uppercase, digits, punctuation

whitespace = " \t"
EN = (ascii_lowercase, ascii_uppercase, digits, punctuation, whitespace)
RU = ("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ", "абвгдеёжзийклмнопрстуфхцчшщъыьэюя", digits, punctuation, whitespace)
UA = ("АБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЭЮЯ", "абвгґдеєжзиіїйклмнопрстуфхцчшщьэюя", digits, punctuation, whitespace)


def get_alphabet(lang=None):
alphabets = {
"EN": EN,
"RU": RU,
"UA": UA
}

return alphabets.get(lang, EN)
57 changes: 57 additions & 0 deletions crypt/asymmetric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from crypt.utils.number_theory_algs import extended_gcd
from crypt.utils.bitstr import str_to_bits, bits_to_str, positive_align_str
from crypt.utils.knapsack import knapsack


class AsymmetricCipher(object):
def encrypt(self, plain_text, public_seq):
block_size = len(public_seq)
text = str_to_bits(plain_text)
text = positive_align_str(text, block_size)

blocks = tuple(zip(*[iter(text)] * block_size))

crt_values = []
for index, block in enumerate(blocks):
total_weight = self._block_total_weight(block, public_seq)
crt_values.append(total_weight)

return crt_values

def decrypt(self, cipher_text, private_seq, m, t):
_, _, neg_t = extended_gcd(m, t)

plain_bits = []
for num in cipher_text:
value = (neg_t * num) % m
bits_block = knapsack(private_seq, value)
plain_bits.append(bits_block)

plain_text = bits_to_str(plain_bits)

return plain_text

@staticmethod
def gen_public_key(secret_sec, t, m):
return [(t * i) % m for i in secret_sec]

def _block_total_weight(self, bits, weight_values):
return sum(w for w, bit in zip(weight_values, bits) if bit == "1")


if __name__ == "__main__":
test_data = """The quick brown fox jumps over the lazy dog."""
secret_key = [1, 3, 5, 11, 21, 44, 87, 175, 349, 701]
s = 1590
m = 43

cipher = AsymmetricCipher()

# eq [43, 129, 215, 473, 903, 302, 561, 1165, 697, 1523]
public_key = AsymmetricCipher.gen_public_key(secret_key, m, s)
crt_text = cipher.encrypt(test_data, public_key)
plain_text = cipher.decrypt(crt_text, secret_key, s, m)

print(public_key)
print(crt_text)
print(plain_text)
49 changes: 49 additions & 0 deletions crypt/caesar_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from crypt.alphabet import get_alphabet
from crypt.cipher_abc import CipherABC


class CaesarCipher(CipherABC):
def __init__(self, key, alphabet='EN'):
super().__init__(key)
self.symbols_collection = get_alphabet(alphabet)

@CipherABC.key.setter
def key(self, value):
self._key = int(value)

def encrypt(self, plain_text):
return self._shift_msg(self._key, plain_text)

def decrypt(self, cipher_text):
return self._shift_msg(-self._key, cipher_text)

def _shift_msg(self, key, message):
shift_map = self._shift_map(key)
shifted_msg = (shift_map.get(char, char) for char in message)

return "".join(shifted_msg)

def _shift_map(self, shift):
assoc_map = {}

for symbols in self.symbols_collection:
symbols_len = len(symbols)

if symbols_len == 0:
continue

for j in range(symbols_len):
in_char = symbols[j]
shifted = (j + shift) % symbols_len
assoc_map[in_char] = symbols[shifted]

return assoc_map


if __name__ == "__main__":
cipher = CaesarCipher("4")
crt_text = cipher.encrypt("the quick brown fox jumps over the lazy dog.")
plain_text = cipher.decrypt(crt_text)

print(crt_text)
print(plain_text)
Loading

0 comments on commit a65539c

Please sign in to comment.