-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cipher_Feistel.js.html
342 lines (274 loc) · 11.4 KB
/
Cipher_Feistel.js.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> Cipher/Feistel.js</title>
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="./build/entry.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="https://fonts.googleapis.com/css?family=Roboto:100,400,700|Inconsolata,700" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<link type="text/css" rel="stylesheet" href="https://jmblog.github.io/color-themes-for-google-code-prettify/themes/tomorrow-night.min.css">
<link type="text/css" rel="stylesheet" href="styles/app.min.css">
<link type="text/css" rel="stylesheet" href="styles/iframe.css">
<link type="text/css" rel="stylesheet" href="">
<script async defer src="https://buttons.github.io/buttons.js"></script>
</head>
<body class="layout small-header">
<div id="stickyNavbarOverlay"></div>
<div class="top-nav">
<div class="inner">
<a id="hamburger" role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
<div class="logo">
<a href="index.html">
<h1 class="navbar-item">Encryption tool</h1>
</a>
</div>
<div class="menu">
<div class="navigation">
<a
href="index.html"
class="link"
>
API Documentation
</a>
</div>
</div>
</div>
</div>
<div id="main">
<div
class="sidebar "
id="sidebarNav"
>
<nav>
<h2><a href="index.html">Documentation</a></h2><div class="category"><h3>Classes</h3><ul><li><a href="Feistel.html">Feistel</a></li><li><a href="SessionManager.html">SessionManager</a></li></ul><h3>Components</h3><ul><li><a href="App.html">App</a></li><li><a href="Footer.html">Footer</a></li><li><a href="Header.html">Header</a></li><li><a href="Login.html">Login</a></li><li><a href="Main.html">Main</a></li><li><a href="MainLayout.html">MainLayout</a></li><li><a href="Selector.html">Selector</a></li><li><a href="TextArea.html">TextArea</a></li></ul><h3>Global</h3><ul><li><a href="global.html#openNotification">openNotification</a></li></ul></div><div class="category"><h2>Constants</h2><h3>Global</h3><ul><li><a href="global.html#BITWISE_FUNCTIONS">BITWISE_FUNCTIONS</a></li></ul></div><div class="category"><h2>Utils</h2><h3>Global</h3><ul><li><a href="global.html#base64ToUTF16">base64ToUTF16</a></li><li><a href="global.html#binaryToText">binaryToText</a></li><li><a href="global.html#decimalToBinary">decimalToBinary</a></li><li><a href="global.html#enableTabulatorInTextArea">enableTabulatorInTextArea</a></li><li><a href="global.html#from8BitBinaryToUnicode">from8BitBinaryToUnicode</a></li><li><a href="global.html#generateKey">generateKey</a></li><li><a href="global.html#getBitWiseFunc">getBitWiseFunc</a></li><li><a href="global.html#textToBinary">textToBinary</a></li><li><a href="global.html#textToUnicodeDecimalArray">textToUnicodeDecimalArray</a></li><li><a href="global.html#unicodeTo8BitBinary">unicodeTo8BitBinary</a></li><li><a href="global.html#UTF16ToBase64">UTF16ToBase64</a></li></ul></div>
</nav>
</div>
<div class="core" id="main-content-wrapper">
<div class="content">
<header class="page-title">
<p>Source</p>
<h1>Cipher/Feistel.js</h1>
</header>
<section>
<article>
<pre class="prettyprint source linenums"><code>import { getBitWiseFunc } from "Utils/BitwiseFunction"
import { sha256 } from 'js-sha256';
import {
textToBinary,
binaryToText,
decimalToBinary,
UTF16ToBase64,
base64ToUTF16
} from "Utils/Converters"
const CHAR_NO_OF_BITS = 16; // UTF-16 number of bits to each sign
const PADDING_CHARACTER = '\u0003' // Unicode 'End of Text'
const BLOCK_SIZE = 128; // Bit size of block
const KEY_SIZE = 64; // Bit size of key
const DEFAULT_ROUND_NO = 8; // Default number of encyption process
const DEFAULT_KEY = "Feistel Cipher"; // Default key - can be any string
const MODES = {
ENCRYPT: 'encrypt',
DECRYPT: 'decrypt'
};
/**
* Feistel class is main point in Cipher Application.<br>
* This is implementation of Feistel Cipher<br>
* It supports all 65535 UTF-16 code points.
* Use encrypt() and decrypt() with appropriate text data.<br>
* You can read more about algorithm here:
* {@link https://en.wikipedia.org/wiki/Feistel_cipher}
*/
class Feistel {
/**
* Provide key and number of encryption rounds
* @constructor
* @param {String} key Main encrypting key
* @param {Number} rounds Number of encryption rounds
*/
constructor(rounds = DEFAULT_ROUND_NO, key = DEFAULT_KEY) {
this.setKey(key)
this.rounds = rounds
this.encryptionFunction = getBitWiseFunc((a,b) => a ^ b)
this.xor = getBitWiseFunc((a,b) => a ^ b)
}
/**
* Function hashing key to binary
* @param {String} key Encrypting key
* @returns {String} Hashed key in binary
*/
hashKey(key) {
const noOfBytes = KEY_SIZE / 8;
const sha64KeyArray = sha256.array(key).slice(0, noOfBytes);
return sha64KeyArray.map(value => decimalToBinary(value, 8)).join('');
}
/**
* Return key for each iteration
* @param {String} masterKey Main key
* @param {Number} iteration Number of current iteration
* @returns {String} Sub key based on main key, for each iteration
*/
getSubKey(masterKey, iteration) {
return this.hashKey(`${masterKey}${iteration}${iteration ** 37}`);
}
/**
* Spliting input in half
* @param {String} binaryText Binary input
* @returns {Array} Array of strings with two sub-keys
*/
divideEvenly(binaryText) {
const half = binaryText.length / 2;
return [binaryText.substr(0, half), binaryText.substr(half)];
}
/**
* Sets hashed master key
* @param {String} key Key provided by user
*/
setKey(key) {
this.key = key;
this.masterKey = this.hashKey(key);
}
/**
* Sets number of rounds choosen by user
* @param {Number} rounds
*/
setRounds(rounds) {
this.rounds = rounds;
}
/**
* Return current master key
* @returns {String} Current master key
*/
getKey() {
return this.key;
}
/**
* Seting encrypting function
* @param {Function} operator Bitwise function
*/
setEncryptionOperator(operator) {
this.encryptionFunction = getBitWiseFunc(operator)
}
/**
* Function applied at each round of the enciphering process
* @param {String} left Left side of divided binary
* @param {String} right Right side of divided binary
* @param {String} key Enciphering current iteration key
* @returns {Array} Array with right and left side of input text
*/
makeRound(left, right, key) {
const functionResult = this.encryptionFunction(right, key);
const newRight = this.xor(functionResult, left);
const newLeft = right;
return [newLeft, newRight]
}
/**
* Encrypting / decrypting single block of text
* Single block - 8 letters (128bit / 16bit - one character)
* @param {String} mode Process mode - "encrypt" or "decrypt"
* @param {String} inputBinaryText Binary notation in string
* @returns {String} Encrypted or decrypted text
*/
processSingleBlock(mode, inputBinaryText) {
let [left, right] = this.divideEvenly(inputBinaryText);
for (let i = 0; i < this.rounds; i++) {
const subKeyIteration = mode === MODES.ENCRYPT ? i : this.rounds - i - 1;
const key = this.getSubKey(this.masterKey, subKeyIteration);
[left, right] = this.makeRound(left, right, key);
}
return right + left;
}
/**
* Core function - splits text into blocks and processes them
* @param {String} mode Process mode - "encrypt" or "decrypt"
* @param {String} inputText Provided text
* @returns {String} Encrypted / decrypted text (with possible padding in decrypted text)
*/
process(mode, inputText) {
const blocks = this.divideIntoBlocks(inputText);
let result = '';
for (const textBlock of blocks) {
const blockResult = this.processSingleBlock(mode, textToBinary(textBlock, CHAR_NO_OF_BITS))
result += binaryToText(blockResult, CHAR_NO_OF_BITS);
}
return result;
}
/**
* Divides text into equall blocks
* @param {String} text Provided text
* @returns {Array} Array of strings
*/
divideIntoBlocks(text) {
const noOfChars = BLOCK_SIZE / CHAR_NO_OF_BITS;
const blocks = [];
for (let i = 0; i < text.length; i+= noOfChars) {
const blockText = text.slice(i, i + noOfChars);
blocks.push(blockText);
}
const lastBlockLength = blocks[blocks.length - 1].length;
const requiredPaddingSize = noOfChars - lastBlockLength;
if (requiredPaddingSize) {
const padding = PADDING_CHARACTER.repeat(requiredPaddingSize);
blocks[blocks.length - 1] += padding;
}
return blocks;
}
/**
* Main encypting function
* @param {String} text Provided text
* @returns {String} Base64 encrypted string
*/
encrypt(text) {
const cipher = this.process(MODES.ENCRYPT, text);
return UTF16ToBase64(cipher)
}
/**
* Main decrypting function
* @param {String} base64Text Provided Base64 text
* @returns {String} UTF16 decrypted text
*/
decrypt(base64Text) {
const cipher = base64ToUTF16(base64Text)
const result = this.process(MODES.DECRYPT, cipher);
const paddingIndex = result.indexOf(PADDING_CHARACTER);
let decryptedText = null;
if (paddingIndex === -1) {
decryptedText = result;
} else {
decryptedText = result.slice(0, paddingIndex)
}
return decryptedText
}
}
export default Feistel;</code></pre>
</article>
</section>
</div>
<footer class="footer">
<div class="content has-text-centered">
<p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.7</a></p>
<p class="sidebar-created-by">
<a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
<a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
</p>
</div>
</footer>
</div>
<div id="side-nav" class="side-nav">
</div>
</div>
<script src="scripts/app.min.js"></script>
<script>PR.prettyPrint();</script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>