-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMicrobit_Encryption.js
More file actions
169 lines (156 loc) · 5.6 KB
/
Microbit_Encryption.js
File metadata and controls
169 lines (156 loc) · 5.6 KB
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
168
169
enum MethodOfEncryption {
//% block="Caesar"
Caesar,
//% block="RSA"
RSA,
}
namespace custom {
let p = 23;
let g = 5;
const radioChannel = 100;
let connected = true;
let secrectKey: number;
let listening = false;
//% block
export function WaitForHost() {
radio.setGroup(radioChannel);
let waiting = true;
while (waiting) {
basic.showString("W4H");
radio.onReceivedString(function (receivedString: string) {
if (receivedString == "Pair Please") {
radio.sendString("Let's Connect");
waiting = false;
CalculateKeyNew("Client");
}
})
}
}
//% block
export function WaitForRecipent() {
radio.setGroup(radioChannel);
let pinging = true;
//Keep broadcasting a signal untill a response has been made.
while (pinging) {
basic.showString("W4R");
radio.sendString("Pair Please");
basic.pause(100);
radio.onReceivedString(function (receivedString: string) {
if (receivedString == "Let's Connect") {
pinging = false;
CalculateKeyNew("Host");
}
})
}
}
function CalculateKeyNew(type: string) {
//if youre the host then...
if (type == "Host") {
let a = Math.ceil(Math.random() * 25);
radio.sendValue("A", Math.pow(g, a) % p);
//basic.showString("SN = " + Math.pow(g, a) % p);
radio.onReceivedValue(function (name: string, value: number) {
if (name == "B") {
secrectKey = (Math.pow(value, a) % p);
basic.showNumber(secrectKey);
connected = true;
ListenForMessage();
}
})
//if youre not then...
} else {
//once you get a calulate B and send it back.
let b = Math.floor(Math.random() * 25);
radio.onReceivedValue(function (name: string, value: number) {
if (name == "A") {
let B = (Math.pow(g, b) % p);
radio.sendValue("B", B);
secrectKey = (Math.pow(value, b) % p);
basic.showNumber(secrectKey);
connected = true;
ListenForMessage();
}
})
}
}
let alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
//%block
export function Encrypt(message: string, EncryptionType: MethodOfEncryption) {
let encryptedMessage: string = "";
switch (EncryptionType) {
case MethodOfEncryption.Caesar:
let shift = 10;
for (let i = 0; i < message.length; i++) {
let index: number;
for (let x = 0; x < alphabet.length; x++) {
if (alphabet[x] == message.charAt(i)) {
index = x;
break;
}
}
let newValue = index + shift;
if (newValue > alphabet.length) {
newValue = (newValue - alphabet.length);
}
encryptedMessage = encryptedMessage + alphabet[newValue];
}
case MethodOfEncryption.RSA:
break;
}
return encryptedMessage;
}
//%block
export function decrypt(message: string, EncryptionType: MethodOfEncryption) {
let decryptedMessage: string = "";
switch (EncryptionType) {
case MethodOfEncryption.Caesar:
let shift = 10;
for (let i = 0; i < message.length; i++) {
let index: number;
for (let x = 0; x < alphabet.length; x++) {
if (alphabet[x] == message.charAt(i)) {
index = x;
break;
}
}
let newValue = index - shift;
if (newValue < 0) {
newValue = (alphabet.length + newValue);
}
decryptedMessage = decryptedMessage + alphabet[newValue];
}
break;
case MethodOfEncryption.RSA:
break;
}
return decryptedMessage;
}
//%block
export function SendEncryptedMessage(message: string, method: MethodOfEncryption) {
let listening = false;
switch (method) {
case MethodOfEncryption.Caesar:
radio.sendValue(Encrypt(message, MethodOfEncryption.Caesar), 101);
let listening = true;
break;
case MethodOfEncryption.RSA:
break;
}
//101 means ceasar.
}
//%block
export function ListenForMessage() {
basic.showString("LSN")
radio.onReceivedValue(function (name: string, value: number) {
basic.showString("RCD")
if (value == 101) {
basic.showString(decrypt(name, MethodOfEncryption.Caesar));
} else {
if (value == 102) {
basic.showString(decrypt(name, MethodOfEncryption.RSA));
}
}
ListenForMessage();
})
}
}