-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncryptionWebsite.html
127 lines (92 loc) · 3.03 KB
/
EncryptionWebsite.html
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
<!DOCTYPE html>
<html>
<body>
<h2>Super Top Secret Soviet Encryption</h2>
<p>Enter plain text and the key to encrypt.</p>
Plain Text: <input type="text" id="plainText" size="200">
<br><br>
Key: <input type="number" id="key1">
<br><br>
<button onclick="encrypt()">Encrypt</button>
<p id="cipherText"></p>
<script>
function encrypt() {
var plainText = document.getElementById('plainText').value;
var key1 = document.getElementById('key1').value;
plainText = plainText.toString();
let cipherText = getCharCodes(plainText);
cipherText = toLowerNumbers(cipherText);
cipherText = encryptionAction(cipherText, key1);
cipherText = toHigherNumbers(cipherText);
cipherText = arrayToString(cipherText);
document.getElementById('cipherText').innerHTML = cipherText;
};
//turn cipherText into plainText. input (string, number)
function decrypt() {
var cipherText = document.getElementById('cipherText').value;
var key2 = document.getElementById('key').value;
cipherText = cipherText.toString();
let plainText = getCharCodes(cipherText);
plainText = toLowerNumbers(plainText);
plainText = decryptionAction(plainText, key2);
plainText = toHigherNumbers(plainText);
plainText = arrayToString(plainText);
document.getElementById('plainText').innerHTML = plainText;
};
//modulus that gives positives. input(number, number)
function mod(n, m) {
return ((n % m) + m) % m;
};
//turn plainText into array of Character codes. input(string)
function getCharCodes(plainText) {
var charCodes = [];
for (var i = 0; i < plainText.length; i++) {
charCodes.push(plainText.charCodeAt(i));
}
return charCodes;
};
//turn array of character codes into 0-26. input(array)
const toLowerNumbers = a => a.map(b => b - 32);
//turn 0-26 back to character codes. input(array)
const toHigherNumbers = a => a.map(b => b + 32);
//turn an array of character codes into string. input(array)
function arrayToString(array) {
return array.map(function(code) {
return String.fromCharCode(code);
}).join('');
}
//turn plainText array into cipherText array. input(array, number)
function encryptionAction(array, key) {
let keyString = key.toString();
let keyNumbers = getCharCodes(keyString);
var keyLength = keyNumbers.length;
var encryptedArray = [];
for (let i = 0; i < array.length; i++){
for (let j = 0; j < keyLength; j++){
if (i%keyLength==j) {
var newEntry = ( (array[i] + (10*keyNumbers[j])) % 95 )
encryptedArray.push(newEntry);
};
};
};
return encryptedArray;
};
//turn cipherText array into plainText array. input(array, number)
function decryptionAction(array, key) {
let keyString = key.toString();
let keyNumbers = getCharCodes(keyString);
var keyLength = keyNumbers.length;
var decryptedArray = [];
for (let i = 0; i < array.length; i++){
for (let j = 0; j < keyLength; j++){
if (i%keyLength==j) {
var newEntry = mod(array[i] - (10*keyNumbers[j]), 95 )
decryptedArray.push(newEntry);
};
};
};
return decryptedArray;
};
</script>
</body>
</html>