-
Notifications
You must be signed in to change notification settings - Fork 7
/
asymmetricKeyEncryption.js
98 lines (85 loc) · 3.31 KB
/
asymmetricKeyEncryption.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
var Identity = function(){
this.privateKey = null;
this.publicKey = null;
this.modulus = null;
};
Identity.prototype.generateKeyPair = function(p, q){
/* Should calculate the private and public key, and store them on the Identity */
};
Identity.prototype.signMessage = function(text){
/* Given text, generate and return the senders signature */
};
Identity.prototype.sendMessage = function(plaintext, recipient){
/* Given plaintext and a recipient, sendMessage should follow all the necessary protocols for it to be securely sent, and then send the message */
/* (Hint: look at receiveMessage) */
};
Identity.prototype.receiveMessage = function(ciphertext, signature, sender){
/* Given the ciphertext, signature, and sender, receiveMessage should determine the integrity of the message and selectively read and return the content. */
};
var encryptMessage = function(plaintext, key, modulus){
/* Should turn plaintext into ciphertext according to the RSA protocol and return it */
};
var decryptMessage = function(ciphertext, key, modulus){
/* Should turn ciphertext into plaintext according to the RSA protocol and return it */
};
var confirmAuthenticity = function(text, signature, key, modulus){
/* Should confirm that the sender is who they claim to be */
};
/*******************************************/
// It's dangerous to go alone! Take these!//
// HELPER FUNCTIONS //
// (do not modify) //
/*******************************************/
var letterToNumber = function(letter){
return letters.indexOf(letter);
};
var numberToLetter = function(number){
if(number >= letters.length){
number = number % letters.length; // TODO
} else {
}
return letters[number];
};
var findCoprime = function(number){
for(var i = 2; i < number; i++){
if( determineIfCoprime(i, number) ){
return i
}
}
};
/*******************************************/
// HELPER HELPER FUNCTIONS //
// (you won't use these) //
// (do not modify) //
/*******************************************/
var determineIfCoprime = function(a, b){
var factorsa = factor(a);
var factorsb = factor(b);
delete factorsa[1];
delete factorsb[1];
var smaller = Object.keys(factorsa) < Object.keys(factorsb) ? factorsa : factorsb;
var larger = Object.keys(factorsa) < Object.keys(factorsb) ? factorsb : factorsa;
for(var value in smaller){
if(value in larger) return false
}
return true;
};
var factor = function(number){
var primes = {};
for(var i = 0; i <= Math.sqrt(number); i++){
if(number % i === 0){
primes[i] = true;
primes[number / i] = true;
}
}
primes[number] = true
return primes
};
calculateModInverse = function(number, mod){
for(var i = 1; i < mod; i++){
if(number * i % mod === 1) return i
}
};
var validLetters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' '];
var extendedLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']'];
var letters = validLetters.concat(extendedLetters)