Skip to content

Commit 604172b

Browse files
committed
2 parents fa78c4c + 8285ad7 commit 604172b

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

algorithm/category.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"cryptography": {
1515
"name": "Cryptography",
1616
"list": {
17-
"caesar_cipher": "Caesar Cipher"
17+
"caesar_cipher": "Caesar Cipher",
18+
"affine_cipher": "Affine Cipher"
1819
}
1920
},
2021
"mst": {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
code assumes that plainText contains ONLY LOWER CASE ALPHABETS
3+
*/
4+
5+
Number.prototype.mod = function (n) {
6+
return ((this%n)+n)%n;
7+
};
8+
9+
var keys = {a: 5, b: 7},
10+
N = 26;
11+
12+
function encrypt (plainText) {
13+
var cypherText = '';
14+
function cryptAlpha (alpha) {
15+
var index = alpha.charCodeAt (0) - 'a'.charCodeAt (0);
16+
var result = ((keys.a * index) + keys.b).mod (N);
17+
18+
logger._print ('Index of ' + alpha + ' = ' + index);
19+
20+
result += 'a'.charCodeAt (0);
21+
return String.fromCharCode (result);
22+
}
23+
24+
logger._print ('Beginning Affine Encryption');
25+
logger._print ('Encryption formula: <b>((keys.a * index_of_alphabet) + keys.b) % N</b>');
26+
logger._print ('keys.a=' + keys.a + ', keys.b=' + keys.b + ', N=' + N);
27+
28+
for (var i in plainText) {
29+
ptTracer._select (i)._wait ();
30+
ptTracer._deselect (i)._wait ();
31+
32+
cypherText += cryptAlpha (plainText [i]);
33+
34+
ptTracer._notify (i, cypherText.slice (-1))._wait ();
35+
ptTracer._denotify (i)._wait ();
36+
}
37+
38+
return cypherText;
39+
}
40+
41+
function decrypt (cypherText) {
42+
var plainText = '';
43+
var aInverse = (function () {
44+
for (var i = 1; i < N; i++) {
45+
if ( ((keys.a * i).mod (N)) == 1 ) {
46+
return i;
47+
}
48+
}
49+
}) ();
50+
51+
logger._print ('a<sup>-1</sup> = ' + aInverse);
52+
53+
function decryptAlpha (alpha) {
54+
var index = alpha.charCodeAt (0) - 'a'.charCodeAt (0);
55+
var result = (aInverse * (index - keys.b)).mod (N);
56+
57+
logger._print ('Index of ' + alpha + ' = ' + index);
58+
59+
result += 'a'.charCodeAt (0);
60+
return String.fromCharCode (result);
61+
}
62+
63+
logger._print ('Beginning Affine Decryption');
64+
logger._print ('Decryption formula: <b>(a<sup>-1</sup> * (index - keys.b)) % N</b>');
65+
logger._print ('keys.b=' + keys.b + ', N=' + N);
66+
67+
for (var i in cypherText) {
68+
ctTracer._select (i)._wait ();
69+
ctTracer._deselect (i)._wait ();
70+
71+
plainText += decryptAlpha (cypherText [i]);
72+
73+
ctTracer._notify (i, plainText.slice (-1))._wait ();
74+
ctTracer._denotify (i)._wait ();
75+
}
76+
77+
return plainText;
78+
}
79+
80+
var cipherText = encrypt (plainText);
81+
ctTracer._setData (cipherText);
82+
decrypt (cipherText);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function randString (length) {
2+
var choices = 'abcdefghijklmnopqrstuvwxyz';
3+
var text = '';
4+
5+
for (var i = 0; i < length; i++) {
6+
text += choices [Math.floor (Math.random () * choices.length)];
7+
}
8+
9+
return text;
10+
}
11+
12+
//var plainText = randString (5);
13+
var plainText = 'secret';
14+
var ptTracer = new Array1DTracer ('Encryption');
15+
var ctTracer = new Array1DTracer ('Decryption');
16+
var logger = new LogTracer ();
17+
18+
ptTracer._setData (plainText);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"Affine Cipher": "The affine cipher is a type of monoalphabetic substitution cipher, wherein each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter.",
3+
"Applications": [
4+
"Cryptanalysis",
5+
"More complex Variations of Affine Cihper are used in practical cryptography"
6+
],
7+
"Complexity": {
8+
"time": "worst O(N), N = length of plain/cipher text",
9+
"space": "worst O(N), to create the new mapping (plain->cipher, cipher->plain)"
10+
},
11+
"References": [
12+
"<a href='http://practicalcryptography.com/ciphers/affine-cipher/'>Practicalcryptography</a>"
13+
],
14+
"files": {
15+
"basic": "Encrypting and Decrypting a string using affine functions"
16+
}
17+
}

0 commit comments

Comments
 (0)