Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ And for decryption:
plaintext = ciphertext^d % n

#####Digital Signatures
Using RSA to generate the public-private keys can ensure that no one other than the desired recipient can read the message, but gives the recipient no information about the sender of the message. To authenticate the sender, use digital signatures. Since the public and private keys can reverse each other, all we have to do as the sender of the message to sign it is encrypt a message with our private key, and include that along with the message we’re sending. Then the recipient can unsign (by the same method as decryption) the signature by using our public key. Then, if the unsigned signature and the message are the same, the recipient knows that we were in possession of the private key, and thus trust that we are who we say we are.
Using RSA to generate the public-private keys can ensure that no one other than the desired recipient can read the message, but gives the recipient no information about the sender of the message. To authenticate the sender, use digital signatures. Since the public and private keys can reverse each other, all we have to do as the sender of the message to sign it is encrypt a message with our private key, and include that along with the message we’re sending. Then the recipient can unsign (by the same method as decryption) the signature by using our public key. Then, if the unsigned signature and the message are the same, the recipient knows that we were in possession of the private key, and thus trust that we are who we say we are.
85 changes: 73 additions & 12 deletions asymmetricKeyEncryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,94 @@ var Identity = function(){
this.modulus = null;
};

/**
* Calculates the modulus and private and public keys, and stores them on the
* Identity.
*
* @param {number} p the first of two distinct primes
* @param {number} q the second prime
*
* @sideeffect sets this.modulus, this.publicKey, and this.privateKey on the
* Identity instance
*/
Identity.prototype.generateKeyPair = function(p, q){
/* Should calculate the private and public key, and store them on the Identity */

};

/**
* Given a message, generates and returns the sender's signature. A signature
* is a messsage encrypted using an Identity's private key to verify that they
* sent the message.
*
* @param {string} text the message to sign
* @return {string} the signature
*/
Identity.prototype.signMessage = function(text){
/* Given text, generate and return the senders signature */

};

/**
* Given plaintext and a recipient Identity, generates ciphertext and signature.
* Hint: in this case, the signature is simply the ciphertext encrypted with the
* sender's private key.
*
* @param {string} plaintext the message to be encrypted and sent
* @param {Object} recipient an Identity object
* @return {Object} an object with signature, ciphertext, and sender properties
*/
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) */

};

/**
* Given the ciphertext, signature, and sender, receiveMessage should determine
* the integrity of the message and selectively read and return the content.
*
* @param {string} ciphertext the encrypted message
* @param {string} signature the signed message
* @param {Object} sender an Identity object
* @return {string} the plaintext
*/
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. */

};

/**
* Turns plaintext into ciphertext.
*
* @param {string} plaintext the message to encrypt
* @param {number} key the key (public or private) with which to encrypt
* @param {number} modulus the modulus for modular arithmetic calculations
* @return {string} the ciphertext
*/
var encryptMessage = function(plaintext, key, modulus){
/* Should turn plaintext into ciphertext according to the RSA protocol and return it */

};

/**
* Turns ciphertext into plaintext.
*
* @param {string} ciphertext the encrypted message to decrypt
* @param {number} key the key (public or private) with which to decrypt
* @param {number} modulus the modulus for modular arithmetic calculations
* @return {string} the plaintext
*/
var decryptMessage = function(ciphertext, key, modulus){
/* Should turn ciphertext into plaintext according to the RSA protocol and return it */

};

/**
* Checks that a signature is valid.
*
* @param {string} text the plaintext to check the decrypted signature against
* @param {string} signature the claimed encryption of the plaintext with the
* key in question
* @param {number} key the public key of the sender
* @param {[type]} modulus the modulus for modular arithmetic calculations
* @return {boolean} whether or not the decrypted text matches the signature
*/
var confirmAuthenticity = function(text, signature, key, modulus){
/* Should confirm that the sender is who they claim to be */

};

/*******************************************/
Expand All @@ -52,7 +113,7 @@ var numberToLetter = function(number){
var findCoprime = function(number){
for(var i = 2; i < number; i++){
if( determineIfCoprime(i, number) ){
return i
return i;
}
}
};
Expand All @@ -70,7 +131,7 @@ var determineIfCoprime = function(a, b){
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
if(value in larger) return false;
}
return true;
};
Expand All @@ -83,8 +144,8 @@ var factor = function(number){
primes[number / i] = true;
}
}
primes[number] = true
return primes
primes[number] = true;
return primes;
};

calculateModInverse = function(number, mod){
Expand Down
20 changes: 10 additions & 10 deletions lib/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ describe('symmetric key encryption', function(){
});
});
context('encryptMessage', function(){
it('returns cyphertext for given plaintext', function(){
it('returns ciphertext for given plaintext', function(){
var alice = new Sender();
alice._secretKey = 4;
var encryptedMessage = alice.encryptMessage('secret message');
expect(encryptedMessage).to.equal('wagvap$iawweca');
});
it('returns different cyphertext for different keys', function(){
it('returns different ciphertext for different keys', function(){
var alice = new Sender();
alice._secretKey = 4;
var encryptedMessage1 = alice.encryptMessage('secret message');
Expand All @@ -37,14 +37,14 @@ describe('symmetric key encryption', function(){
});
});
context('decryptMessage', function(){
it('returns plaintext for given cyphertext', function(){
it('returns plaintext for given ciphertext', function(){
var alice = new Sender();
alice._secretKey = 4;
expect(alice.decryptMessage('wagvap$iawweca')).to.equal('secret message');
});
});
context('sendMessage', function(){
it('returns ciphertext for given cyphertext and passes it to recipient.receiveMessage', function(){
it('returns ciphertext for given ciphertext and passes it to recipient.receiveMessage', function(){
var spy = sinon.spy(Sender.prototype, 'receiveMessage');
var alice = new Sender();
alice._secretKey = 4;
Expand All @@ -57,7 +57,7 @@ describe('symmetric key encryption', function(){
});
});
context('receiveMessage', function(){
it('returns plaintext for given cyphertext', function(){
it('returns plaintext for given ciphertext', function(){
var alice = new Sender();
alice._secretKey = 4;
var ciphertext = 'wagvap$iawweca';
Expand Down Expand Up @@ -94,11 +94,11 @@ describe('asymmetric key encryption', function(){
});
});
context('encryptMessage', function(){
it('returns cyphertext for given plaintext', function(){
it('returns ciphertext for given plaintext', function(){
var encryptedMessage = encryptMessage('secret message', 7, 33);
expect(encryptedMessage).to.equal('gqCiqnfmqggaDq');
});
it('returns different cyphertext for different keys', function(){
it('returns different ciphertext for different keys', function(){
var encryptedMessage1 = encryptMessage('secret message', 7, 33);
var encryptedMessage2 = encryptMessage('secret message', 3, 33);
expect(encryptedMessage1).not.to.equal(encryptedMessage2);
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('asymmetric key encryption', function(){
});
});
context('decryptMessage', function(){
it('returns plaintext for given cyphertext', function(){
it('returns plaintext for given ciphertext', function(){
var alice = new Identity();
var encryptedMessage = encryptMessage('secret message', 7, 33);
expect(encryptedMessage).to.equal('gqCiqnfmqggaDq');
Expand All @@ -144,7 +144,7 @@ describe('asymmetric key encryption', function(){
});
});
context('sendMessage', function(){
it('returns plaintext for given cyphertext', function(){
it('returns plaintext for given ciphertext', function(){
var spy = sinon.spy(Identity.prototype, 'receiveMessage');
var alice = setupIdentity(7, 3, 33);
var bob = setupIdentity(7, 3, 33);
Expand All @@ -161,7 +161,7 @@ describe('asymmetric key encryption', function(){
var response = bob.receiveMessage('blahblah', 'something different', alice);
expect(response).to.equal('Identity not authenticated');
});
it('returns plaintext for given cyphertext', function(){
it('returns plaintext for given ciphertext', function(){
var alice = setupIdentity(7, 3, 33); //3,11
var bob = setupIdentity(5, 5, 35); //7,17
var plaintext = 'secret message';
Expand Down