-
Notifications
You must be signed in to change notification settings - Fork 0
/
DumbCipher.py
83 lines (62 loc) · 2 KB
/
DumbCipher.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
#!/usr/bin/env python
# coding: utf-8
# In[12]:
###############################
# Creates a naively encoded cipher|key pair.
# The message is encoded by randomy hiding its characters in an ascii + ' '
# random text.
# The key counterpart is created the same way but does not contain any
# 'o' or 'O'.
# These char are to be considered as circles indicating the message's char
# positions.
# A simple reveal is to select the whole text and then drag the selected part
# so that the right (key) selected part overlaps the left (cipher) selected part.
#
# author : luminilion
###############################
import string
import random
import numpy as np
def create_text(secret, alphabet, indicator, k_alphabet) :
#print(secret)
#print(alphabet)
#print(indicator)
#print(k_alphabet)
## Metrics
length = len(secret)*2
width = 20
size = width*length
print('Message of size', length, 'x', width)
## Creation of the letters placement
places = np.arange(size)
np.random.shuffle(places)
places = places[:len(secret)]
places = np.sort(places)
## This is the letters placement
#print(list(zip(places, secret)))
msg = ''
for i in range (0, length) :
code = ''
key = ''
for j in range (0, width) :
if (len(places) > 0) and (places[0] == i*width + j) :
code += secret[0]
secret = secret[1:]
key += random.choice(indicator)
places = places[1:]
else :
code += random.choice(alphabet)
key += random.choice(k_alphabet)
msg = msg + code + '|' + key + '\n'
return msg
#######
secret = "This is a secret"
alphabet = string.ascii_letters + ' '
indicator = 'oO'
k_alphabet = alphabet.replace('o', '')
k_alphabet = k_alphabet.replace('O', '')
print(create_text(secret, alphabet, indicator, k_alphabet))
print()
alphabet = '_'
k_alphabet = '_'
print(create_text(secret, alphabet, indicator, k_alphabet))