0% found this document useful (0 votes)
3 views10 pages

Java Cryptography Algorithms Overview

The document outlines various cryptography and network security algorithms implemented in Java, including Rijndael (AES), RC4, RSA, Diffie-Hellman Key Exchange, SHA-1, and MD5. Each section includes a problem statement, Java program code, and the expected output. The author, D.N.S Hari Charan, is a student in the CSE-A class and presents these implementations over multiple weeks.

Uploaded by

22r21a0540
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)
3 views10 pages

Java Cryptography Algorithms Overview

The document outlines various cryptography and network security algorithms implemented in Java, including Rijndael (AES), RC4, RSA, Diffie-Hellman Key Exchange, SHA-1, and MD5. Each section includes a problem statement, Java program code, and the expected output. The author, D.N.S Hari Charan, is a student in the CSE-A class and presents these implementations over multiple weeks.

Uploaded by

22r21a0540
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

CRYPTOGRAPHY AND NETWORK SECURITY

WEEK-06(Rijndael Algorithm)
Name: D.N.S Hari Charan
Roll No: 22R21A0517
Class: CSE-A
Problem statement: Write a C/JAVA program to implement the Rijndael
algorithm logic.
PROGRAM:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class AES {
public static String asHex (byte buf[]) {
StringBuffer strbuf = new StringBuffer([Link] * 2);
for (int i = 0; i < [Link]; i++)
{
if (((int) buf[i] & 0xff) < 0x10)
[Link]("0");
[Link]([Link]((int) buf[i] & 0xff, 16));
}
return [Link]();
}
public static void main(String[] args) throws Exception {
String message="AES still rocks!!";
KeyGenerator kgen = [Link]("AES"); [Link](128);
SecretKey skey = [Link]();
byte[] raw = [Link]();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = [Link]("AES");
[Link](Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = [Link](([Link] == 0 ? message : args[0]).getBytes());
[Link]("encrypted string: " + asHex(encrypted));
[Link](Cipher.DECRYPT_MODE, skeySpec);
byte[] original = [Link](encrypted);
String originalString = new String(original);
[Link]("Original string: " + originalString + " " + asHex(original));
}}
OUTPUT:
CRYPTOGRAPHY AND NETWORK SECURITY
WEEK-07(RC4 Algorithm)
Name: D.N.S Hari Charan
Roll No: 22R21A0517
Class: CSE-A
Problem statement: Write the RC4 logic in Java Using Java cryptography;
encrypt the text “Hello world” using Blowfish. Create your own key using
Java key tool.
PROGRAM:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].Base64;
public class BlowFish{
public static void main(String[] args) throws Exception {
KeyGenerator keygenerator = [Link]("Blowfish");
SecretKey secretkey = [Link]();
Cipher cipher = [Link]("Blowfish");
String inputText = [Link]("Input your message: ");
[Link](Cipher.ENCRYPT_MODE, secretkey);
byte[] encrypted = [Link]([Link]());
[Link](Cipher.DECRYPT_MODE, secretkey);
byte[] decrypted = [Link](encrypted);
String encryptedBase64 = [Link]().encodeToString(encrypted);
[Link](
[Link](),"Encrypted text (Base64): " + encryptedBase64 +"\nDecrypted
text: " + new String(decrypted));
[Link](0);
}
}
OUTPUT:
CRYPTOGRAPHY AND NETWORK SECURITY
WEEK-08(RSA Algorithm)
Name: D.N.S Hari Charan
Roll No: 22R21A0517
Class: CSE-A
Problem statement: Write a Java program to implement RSA algorithm.
PROGRAM:
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
public class RSA
{
static Scanner sc = new Scanner([Link]);
public static void main(String[] args) {
[Link]("Enter a Prime number: ");
BigInteger p = [Link]();
[Link]("Enter another prime number: ");
BigInteger q = [Link]();
BigInteger n = [Link](q);
BigInteger n2 = [Link]([Link]).multiply([Link]([Link]));
BigInteger e = generateE(n2);
BigInteger d = [Link](n2);
[Link]("Encryption keys are: " + e + ", " + n);
[Link]("Decryption keys are: " + d + ", " + n);
}
public static BigInteger generateE(BigInteger fiofn)
{
int y, intGCD;
BigInteger e,gcd;
Random x = new Random();
do
{
y = [Link]([Link]()-1);
String z = [Link](y);
e = new BigInteger(z);
gcd = [Link](e);
intGCD = [Link]();
}
while(y <= 2 || intGCD != 1);
return e;
}}
OUTPUT:
CRYPTOGRAPHY AND NETWORK SECURITY
WEEK-9(Diffie-Hellman Key Exchange)
Name: D.N.S Hari Charan
Roll No: 22R21A0517
Class: CSE-A
Problem statement: Implement the Diffie-Hellman Key Exchange
mechanism using HTML and JavaScript.
PROGRAM:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class DiffeHellman {
public final static int pValue = 47;
public final static int gValue = 71;
public final static int XaValue = 9;
public final static int XbValue = 14;
public static void main(String[] args) throws Exception
{
BigInteger p = new BigInteger([Link](pValue));
BigInteger g = new BigInteger([Link](gValue));
BigInteger Xa = new BigInteger([Link](XaValue));
BigInteger Xb = new BigInteger([Link](XbValue));
createKey();
int bitLength = 512;
SecureRandom rnd = new SecureRandom();
p = [Link](bitLength, rnd);
g = [Link](bitLength, rnd);
createSpecificKey(p, g);
}
public static void createKey() throws Exception
{
KeyPairGenerator kpg = [Link]("DiffieHellman");
[Link](512);
KeyPair kp = [Link]();
KeyFactory kfactory = [Link]("DiffieHellman");
DHPublicKeySpec kspec = (DHPublicKeySpec)
[Link]([Link](),[Link]);
[Link]("Public key is: " +kspec);
}
public static void createSpecificKey(BigInteger p, BigInteger g) throws Exception
{
KeyPairGenerator kpg = [Link]("DiffieHellman");
DHParameterSpec param = new DHParameterSpec(p, g);
[Link](param);
KeyPair kp = [Link]();
KeyFactory kfactory = [Link]("DiffieHellman");
DHPublicKeySpec kspec = (DHPublicKeySpec)
[Link]([Link](),[Link]);
[Link]("\nPublic key is : " +kspec);
}
}
OUTPUT:
CRYPTOGRAPHY AND NETWORK SECURITY
WEEK-10(SHA-1 Algorithm)
Name: D.N.S Hari Charan
Roll No: 22R21A0517
Class: CSE-A
Problem statement: Calculate the message digest of a text using the SHA-1
algorithm in JAVA.
PROGRAM:
import [Link].*;
public class SHA1 {
public static void main(String[] a) {
try {
MessageDigest md = [Link]("SHA1");
[Link]("Message digest object info: ");
[Link](" Algorithm = " +[Link]());
[Link](" Provider = " +[Link]());
[Link](" ToString = " +[Link]());
String input = "";
[Link]([Link]());
byte[] output = [Link]();
[Link]("SHA1(\""+input+"\") = " +bytesToHex(output));
input = "abc";
[Link]([Link]());
output = [Link]();
[Link]("SHA1(\""+input+"\") = " +bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
[Link]([Link]());
output = [Link]();
[Link]("SHA1(\"" +input+"\") = " +bytesToHex(output));
}
catch (Exception e)
{
[Link]("Exception: " +e);
}
}
public static String bytesToHex(byte[] b)
{
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
StringBuffer buf = new StringBuffer();
for (int j=0; j<[Link]; j++)
{
[Link](hexDigit[(b[j] >> 4) & 0x0f]);
[Link](hexDigit[b[j] & 0x0f]); }
return [Link](); }
}
OUTPUT:
CRYPTOGRAPHY AND NETWORK SECURITY
WEEK-11(MD5 Algorithm)
Name: D.N.S Hari Charan
Roll No: 22R21A0517
Class: CSE-A
Problem statement: Calculate the message digest of a text using the MD5
algorithm in JAVA.
PROGRAM:
import [Link].*;
public class MD5
{
public static void main(String[] a)
{
try {
MessageDigest md = [Link]("MD5");
[Link]("Message digest object info: ");
[Link](" Algorithm = " +[Link]());
[Link](" Provider = " +[Link]());
[Link](" ToString = " +[Link]());
String input = "";
[Link]([Link]());
byte[] output = [Link]();
[Link]("MD5(\""+input+"\") = " +bytesToHex(output));
input = "abc";
[Link]([Link]());
output = [Link]();
[Link]("MD5(\""+input+"\") = " +bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
[Link]([Link]());
output = [Link]();
[Link]("MD5(\"" +input+"\") = " +bytesToHex(output));
}
catch (Exception e) {
[Link]("Exception: " +e);
}}
public static String bytesToHex(byte[] b)
{
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
StringBuffer buf = new StringBuffer();
for (int j=0; j<[Link]; j++)
{
[Link](hexDigit[(b[j] >> 4) & 0x0f]);
[Link](hexDigit[b[j] & 0x0f]); }
return [Link]();
}
}
OUTPUT:

Common questions

Powered by AI

RSA is an asymmetric encryption algorithm that uses a pair of keys – a public key for encryption and a private key for decryption. This differs significantly from symmetric encryption algorithms like AES and Blowfish, which use the same key for both encryption and decryption. The key management in RSA is more complex because it involves generating a pair of keys and securely distributing the public key while keeping the private key secret. RSA's use cases typically involve situations where secure key exchange is crucial, such as digital signatures and secure communications over an untrusted network .

Potential cryptographic vulnerabilities in using the MD5 algorithm include its susceptibility to collision attacks, where two different inputs can produce the same hash output. This vulnerability is exacerbated in the program by not incorporating random salts or additional security measures, making it inadequate for situations requiring high security, like digital certificates or SSL keys. Due to these weaknesses, MD5 is unsuitable for modern applications needing collision resistance .

The implementation of the SHA-1 algorithm in Java calculates a secure message digest by creating a MessageDigest instance with SHA-1 as the algorithm. This implementation processes the input data through the digest method, which applies SHA-1's hashing process to produce a fixed-size, 160-bit (20-byte) hash value, which is the message digest. The program updates the digest with data from the input, ensuring that any changes in the input will result in a different hash output, maintaining data integrity and detecting alterations .

The KeyPairGenerator class in the Diffie-Hellman key exchange implementation plays a critical role in generating public-private key pairs used for secure key exchange. It initializes with parameters that define the key's security level (such as bit length), ensuring that the generated keys are strong and secure. This initialization process, combined with subsequent key pair generation, allows the secure exchange of symmetric keys needed for encrypted communication, highlighting its importance in the cryptographic process .

Key pairs in RSA encryption enhance data security by splitting the encryption and decryption tasks between two mathematically related keys: a public key and a private key. The public key is openly shared to encrypt data, but only the owner of the corresponding private key can decrypt it. This separation allows secure communication without needing to exchange a shared secret key, mitigating risks of interception. This is a significant security advantage compared to single-key systems, where the same key must be securely shared and managed .

The Diffie-Hellman key exchange mechanism in the Java program is effective in securely exchanging keys over an insecure channel. The program uses a mix of hardcoded small prime numbers and dynamic generation of larger prime numbers for better security. The generation of public keys through secure methods and the creation of keys specific to the session minimizes the risk of interception and unauthorized access. However, the use of constants for prime numbers in some parts of the program could be less secure than generating primes dynamically in a real-world application .

Converting bytes to a hexadecimal string in cryptographic applications like SHA-1 and MD5 is important for readability and interoperability. It provides a human-readable representation of binary data, which is crucial for displaying and comparing hash outputs in logs, debugging, and verifying data integrity across different systems. Hexadecimal representation also facilitates easy copy and paste operations, which are critical in ensuring that checksums or hash values are correctly communicated and compared .

The use of Java's KeyGenerator in the Blowfish algorithm implementation is significant because it enables the automated generation of secure, random keys necessary for encryption. By using KeyGenerator, the program can create a key with the appropriate length and randomness, essential for ensuring the encryption's strength and security. This helps prevent predictable key patterns that could be exploited .

Using hardcoded prime numbers in the Diffie-Hellman key exchange program can lead to security vulnerabilities. Fixed prime values may reduce the randomness and unpredictability of the generated keys, making them susceptible to attacks if the values are known or predictable. This contrasts with dynamically generated primes during each session, which significantly enhances security by ensuring that key generation remains unpredictable and unique. For optimal security, especially in public implementations, using large, randomly generated primes is advisable .

The Rijndael algorithm, selected as the Advanced Encryption Standard (AES), ensures security through its structure and key length. It supports key sizes of 128, 192, and 256 bits, providing strong security against brute-force attacks due to the large key space. The algorithm involves multiple rounds of encryption, where each round consists of several steps: SubBytes, ShiftRows, MixColumns, and AddRoundKey. These steps involve substitution, permutation, diffusion, and key addition processes that are mathematically robust, ensuring that each bit of the plaintext and the key influences each bit of the ciphertext .

You might also like