Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: RSA cryptosystem signature project #184

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
Binary file added index51.js
Binary file not shown.
12 changes: 12 additions & 0 deletions rsa-cryptosystem-signature/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>RSA Digital Signature System</title>
</head>
<body>
<script src="index.js"></script>
</body>
</html>
72 changes: 72 additions & 0 deletions rsa-cryptosystem-signature/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const firstPrime = 2;
const secondPrime = 5;
const N = firstPrime * secondPrime;
const phiOfN = (firstPrime - 1) * (secondPrime - 1);
let publicKey = 0;

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; ++i) {
hashValue += message.charCodeAt(i);
}
return hashValue % N;
}

function isCoPrime(smallerNum, largerNum) {
for (let i = 2; i <= smallerNum; ++i) {
if (smallerNum % i === 0 && largerNum % i === 0) {
return false;
}
}
return true;
}

function generatePrivateKey() {
for (let privateKey = 2; privateKey < phiOfN; ++privateKey) {
if (isCoPrime(privateKey, N) && isCoPrime(privateKey, phiOfN)) {
return privateKey;
}
}

console.log("Private key can't be generated.");
return 0;
}

function generatePublicKey(privateKey) {
while (privateKey) {
if ((publicKey * privateKey) % phiOfN === 1 && privateKey !== publicKey) {
return;
}
++publicKey;
}

console.log("Public key can't be generated.");
}

function generateSignature(hashValue, privateKey) {
return Math.pow(hashValue, privateKey) % N;
}

function decryptSignature(digitalSignature) {
return Math.pow(digitalSignature, publicKey) % N;
}

function sendMsgToBob(message) {
const privateKey = generatePrivateKey();
generatePublicKey(privateKey);
const hashValue = hashTheMessage(message);
const generatedSignature = generateSignature(hashValue, privateKey);
sendAndVerify(generatedSignature, message);
}

function sendAndVerify(digitalSignature, message) {
const hashValue = hashTheMessage(message);
const decryptedSignature = decryptSignature(digitalSignature);
if (hashValue === decryptedSignature) {
console.log("Success! Data is intact and signature is verified.");
} else {
console.log("Failure! There's something wrong with data or signature.");
}
}

sendMsgToBob("Hey Bob, I'm Alice here. Bob, Buy 300 shares of TSLA!");
9 changes: 9 additions & 0 deletions rsa-cryptosystem-signature/index01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Cryptography is a mathematical art of protecting information from an unauthorized access.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

Digital signature is a way to verify the authenticity and integrity of documents and messages.

First, we hash our message, which is a method to transform a document into a fixed-size value.

Create a function `hashTheMessage()` with `message` as a parameter. Functions which hash the data are called hash functions.
*/
6 changes: 6 additions & 0 deletions rsa-cryptosystem-signature/index02.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
The fixed-size value is also known as a hash value.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved
Create a variable `hashValue` and set it to 0.
*/

function hashTheMessage(message) {}
9 changes: 9 additions & 0 deletions rsa-cryptosystem-signature/index03.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Hash functions ideally use all of the data for hashing.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

Create a `for` loop to iterate over all the characters of `message`.
*/

function hashTheMessage(message) {
let hashValue = 0;
}
11 changes: 11 additions & 0 deletions rsa-cryptosystem-signature/index04.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
The hash value should not let users know the original data. So, we will represent our message as an integer.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

Add the ASCII value of characters to `hashValue`.
Use `charCodeAt(index)` to find the ASCII value of each character in `message`.
*/

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; i++) {}
}
11 changes: 11 additions & 0 deletions rsa-cryptosystem-signature/index05.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Hash functions should always return the same hash value when same message is passed.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved
Return `hashValue`.
*/

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; i++) {
hashValue += message.charCodeAt(i);
}
}
15 changes: 15 additions & 0 deletions rsa-cryptosystem-signature/index06.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; i++) {
hashValue += message.charCodeAt(i);
}
return hashValue;
}

/*
In `ROT13`, the values of letters were shifted by 13 places. The number 13 is called a key. It was used for both encryption and decryption. This is known as Symmetric key cryptography.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

In Asymmetric key cryptography, each user has two different keys - public and private. The public key can be shared with everyone.

Create an empty function `generatePublicKey()`.
*/
15 changes: 15 additions & 0 deletions rsa-cryptosystem-signature/index07.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; i++) {
hashValue += message.charCodeAt(i);
}
return hashValue;
}

function generatePublicKey() {}

/*
The private key is only known to the user. It must be kept as a secret.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

Create an empty function `generatePrivateKey()`.
*/
17 changes: 17 additions & 0 deletions rsa-cryptosystem-signature/index08.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
If `publicKey` is used for encryption then only the `privateKey` can decrypt it. Keep in mind that the vice-versa is also true. Therefore, they are referred to as key pairs.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

Create a variable `publicKey` and set it to 0.
*/

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; ++i) {
hashValue += message.charCodeAt(i);
}
return hashValue;
}

function generatePrivateKey() {}

function generatePublicKey() {}
21 changes: 21 additions & 0 deletions rsa-cryptosystem-signature/index09.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let publicKey = 0;

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; ++i) {
hashValue += message.charCodeAt(i);
}
return hashValue;
}

function generatePrivateKey() {}

function generatePublicKey() {}

/*
Suppose Alice sends an encrypted message to Bob using a symmetric key. There's no way for Bob to verify if the sender is Alice. Since anyone with access to that key can send Bob the message and claim to be Alice.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

Digital signature is the solution. Alice encrypts the hash value of data with his private key which we call signature.

Create an empty function `generateSignature()`.
*/
24 changes: 24 additions & 0 deletions rsa-cryptosystem-signature/index10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let publicKey = 0;

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; ++i) {
hashValue += message.charCodeAt(i);
}
return hashValue;
}

function generatePrivateKey() {}

function generatePublicKey() {}

function generateSignature() {}

/*
Alice sends the signature along with the original data to Bob. Bob decrypts the received signature with Alice's public key and hashes the received data.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

If the hash value of the received data and the decrypted signature match then the signature is authentic and data is intact.

Create an empty function `decryptSignature()`.
Tip: Go back to previous challenge, read it once more and come back. You'll get the idea.
*/
25 changes: 25 additions & 0 deletions rsa-cryptosystem-signature/index11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let publicKey = 0;

/*
We'll use the RSA asymmetric cryptographic algorithm to generate key pairs, to encrypt and decrypt data.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

RSA is based on the fact that finding prime factors of a large number is difficult.

Create a constant `firstPrime` and set it to 2.
*/

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; ++i) {
hashValue += message.charCodeAt(i);
}
return hashValue;
}

function generatePrivateKey() {}

function generatePublicKey() {}

function generateSignature() {}

function decryptSignature() {}
25 changes: 25 additions & 0 deletions rsa-cryptosystem-signature/index12.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const firstPrime = 2;
let publicKey = 0;

/*
Remember, prime numbers are only divisible by 1 and by the number itself.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved
E.g. 2, 3, 5, 7, 11, etc.

Create a constant `secondPrime` and set it to 5.
*/

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; ++i) {
hashValue += message.charCodeAt(i);
}
return hashValue;
}

function generatePrivateKey() {}

function generatePublicKey() {}

function generateSignature() {}

function decryptSignature() {}
25 changes: 25 additions & 0 deletions rsa-cryptosystem-signature/index13.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const firstPrime = 2;
const secondPrime = 5;
let publicKey = 0;

/*
Create a constant `N` and set it as `firstPrime * secondPrime`. `N` will be a part of both private and public key. Here, `N = 10`. So, it's easy to find the prime factors 2 and 5.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

But in real world usage `N` is around 600 digits long which makes prime factorization almost impossible. Our implementation is insecure. It is just to teach you about the fundamentals of asymmetric cryptography.
*/

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; ++i) {
hashValue += message.charCodeAt(i);
}
return hashValue;
}

function generatePrivateKey() {}

function generatePublicKey() {}

function generateSignature() {}

function decryptSignature() {}
28 changes: 28 additions & 0 deletions rsa-cryptosystem-signature/index14.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const firstPrime = 3;
const secondPrime = 5;
const N = firstPrime * secondPrime;
let publicKey = 0;

/*
Private key must be between 1 and `Φ(N)` i.e. 1 < privateKey < `Φ(N)`.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

`Φ(N)` pronounced as phi of N is called Euler's totient function. It outputs number of integers upto `N` that are coprime with `N`.

Create a constant `phiOfN` and set it to 0.
*/

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; ++i) {
hashValue += message.charCodeAt(i);
}
return hashValue;
}

function generatePrivateKey() {}

function generatePublicKey() {}

function generateSignature() {}

function decryptSignature() {}
30 changes: 30 additions & 0 deletions rsa-cryptosystem-signature/index15.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const firstPrime = 2;
const secondPrime = 5;
const N = firstPrime * secondPrime;
const phiOfN = 0;
let publicKey = 0;

function hashTheMessage(message) {
let hashValue = 0;
for (let i = 0, msgLength = message.length; i < msgLength; ++i) {
hashValue += message.charCodeAt(i);
}
return hashValue;
}

/*
Two integers are coprime if the only positive integer that divides both of them simultaneously is 1.
vkWeb marked this conversation as resolved.
Show resolved Hide resolved

E.g. 10 and 7 are coprime.
2 and 5 divides 10 but not 7. Hence, they both are only divisible by 1.

Create an empty function `isCoPrime` with two integer parameters.
*/

function generatePrivateKey() {}

function generatePublicKey() {}

function generateSignature() {}

function decryptSignature() {}
Loading