-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestingSolve.js
134 lines (98 loc) · 3.25 KB
/
testingSolve.js
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
//solve it
let todaysmessage = "b~f`);q~hkVgR)dS6/$-vkj=/g__,9p:";
for (let i = 100000; i < 999999; i++) {
let message = decrypt(todaysmessage,i);
if ( message.charAt(0)=="S" && message.charAt(1)=="e" && message.charAt(2)=="p" && message.charAt(3)=="t" && message.charAt(4)=="e" && message.charAt(5)=="m" ) {
console.log(message);
};
if (i % 100000 == 0){
console.log(i);
};
};
//FUNCTIONS
//IMPORTANT Use these functions to encrypt or decrypt a string (of lowercase letters or numbers or space) with a specific key (which is a number)
//turn plainText into cipherText. input (string, number)
function encrypt(plainText, key) {
let cipherText = getCharCodes(plainText);
cipherText = toLowerNumbers(cipherText);
cipherText = encryptionAction(cipherText, key);
cipherText = toHigherNumbers(cipherText);
cipherText = arrayToString(cipherText);
return cipherText;
};
//turn cipherText into plainText. input (string, number)
function decrypt(cipherText, key) {
let plainText = getCharCodes(cipherText);
plainText = toLowerNumbers(plainText);
plainText = decryptionAction(plainText, key);
plainText = toHigherNumbers(plainText);
plainText = arrayToString(plainText);
return plainText;
};
//The rest of these functions are subset functions of encrypt and decrypt
//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)
function toLowerNumbers(array) {
var newArray = [];
for (var i = 0; i < array.length; i++) {
newArray.push(array[i] - 32);
};
return newArray;
};
//turn 0-26 back to character codes. input(array)
function toHigherNumbers(array) {
var newArray = [];
for (var i = 0; i < array.length; i++) {
newArray.push(array[i] + 32);
};
return newArray;
};
//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;
};