Lab Exercise
Lab Exercise
no:A13PCS008
Aim:
To implement a basic XOR encryption method for secure communication between a client and server,
while illustrating the principles of symmetric encryption and its vulnerabilities.
Coding:
package xor;
for(int i=0;i<plaintext.length();i++)
char encryptedChar=(char)(plaintext.charAt(i)^key.charAt(i%key.length()));
ciphertext.append(encryptedChar);
return ciphertext.toString();
for(int i=0;i<ciphertext.length();i++)
{
char decryptedChar=(char)(ciphertext.charAt(i)^key.charAt(i%key.length()));
decryptedtext.append(decryptedChar);
return decryptedtext.toString();
String key="mysecretkey";
System.out.println("Encrypted :"+encryptedText);
System.out.println("Decrypted :"+decryptedText);
Output:
Encrypted:+C"
Result:
The XOR program successfully encrypts and decrypts messages, enabling secure data transmission
between client and server.
Name:S.Matheswaran Reg.no:A13PCS008
Aim:
To implement a Caesar cipher for encrypting and decrypting messages, demonstrating the principles of
symmetric encryption and providing a foundational understanding of classical cryptographic techniques.
Coding:
shift = shift % 26 + 26; // Ensure shift is positive and within the range [0, 25]
if (Character.isLetter(i)) {
} else {
return encrypted.toString();
Output:
Result:
The Caesar cipher program successfully encrypts and decrypts messages using character shifting,
demonstrating basic symmetric encryption. It highlights the simplicity and inherent vulnerabilities of
classical cryptographic methods.
Name:S.Matheswaran Reg.no:A13PCS008
Analyze and implement a program for Encryption and Decryption using the HillCipher
Aim:
To implement the Hill cipher for encrypting and decrypting messages using linear algebra, demonstrating
the principles of symmetric encryption and matrix operations in cryptography.
Coding:
package hillcipher;
int k = 0;
k++;
int x, i, j;
cipherMatrix[i][j] = 0;
getKeyMatrix(key, keyMatrix);
String CipherText="";
HillCipher(message, key);
Output:
Ciphertext:NFT
Result:
The Hill cipher program successfully encrypts and decrypts messages using matrix transformations,
showcasing the effectiveness of linear algebra in symmetric encryption. It illustrates both the advantages
of increased security over classical ciphers and the computational complexity involved.
Name:S.Matheswaran Reg.no:A13PCS008
Aim:
To implement a substitution cipher for encrypting and decrypting messages by replacing characters,
demonstrating the principles of symmetric encryption and the concept of character mapping in
cryptography.
Coding:
package substitutioncipher;
import java.util.Scanner;
key = key.toUpperCase();
if (index != -1) {
ciphertext.append(key.charAt(index));
} else {
ciphertext.append(c);
return ciphertext.toString(); }
key = key.toUpperCase();
if (index != -1) {
plaintext.append(alphabet.charAt(index));
} else {
plaintext.append(c); }
return plaintext.toString();
Output:
Plaintext: MATHES
Encrypted: DQZITL
Decrypted: MATHES
Result:
The substitution cipher program effectively encrypts and decrypts messages through character
replacement, illustrating basic symmetric encryption techniques. It highlights the ease of implementation
and the vulnerabilities to frequency analysis and pattern recognition.
Name:S.Matheswaran Reg.no:A13PCS008
Aim:
To implement the Data Encryption Standard (DES) for encrypting and decrypting data, demonstrating the
principles of symmetric key cryptography and the use of Feistel network structure for enhanced security.
Coding:
package des;
import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
private static final byte[] initialization_vector = { 22, 33, 11, 44, 55, 99, 66, 77 };
try
encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
e.printStackTrace();
}
}
writeBytes(input, output);
writeBytes(input, output);
int readBytes = 0;
output.write(writeBuffer, 0, readBytes);
output.close();
input.close();
Output:
Result:
The DES program successfully encrypts and decrypts data using a 512-bit key and a Feistel network
structure, illustrating the principles of symmetric key cryptography. It showcases effective data
protection, while also highlighting vulnerabilities due to its relatively short key length in the context of
modern cryptographic standards.
Name: S.Matheswaran Reg.no:A13PCS008
Aim:
To implement the Diffie-Hellman key exchange protocol to enable secure key generation and sharing
over an insecure channel, demonstrating the principles of public key cryptography and the concept of
shared secret generation.
Coding:
package diffiehellman;
import java.math.BigInteger;
import java.security.SecureRandom;
BigInteger P = BigInteger.probablePrime(BIT_LENGTH,random);
Output:
P (Prime
Number) :9251286235985983310574457334147259434781711604346367716313683632446140068137
430634311305390488371052731062372401348019987079364921856945059440867579470869
G (Primitive
Root) :3217574186575186324075650600888175570783027953238455281666894954935814325273091
41588941041921506625346227321302437698479077458814518569606184497528494908
Result:
The Diffie-Hellman implementation successfully generates and shares a secret key between two parties
over an insecure channel, demonstrating the feasibility of secure key exchange.
Name: S.Matheswaran Reg.no:A13PCS008
Aim:
To implement a digital signature scheme to verify the authenticity and integrity of messages,
demonstrating the principles of asymmetric cryptography and the use of hash functions in
creating secure digital signatures.
Coding:
package digitalsignature;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.util.Scanner;
import javax.xml.bind.DatatypeConverter;
public static byte[] Create_Digital_Signature( byte[] input, PrivateKey Key) throws Exception
{
Signature signature = Signature.getInstance( SIGNING_ALGORITHM);
signature.initSign(Key);
signature.update(input);
return signature.sign();
signature.initVerify(key);
signature.update(input);
Output:
Signature Value:
667AF8CB9FFEBD447C77898318E6D607F4CE78654F70B987E5C98F10C6247B85358A3CE289B71
8295DCE441CB340E5DDE12CD751176C704A319089331353268DE4C8A9669C010C5E43B0BE100
D0C90CC6EA9E8BC266E80127F1BC38A6565792384D15EADFBF5026EE1F0D090C149E05B10EF5
B3B2C93D3A3DE6CFC26A36722DA1A0F739151A257329DBCF31FC1A14432E9D1A25BC0DAAB
5F70B6FD1E10FBF994FCA7A863A1C18B322EB3DC9BC9D31CBFD0FC7C7277CA57D2AC541991
41A92328E6D1601F1585C086EF5AB267C6E134312251018C4EAA7019D528472B9093EDF9B42FE4
879F9730D303BAEBE299C41B60C77E046CEF3C332AE0F84E7587500101
Verification: true
Result:
The digital signature program in Java effectively verifies document integrity and authenticity
using cryptographic hashing and private key signing.
4o mini
Name:S.Matheswaran Reg.no:A13PCS008
Aim:
To implement the RSA algorithm for encrypting and decrypting messages, demonstrating the principles
of asymmetric cryptography and the use of large prime numbers for secure key generation.
Coding:
package rsa;
import java.math.BigInteger;
import java.security.SecureRandom;
public RSA() {
modulus = p.multiply(q);
privateKey = publicKey.modInverse(phi);}
Output:
Encrypted Message:
41b6d5a18650713b0024eded4812edb114435814f1101b1dbd125e0221c60d3f83bfd3cba736e4ea05b1594
110b2f4c5ac3ec028e0e937ef11df6afe6689cd8941ec9343fdb735def286a67bc69d5317fb8f50206660cb50
19c97d07ee64ccf77b14e9bc1eddd5bce30d500edf02db1b81cdd0e2722c30e61eb69ca8f4a33752b5156bf4
6d4989aaf1c2c8df7cebe5968fd2637750da7284d986da687300a0cc8010c9abf31838dd0e2a1583dbddd94a
cc32bc01308eb5638c413d1b4bf7bef9d6c1862e532575345e3f2bfc67b724edaf018f2a05f1bb016f74d588a
4c6c972992d9261a678119ea992430b44c4c1c129de07e201dd08994f1ee5c69dbfefd7
Result:
The RSA implementation successfully encrypts and decrypts messages using a pair of public and private
keys, illustrating the effectiveness of asymmetric cryptography. It highlights the robustness of RSA in
secure communications, while emphasizing the importance of key size and prime factorization for
maintaining security.
Name:S.Matheswaran Reg.no:A13PCS008
Computation of Hash values using SHA Algorithm
Aim:
To implement the SHA-256 hashing algorithm to generate a fixed-size hash value for input data,
demonstrating the principles of cryptographic hashing and its application in ensuring data integrity and
authenticity.
Coding:
package sha256;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
MessageDigest md = MessageDigest.getInstance("SHA-256");
return md.digest(input.getBytes(StandardCharsets.UTF_8));
hexString.insert(0, '0');
}
return hexString.toString();
String s1 = "Mathes";
String s2 = "helloworld";
catch (NoSuchAlgorithmException e) {
}}}
Output:
Mathes : 4629cfeb13aca3658e4474f6f0eb93420dd5e5c3da7c1721ed36088db5d928db
helloworld : 936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af
Result:
The SHA-256 implementation successfully generates a unique, fixed-size hash value for input
data, illustrating the effectiveness of cryptographic hashing in ensuring data integrity. It
highlights the algorithm's resistance to collisions and pre-image attacks, making it suitable for
secure data verification and digital signatures.
Name:S.Matheswaran Reg.no:A13PCS008
Apply Watermark Techniques in Digital Images
Aim:
To implement a digital watermarking technique for embedding information within digital media,
demonstrating methods for ensuring copyright protection and data authenticity while minimizing
perceptual impact on the original content.
Coding:
package watermarking;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
File f = null;
try {
img = ImageIO.read(f);
}
catch (IOException e) {
System.out.println(e);
graphics.drawImage(img, 0, 0, null);
graphics.dispose();
try {
catch (IOException e) {
System.out.println(e);
Output:
Result:
The watermarking implementation successfully embeds information within digital media, effectively
demonstrating copyright protection and data authenticity. It preserves the perceptual quality of the
original content while providing a robust method for detecting unauthorized use or tampering.