-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency.html
More file actions
262 lines (215 loc) · 10 KB
/
frequency.html
File metadata and controls
262 lines (215 loc) · 10 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<!doctype html>
<html>
<head>
<title>Encryption</title>
<style>
</style>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<div id="app">
<template>
<v-app id="inspire">
<v-content>
<v-container fluid fill-height>
<v-layout justify-center>
<v-flex xs12 md8 lg6 class="text-xs-center">
<div class="display-3 my-5"> Frequency Analysis</div>
<div class="white pa-5 title font-weight-light">
<div class="display-1 font-weight-thin">Insert plain text or <v-btn class="" id="convertButton"
@click="longPlainText()">Long Plain Text</v-btn>
</div>
<div>
<v-textarea outline single-line class="pa-2" type="text" id="plainText" v-model="startText" @keyup="update()"></v-textarea>
</div>
<div class="headline font-weight-thin pa-3">Plain text letter frequency</div>
<div class="px-5 pl-2">
<svg height="100" width="100%" id="plain">
<g transform="translate(0, 0)">
<rect :x="startingX(index)" y="0" width="3.8%" :height="f * 100" style="fill:blue;stroke:pink;stroke-width:1;"
v-for="(f, index) in frequencyPlain()" />
</g>
</svg>
</div>
<div class="display-1 font-weight-thin">Insert key or <v-btn class="" id="convertButton" @click="oneTimePad()">One
Time Pad</v-btn>
</div>
<div class="pa-3">
<v-text-field outline single-line class="px-5" type="text" id="keyString" v-model="uncleanKeyString"
@keyup="update()"></v-text-field>
</div>
<div class="headline font-weight-thin pa-2">Cipher text</div>
<div>
<p id="encryptedCharacters" class="headline text-truncate">{{encryptedCharacters}}</p>
</div>
<div class="headline font-weight-thin pa-2">Cipher text letter frequency</div>
<div class="px-5 pl-2">
<svg height="100" width="100%">
<g transform="translate(0, 0)">
<rect :x="startingX(index)" y="0" width="3.8%" :height="f * 100" style="fill:blue;stroke:pink;stroke-width:1;"
v-for="(f, index) in frequencyCipher()" />
</g>
</svg>
</div>
<div class="display-1 font-weight-thin">Key length guess </div>
<div class="pa-3">
<v-slider min=1 thumb-label="always" v-model="lengthGuess"></v-text-field>
</div>
<div class="headline font-weight-thin pa-2">Cipher text frequency at that length</div>
<div class="px-5 pl-2">
<svg height="100" width="100%">
<g transform="translate(0, 0)">
<rect :x="startingX(index)" y="0" width="3.8%" :height="f * 100" style="fill:blue;stroke:pink;stroke-width:1;"
v-for="(f, index) in frequencyGuess()" />
</g>
</svg>
</div>
</div>
</v-flex>
</v-layout>
</v-container>
</v-content>
<v-footer color="indigo" app>
<span class="white--text">© 2018</span>
</v-footer>
</v-app>
</template>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src='https://unpkg.com/vue'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js'></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.js"></script>
<script>
var base = "a".charCodeAt(0);
var app = new Vue({
el: '#app',
vuetify: new Vuetify(),
data: {
drawer: null,
source: "",
rows: [],
startText: "Starting Off Text",
endText: "",
uncleanKeyString: "d",
keyString: "",
keyArray: [],
numeric: [],
encryptedNumbers: [],
encryptedCharacters: "",
cleanText: "",
lengthGuess: 1,
},
computed: {
},
methods: {
startingX(c) {
let baseWidth = ($("#plain").width()) / 26;
return baseWidth * c;
},
frequencyPlain() {
let array = _.groupBy(this.numeric);
for (let i = 0; i < 26; i++) {
if (!array[i]) {
array[i] = [];
}
}
let lengths = _.map(array, i => i.length);
let m = Math.max.apply(null, lengths);
let toReturn = [];
for (let i = 0; i < 26; i++) {
toReturn.push(lengths[i] / m);
}
return toReturn;
},
frequencyCipher() {
let array = _.groupBy(this.encryptedNumbers);
for (let i = 0; i < 26; i++) {
if (!array[i]) {
array[i] = [];
}
}
let lengths = _.map(array, i => i.length);
let m = Math.max.apply(null, lengths);
let toReturn = [];
for (let i = 0; i < 26; i++) {
toReturn.push(lengths[i] / m);
}
return toReturn;
},
frequencyGuess() {
let guessArray = [];
for(let i = 0; i < this.encryptedNumbers.length; i+= app.lengthGuess)
{
guessArray.push(this.encryptedNumbers[i]);
}
let array = _.groupBy(guessArray);
for (let i = 0; i < 26; i++) {
if (!array[i]) {
array[i] = [];
}
}
let lengths = _.map(array, i => i.length);
let m = Math.max.apply(null, lengths);
let toReturn = [];
for (let i = 0; i < 26; i++) {
toReturn.push(lengths[i] / m);
}
return toReturn;
},
update : _.debounce(()=> {
app.cleanText = "";
app.keyString = "";
app.numeric = [];
app.keyArray = [];
app.encryptedNumbers = [];
app.encryptedCharacters = [];
for (c of app.startText) {
var char = c.toLowerCase();
if (/[a-z]/.test(char)) {
app.cleanText += char;
}
}
for (c of app.uncleanKeyString) {
var char = c.toLowerCase();
if (/[a-z]/.test(char)) {
app.keyString += char;
}
}
for (c of app.cleanText) {
var code = c.charCodeAt(0) - base;
app.numeric.push(code);
}
for (c of app.keyString) {
var code = c.charCodeAt(0) - base;
app.keyArray.push(code);
}
for (var i = 0, j = 0; i < app.numeric.length; i++) {
app.encryptedNumbers.push((app.numeric[i] + app.keyArray[j]) % 26)
j++;
j %= app.keyArray.length;
}
app.encryptedNumbers.forEach(i => app.encryptedCharacters += String.fromCharCode(i + base));
}, 100),
oneTimePad() {
app.uncleanKeyString = "";
for (let i = 0; i < 2000; i++) {
app.uncleanKeyString += String.fromCharCode(base + Math.random() * 26);
}
app.update();
},
longPlainText() {
app.startText =
"The unanimous Declaration of the thirteen united States of America, When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation." +
"We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.--That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed, --That whenever any Form of Government becomes destructive of these ends, it is the Right of the People to alter or to abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in such form, as to them shall seem most likely to effect their Safety and Happiness. Prudence, indeed, will dictate that Governments long established should not be changed for light and transient causes; and accordingly all experience hath shewn, that mankind are more disposed to suffer, while evils are sufferable, than to right themselves by abolishing the forms to which they are accustomed. But when a long train of abuses and usurpations, pursuing invariably the same Object evinces a design to reduce them under absolute Despotism, it is their right, it is their duty, to throw off such Government, and to provide new Guards for their future security.--Such has been the patient sufferance of these Colonies; and such is now the necessity which constrains them to alter their former Systems of Government. The history of the present King of Great Britain is a history of repeated injuries and usurpations, all having in direct object the establishment of an absolute Tyranny over these States. To prove this, let Facts be submitted to a candid world.";
app.update();
},
},
});
</script>
<script>
app.update();
</script>
</body>
</html>