Crypto DA
Crypto DA
RegNo: 21BCE0664
Assignment 4
int main() {
int p, q, n, e, d;
cout << "Enter two prime numbers (p and q): ";
cin >> p >> q;
cout << "Enter plaintext to be encrypted: ";
int plaintext;
cin >> plaintext;
// Generate keys
generateKeys(p, q, n, e, d);
// Encrypt plaintext
long long encrypted = encrypt(plaintext, e, n);
cout << "Encrypted text: " << encrypted << endl;
// Decrypt ciphertext
long long decrypted = decrypt(encrypted, d, n);
cout << "Decrypted text: " << decrypted << endl;
return 0;
}
Output
MD5
MD5 is a cryptographic hash function algorithm that takes the
message as input of any length and changes it into a fixed-length
message of 16 bytes. MD5 algorithm stands for the message-
digest algorithm. MD5 was developed as an improvement of MD4,
with advanced security purposes. The output of MD5 (Digest size)
is always 128 bits. MD5 was developed in 1991 by Ronald Rivest.
Use Of MD5 Algorithm: It is used for file authentication. In a web
application, it is used for security purposes. e.g. Secure password
of users etc. Using this algorithm, We can store our password in
128 bits format
Code
import java.security.MessageDigest;
import java.util.*;
public class sha
{
public static void main(String[] args)
{
try
{
Scanner obj = new Scanner(System.in);
System.out.print("Enter the message to be encrypted -> ");
String originalMessage = obj.nextLine();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(originalMessage.getBytes());
byte[] mdBytes = md.digest();
StringBuilder sb = new StringBuilder();
for (byte mdByte : mdBytes)
{
sb.append(Integer.toString((mdByte & 0xff) + 0x100, 16).substring(1));
}
String md5Hash = sb.toString();
System.out.println("\nOriginal Message -> " + originalMessage);
System.out.println("MD5 Hash Value -> " + md5Hash);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Output
SHA
SHA-512, or Secure Hash Algorithm 512, is a hashing algorithm
used to convert text of any length into a fixed-size string. Each
output produces a SHA-512 length of 512 bits (64 bytes). This
algorithm is commonly used for email addresses hashing,
password hashing, and digital record verification. SHA-512 is also
used in blockchain technology, with the most notable example
being the BitShares network. Key Features of the SHA-512 Hash
Function • Robustness and Resistance to various cryptographic
attacks • Logical and bitwise operations • Iterative Structure •
Versatility • Resistance to Birthday Attacks
Code
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
// Main Function
public static String encryptThisString(String input) {
try {
// getInstance() method is called with algorithm SHA-512
MessageDigest md = MessageDigest.getInstance("SHA-512");
// Driver code
public static void main(String args[]) {
System.out.println("HashCode Generated by SHA-512 for: ");
Output