This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndv_cypher.py
167 lines (131 loc) · 4.61 KB
/
ndv_cypher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
Module containing classes for encryption.
classes:
-VermamEncrypt
-VernamDecrypt
Usage example:
>>> encrypted_text, key = VernamEncrypt.encrypt("sample text")
"""
import random
"""
!/usr/bin/env python
-*- coding: utf-8 -*-
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
"""
__author__ = "Nicholas De Villiers"
__copyright__ = "Copyright (C) 2017 Nicholas De Villiers"
__license__ = "Public Domain"
__version__ = "1.0"
class VernamEncrypt:
"""
Class for encryption using a vernam cypher.
Methods:
-encrypt(cls, text) [CLASS METHOD]
-_gen_char(val) [STATIC METHOD]
Usage:
>>> encrypted_text, key = VernamEncrypt.encrypt("sample text")
"""
def __init__(self):
pass
@classmethod
def encrypt(cls, text):
"""
Encrypts any given text using a vernam cypher.
Args taken:
-text (str)
Returns encr_text (str) and encr_key (str).
Usage:
>>> encrypt = VernamEncrypt
>>> encr_text, key = encrypt.encrypt("sample text")
"""
plain_ascii = []
key = []
encrypt_list = []
for char in text:
asc = ord(char) # converts character to ascii value
asc = int(asc) # converts ascii value to int
plain_ascii.append(asc)
for val in plain_ascii:
new_value, shift = cls._gen_char(val) # returns shifted letter and key
# below: shifted char and key converted to string
new_value = str(new_value)
shift = str(shift)
# values appended to list
key.append(shift)
encrypt_list.append(new_value)
# lists converted to string
encr_text = str(("".join(encrypt_list)))
encr_key = (" ".join(key))
return encr_text, encr_key # ciphertext and key(s) returned as strings
@staticmethod
def _gen_char(val):
"""
Private method - shifts a character by a random amount of bits.
Args taken: val (str)
Returns new_value (str) and shift (int).
Can only be called by other methods in class/instance.
"""
shift = 0
new_value = ""
valid = False
while not valid:
shift = random.randint((0 - val), (127 - val)) # shift is random
new_value = chr(val + shift)
if 32 <= ord(new_value) < 127 and ord(new_value) != 92: # ensures that all new values are actual chars
valid = True
return new_value, shift
class VernamDecrypt:
"""
Class for decryption using a vernam cypher.
Methods:
-decrypt(cipher, keytext) [CLASS METHOD]
-__use_input(cipher) [STATIC METHOD]
Usage:
>>> encrypted_text, key = VernamEncrypt.encrypt("sample text")
"""
def __init__(self):
pass
@staticmethod
def _user_input(cipher):
"""
Private method - converts ciphertext into list of ASCII values.
Args taken:
-cipher (str)
Returns cipher_list (list).
Can only be called by other methods in class/instance.
"""
cipher_list = []
for x in cipher:
cipher_list.append(ord(x)) # converts input to a list of ascii values
return cipher_list
@classmethod
def decrypt(cls, cipher, keytext):
"""
Decrypts given text using a vernam cypher, with provided key.
Args taken:
-cipher (str)
-keytext (str)
Returns plain_text(str).
Usage:
>>> cipher = VernamDecrypt
>>> text = cipher.decrypt("kabno", "3 -4 -10 2 0")
"""
cipher_list = cls._user_input(cipher)
plain_list = []
key = str(keytext).split() # splits key string into list values
for x in range(0, len(key)):
new_val = chr(int(cipher_list[x]) - int(key[x])) # shift is reversed
plain_list.append(new_val) # reverse shifted values added to list
plain_text = "".join(plain_list) # decrypted characters joined as a string
cipher_list.clear()
plain_list.clear()
return plain_text