Cryptolabda 5
Cryptolabda 5
Name: C. Rohith
Register No: 21BCE0810.
Slot: L13 + L14
Name: C. Rohith Register No: 21BCE0810
1. Implement the following techniques without using standard cryptographic library Functions
A.
Aim: To implement secure hash algorithm (SHA) without using standard cryptographic library functions in java.
Algorithm:
1. Define the initial hash values (H) and constants (K) for SHA-256.
3. Implement padding:
- Append '0' bits until the message length is congruent to 448 modulo 512.
- Extend the 16 32-bit words into 64 32-bit words using the SHA-256 expansion function.
7. Compute the final hash value by concatenating the working variables' values.
Code:
// SHA-256 functions
private static int rotr(int x, int n) {
return (x >>> n) | (x << (32 - n));
}
return h;
}
return hash;
}
Screenshot:
Name: C. Rohith Register No: 21BCE0810
Name: C. Rohith Register No: 21BCE0810
Output:
Result: We have successfully implemented Secure Hash Algorithm (SHA) without using standard library in java.
Name: C. Rohith Register No: 21BCE0810
Aim: To implement RSA based Digital signature Algorithm without using standard cryptographic library in java.
Algorithm:
Key Generation:
4. Choose a public exponent `e` such that `1 < e < phi(n)` and `e` is coprime with `phi(n)`.
5. Calculate the private exponent `d` as the modular multiplicative inverse of `e` modulo `phi(n)`.
Signing:
2. Calculate the signature `S` using the private key `(d, n)` by computing `S = m^d mod n`.
Verification:
3. Calculate `m'` using the public key `(e, n)` by computing `m' = S^e mod n`.
Code:
import java.math.BigInteger;
import java.security.SecureRandom;
generateKeyPair();
Name: C. Rohith Register No: 21BCE0810
// Output result
System.out.println("Original Message: " + originalMessage);
System.out.println("Signature: " + signature.toString());
System.out.println("Signature Verified: " + isVerified);
}
}
Screenshot:
Name: C. Rohith Register No: 21BCE0810
Output:
Result: We have successfully implemented RSA based Digital signature Algorithm without using standard
cryptographic library in java.
Name: C. Rohith Register No: 21BCE0810
Aim: To implement ElGamal based Digital signature Algorithm without using standard cryptographic library in java.
Algorithm:
Key Generation:
Signature Generation:
1. Choose a random integer \( k \) such that \( 1 < k < p - 1 \) and \( \text{gcd}(k, p - 1) = 1 \).
3. Compute \( s = (H(m) - x \cdot r) \cdot k^{-1} \mod (p - 1) \), where \( H(m) \) is the hash of the message \( m \).
Signature Verification:
Code:
import java.math.BigInteger;
import java.security.SecureRandom;
p = BigInteger.probablePrime(1024, random);
g = findGenerator();
Name: C. Rohith Register No: 21BCE0810
y = g.modPow(x, p);
}
r = g.modPow(k, p);
if (r.compareTo(BigInteger.ONE) < 0 ||
r.compareTo(p.subtract(BigInteger.ONE)) >= 0 ||
s.compareTo(BigInteger.ONE) < 0 ||
s.compareTo(p.subtract(BigInteger.ONE)) >= 0) {
return false;
}
BigInteger u1 = h.multiply(w).mod(p.subtract(BigInteger.ONE));
BigInteger u2 = r.multiply(w).mod(p.subtract(BigInteger.ONE));
return v1.equals(r);
}
generateKeyPair();
String originalMessage = "Hello, world!";
byte[] messageBytes = originalMessage.getBytes();
BigInteger[] signature = sign(messageBytes);
boolean isVerified = verify(messageBytes, signature);
System.out.println("Original Message: " + originalMessage);
System.out.println("Signature (r): " + signature[0]);
System.out.println("Signature (s): " + signature[1]);
System.out.println("Signature Verified: " + isVerified);
}
}
Screenshot:
Name: C. Rohith Register No: 21BCE0810
Name: C. Rohith Register No: 21BCE0810
Output:
Result: We have successfully implemented ElGamal based Digital signature Algorithm without using standard
cryptographic library in java.