0% found this document useful (0 votes)
15 views15 pages

Elgamal Based DSA

The Digital Signature Algorithm (DSA) is a variant of the ElGamal signature scheme, which should not be confused with ElGamal encryption. ElGamal encryption can be defined over any cyclic group , like multiplicative group of integers modulo n if and only if n is 1, 2, 4, pk or 2pk, where p is an odd prime and k > 0.

Uploaded by

daksh.singh2021a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

Elgamal Based DSA

The Digital Signature Algorithm (DSA) is a variant of the ElGamal signature scheme, which should not be confused with ElGamal encryption. ElGamal encryption can be defined over any cyclic group , like multiplicative group of integers modulo n if and only if n is 1, 2, 4, pk or 2pk, where p is an odd prime and k > 0.

Uploaded by

daksh.singh2021a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Elgamal based DSA-

#include <iostream>

#include <cmath>

#include <cstdlib>

#include <ctime>

using namespace std;

bool isPrime(int n) {

if (n <= 1) return false;

if (n <= 3) return true;

if (n % 2 == 0 || n % 3 == 0) return false;

for (int i = 5; i * i <= n; i += 6)

if (n % i == 0 || n % (i + 2) == 0)

return false;

return true;

int modExp(int a, int b, int m) {

int result = 1;

a = a % m;

while (b > 0) {

if (b % 2 == 1)

result = (result * a) % m;

b = b / 2;

a = (a * a) % m;

return result;
}

int generatePrime(int min, int max) {

int num;

do {

num = rand() % (max - min + 1) + min;

} while (!isPrime(num));

return num;

void generateKeys(int &p, int &g, int &x, int &y) {

p = generatePrime(100, 1000);

g = rand() % (p - 1) + 1;

x = rand() % (p - 1) + 1;

y = modExp(g, x, p);

void encrypt(int p, int g, int y, int plaintext, int &c1, int &c2) {

int k = rand() % (p - 1) + 1;

c1 = modExp(g, k, p);

c2 = (plaintext * modExp(y, k, p)) % p;

int decrypt(int p, int x, int c1, int c2) {

int s = modExp(c1, x, p);

int plaintext = (c2 * modExp(s, p - 2, p)) % p;

return plaintext;

int main() {

srand(time(0));

int p, g, x, y;

generateKeys(p, g, x, y);

cout << "Public Key (p, g, y): (" << p << ", " << g << ", " << y << ")" << endl;

cout << "Private Key (x): " << x << endl;


int plaintext;

cout << "Enter the plaintext message: ";

cin >> plaintext;

int c1, c2;

encrypt(p, g, y, plaintext, c1, c2);

cout << "Encrypted Message (c1, c2): (" << c1 << ", " << c2 << ")" << endl;

int decryptedMsg = decrypt(p, x, c1, c2);

cout << "Decrypted Message: " << decryptedMsg << endl;

return 0;

RSA based DSA-


#include <iostream>

#include <cmath>

#include <string>

using namespace std;

bool isPrime(int n) {

if (n <= 1) return false;

if (n <= 3) return true;

if (n % 2 == 0 || n % 3 == 0) return false;

for (int i = 5; i * i <= n; i += 6)

if (n % i == 0 || n % (i + 2) == 0)

return false;

return true;

int gcd(int a, int b) {

if (b == 0)
return a;

else

return gcd(b, a % b);

void generateKeys(int p, int q, int &n, int &e, int &d) {

if (!isPrime(p) || !isPrime(q)) {

cout << "p and q must be prime numbers." << endl;

return;

n = p * q;

int phi = (p - 1) * (q - 1);

for (e = 2; e < phi; e++) {

if (gcd(e, phi) == 1)

break;

for (d = 2; d < phi; d++) {

if ((d * e) % phi == 1)

break;

int encrypt(int msg, int e, int n) {

return int(pow(msg, e)) % n;

int decrypt(int encryptedMsg, int d, int n) {

long long decryptedMsg = 1;

for (int i = 0; i < d; i++) {

decryptedMsg = (decryptedMsg * encryptedMsg) % n;

return decryptedMsg;

}
int main() {

int p, q, n, e, d;

cout << "Enter two prime numbers (p and q): ";

cin >> p >> q;

generateKeys(p, q, n, e, d);

cout << "Public Key (n, e): (" << n << ", " << e << ")" << endl;

cout << "Private Key (n, d): (" << n << ", " << d << ")" << endl;

int msg;

cout << "Enter the message to encrypt: ";

cin >> msg;

int encryptedMsg = encrypt(msg, e, n);

cout << "Encrypted Message: " << encryptedMsg << endl;

int decryptedMsg = decrypt(encryptedMsg, d, n);

cout << "Decrypted Message: " << decryptedMsg << endl;

return 0;

SHA-
#include <iostream>

#include <string>

#include <sstream>

#include <iomanip>

using namespace std;

// Constants for SHA-512

const uint64_t K[80] = {

0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL,


0xe9b5dba58189dbbcULL,

0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL,


0xab1c5ed5da6d8118ULL,
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL,
0x550c7dc3d5ffb4e2ULL,

0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL,


0xc19bf174cf692694ULL,

0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL,


0x240ca1cc77ac9c65ULL,

0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL,


0x76f988da831153b5ULL,

0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL,


0xbf597fc7beef0ee4ULL,

0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL,


0x142929670a0e6e70ULL,

0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL,


0x53380d139d95b3dfULL,

0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL,


0x92722c851482353bULL,

0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL,


0xc76c51a30654be30ULL,

0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL,


0x106aa07032bbd1b8ULL,

0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL,


0x34b0bcb5e19b48a8ULL,

0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL,


0x682e6ff3d6b2b8a3ULL,

0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL,


0x8cc702081a6439ecULL,

0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL,


0xc67178f2e372532bULL,

0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL,


0xf57d4f7fee6ed178ULL,

0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL,


0x1b710b35131c471bULL,

0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL,


0x431d67c49c100d4cULL,

0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL,


0x6c44198c4a475817ULL

};
// Function to perform SHA-512 padding

string sha512_padding(const string &input) {

uint64_t bit_length = input.length() * 8;

uint64_t num_blocks = ((bit_length + 128) / 1024) + 1;

uint64_t new_length = num_blocks * 1024;

uint64_t pad_length = new_length - bit_length;

string padding;

padding += (char)0x80; // Append 1 bit

for (uint64_t i = 0; i < (pad_length / 8) - 1; ++i) {

padding += (char)0x00; // Append 0 bits

// Append original bit length

for (int i = 7; i >= 0; --i) {

padding += (char)((bit_length >> (i * 8)) & 0xFF);

return padding;

// Function to perform SHA-512 transformation

string sha512_transform(const string &block) {

uint64_t w[80];

uint64_t a, b, c, d, e, f, g, h;

uint64_t temp1, temp2;

uint64_t maj, ch;

uint64_t H[8] = {
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL,
0xa54ff53a5f1d36f1ULL,

0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL,


0x5be0cd19137e2179ULL

};

// Message schedule

for (int i = 0; i < 16; ++i) {

w[i] = ((uint64_t)(block[i * 8 + 0] & 0xFF) << 56) |

((uint64_t)(block[i * 8 + 1] & 0xFF) << 48) |

((uint64_t)(block[i * 8 + 2] & 0xFF) << 40) |

((uint64_t)(block[i * 8 + 3] & 0xFF) << 32) |

((uint64_t)(block[i * 8 + 4] & 0xFF) << 24) |

((uint64_t)(block[i * 8 + 5] & 0xFF) << 16) |

((uint64_t)(block[i * 8 + 6] & 0xFF) << 8) |

((uint64_t)(block[i * 8 + 7] & 0xFF));

// Extend the first 16 words into the remaining 64 words w[16..63] of the message schedule array

for (int i = 16; i < 80; ++i) {

uint64_t s0 = (w[i - 15] >> 1) | (w[i - 15] << 63);

uint64_t s1 = (w[i - 15] >> 8) | (w[i - 15] << 56);

uint64_t s2 = w[i - 15] >> 7;

w[i] = s0 + s1 + s2 + w[i - 16];

s0 = (w[i - 2] >> 19) | (w[i - 2] << 45);

s1 = (w[i - 2] >> 61) | (w[i - 2] << 3);

s2 = w[i - 2] >> 6;

w[i] += s0 + s1 + s2 + w[i - 7];

w[i] += w[i - 15] + w[i - 16];

}
// Initialize working variables to current hash value

a = H[0];

b = H[1];

c = H[2];

d = H[3];

e = H[4];

f = H[5];

g = H[6];

h = H[7];

// Compression function main loop

for (int i = 0; i < 80; ++i) {

uint64_t S0 = (a >> 28) | (a << 36);

uint64_t S1 = (a >> 34) | (a << 30);

uint64_t S2 = (a >> 39) | (a << 25);

uint64_t s0 = (a >> 14) | (a << 50);

uint64_t s1 = (a >> 18) | (a << 46);

uint64_t s2 = (a >> 41) | (a << 23);

uint64_t S3 = (a >> 19) | (a << 45);

uint64_t S4 = (a >> 61) | (a << 3);

uint64_t S5 = (a >> 6) | (a << 58);

uint64_t S6 = (a >> 41) | (a << 23);

uint64_t S7 = (a >> 45) | (a << 19);

maj = (a & b) ^ (a & c) ^ (b & c);

ch = (e & f) ^ ((~e) & g);

temp1 = h + S1 + ch + K[i] + w[i];

temp2 = S0 + maj;

h = g;

g = f;

f = e;
e = d + temp1;

d = c;

c = b;

b = a;

a = temp1 + temp2;

// Add the compressed chunk to the current hash value

H[0] += a;

H[1] += b;

H[2] += c;

H[3] += d;

H[4] += e;

H[5] += f;

H[6] += g;

H[7] += h;

// Produce the final hash value

stringstream ss;

for (int i = 0; i < 8; ++i) {

ss << hex << setfill('0') << setw(16) << H[i];

return ss.str();

// Function to perform SHA-512 hash

string sha512(const string &message) {

string padded_message = sha512_padding(message);

string hash;

for (size_t i = 0; i < padded_message.length(); i += 128) {


hash += sha512_transform(padded_message.substr(i, 128));

return hash;

int main() {

string message;

cout << "Enter the message to hash: ";

getline(cin, message);

string hashed_message = sha512(message);

cout << "SHA-512 Hash: " << hashed_message << endl;

return 0;

Elyptic curve cryptography-


#include <iostream>
#include <cmath>
#include <utility>

using namespace std;

// Define the parameters of the elliptic curve


const int p = 17; // prime number
const int a = 2; // coefficient a
const int b = 2; // coefficient b
// Define a struct for representing points on the elliptic curve
struct Point {
int x;
int y;
};

// Function to check if a number is a quadratic residue modulo p


bool isQuadraticResidue(int x) {
int residue = (int)pow(x, (p - 1) / 2) % p;
return (residue == 1);
}

// Function to perform modular exponentiation (base^exp % mod)


int modExp(int base, int exp, int mod) {
int result = 1;
base = base % mod;
while (exp > 0) {
if (exp % 2 == 1)
result = (result * base) % mod;
exp = exp / 2;
base = (base * base) % mod;
}
return result;
}

// Function to calculate the inverse of a number modulo p


int modInverse(int num, int mod) {
return modExp(num, mod - 2, mod);
}

// Function to add two points on the elliptic curve


Point addPoints(Point P, Point Q) {
Point R;
int s;

// Case of adding a point to its inverse (P = -Q)


if (P.x == Q.x && P.y == (-Q.y % p + p) % p) {
R.x = -1;
R.y = -1;
return R; // The result is the point at infinity
}

// Case of adding a point to itself (P = Q)


if (P.x == Q.x && P.y == Q.y) {
s = ((3 * P.x * P.x + a) * modInverse(2 * P.y, p)) % p;
} else {
s = ((Q.y - P.y) * modInverse(Q.x - P.x, p)) % p;
}

R.x = (s * s - P.x - Q.x + p) % p;


R.y = (s * (P.x - R.x) - P.y + p) % p;

return R;
}

// Function to perform scalar multiplication of a point


Point scalarMultiply(Point P, int k) {
Point Q = P;
Point R = { -1, -1 }; // Point at infinity
while (k > 0) {
if (k % 2 == 1)
R = addPoints(R, Q);
Q = addPoints(Q, Q);
k = k / 2;
}
return R;
}

int main() {
// Choose a base point on the curve (for demonstration purposes)
Point base = { 5, 1 };
// Choose a scalar (private key)
int scalar = 3;

// Compute the public key by scalar multiplication


Point publicKey = scalarMultiply(base, scalar);

cout << "Base Point: (" << base.x << ", " << base.y << ")" << endl;
cout << "Scalar: " << scalar << endl;
cout << "Public Key: (" << publicKey.x << ", " << publicKey.y << ")"
<< endl;

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy