IS Activity
IS Activity
IS Activity
Information Security
Submitted in partial fulfillment of the requirements
for the award of degree
Course in-charge
b) Substitution Cipher
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class SubstitutionCipher
{
private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the plaintext: ");
String plaintext = input.nextLine();
System.out.print("Enter the key (a string of 26 unique lowercase letters): ");
String key = input.nextLine();
String ciphertext = encrypt(plaintext, key);
System.out.println("Ciphertext: " + ciphertext);
String decryptedText = decrypt(ciphertext, key);
System.out.println("Decrypted Text: " + decryptedText);
}
public static String encrypt(String plaintext, String key)
{
Map<Character, Character> substitutionMap = new HashMap<>();
for (int i = 0; i < ALPHABET.length(); i++)
{
substitutionMap.put(ALPHABET.charAt(i), key.charAt(i));
}
StringBuilder ciphertext = new StringBuilder();
for (char c : plaintext.toCharArray())
{
if (Character.isLetter(c))
{
char substituted = substitutionMap.get(Character.toLowerCase(c));
if (Character.isUpperCase(c))
{
substituted = Character.toUpperCase(substituted);
}
ciphertext.append(substituted);
}
else
{
ciphertext.append(c);
}
}
return ciphertext.toString();
}
public static String decrypt(String ciphertext, String key)
{
Map<Character, Character> substitutionMap = new HashMap<>();
for (int i = 0; i < ALPHABET.length(); i++)
{
substitutionMap.put(key.charAt(i), ALPHABET.charAt(i));
}
StringBuilder decryptedText = new StringBuilder();
for (char c : ciphertext.toCharArray())
{
if (Character.isLetter(c))
{
char substituted = substitutionMap.get(Character.toLowerCase(c));
if (Character.isUpperCase(c))
{
substituted = Character.toUpperCase(substituted);
}
decryptedText.append(substituted);
}
else {
decryptedText.append(c);
}
}
return decryptedText.toString();
}
}
Output:
Enter the plaintext: Info
Enter the key (a string of 26 unique lowercase letters): mnbvcxzlkjhgfdsapoiuytrewq
Ciphertext: Kdxs
Decrypted Text: Info
c) Vignere cipher
import java.util.Scanner;
public class VigenereCipher
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the plaintext: ");
String plaintext = input.nextLine();
System.out.print("Enter the key (a string of characters): ");
String key = input.nextLine();
String ciphertext = encrypt(plaintext, key);
System.out.println("Ciphertext: " + ciphertext);
String decryptedText = decrypt(ciphertext, key);
System.out.println("Decrypted Text: " + decryptedText);
}
public static String encrypt(String plaintext, String key)
{
StringBuilder ciphertext = new StringBuilder();
int keyIndex = 0;
for (char c : plaintext.toCharArray())
{
if (Character.isLetter(c))
{
int shift = key.charAt(keyIndex) - 'a';
char substituted = shiftChar(c, shift);
ciphertext.append(substituted);
keyIndex = (keyIndex + 1) % key.length();
}
else
{
ciphertext.append(c);
}
}
return ciphertext.toString();
}
public static String decrypt(String ciphertext, String key)
{
StringBuilder decryptedText = new StringBuilder();
int keyIndex = 0;
for (char c : ciphertext.toCharArray())
{
if (Character.isLetter(c))
{
int shift = key.charAt(keyIndex) - 'a';
char substituted = shiftChar(c, -shift);
decryptedText.append(substituted);
keyIndex = (keyIndex + 1) % key.length();
}
else
{
decryptedText.append(c);
}
}
return decryptedText.toString();
}
private static char shiftChar(char c, int shift)
{
if (Character.isUpperCase(c))
{
return (char) ('A' + (c - 'A' + shift + 26) % 26);
}
else {
return (char) ('a' + (c - 'a' + shift + 26) % 26);
}
}
}
Output:
Enter the plaintext: Secure
Enter the key (a string of characters): key
Ciphertext: Ciaevc
Decrypted Text: Secure
j = 0;
for (i = 0; i < 256; i++)
{
j = (j + S[i] + T[i]) % 256;
// Swap values of S[i] and S[j]
unsigned char temp = S[i];
S[i] = S[j];
S[j] = temp;
}
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedBytes =cipher.doFinal(Base64.getDecoder().decode(encryptedText));
String decryptedText = new String(decryptedBytes);
System.out.println("Decrypted text: " + decryptedText);
}
}
Output:
Encrypted text: H43+hmnCzCpWWJSrTB6W2w4TMDuotX7b
Decrypted text: This is a test message.
4) Using Java Cryptography, encrypt the text “Hello world” using BlowFish. Create
your own key using Java keytool.
➢ Sure, here are the steps to encrypt the text "Hello world" using BlowFish algorithm
and a custom key generated using Java keytool:
Step 1: Generate a Secret Key using Java KeytoolJava Keytool is a command- line t
ool that can generate cryptographic keys and certificates. Here, we'll use it to generate
a secret key for BlowFish encryption.
Open a terminal window and type the following command:
key tool -genkey -alias mykey -keyalg Blowfish -keysize 128 -storetype JCEKS -keystore
mykeystore.jck
This command will generate a new secret key with the alias mykey, using the BlowFish
algorithm with a key size of 128 bits. The key will be stored in a Java Cryptography
Extension KeyStore (JCEKS) format in the file mykeystore.jck. During this process, you will
be prompted to enter a password to protect the key store.
Step 2: Implement the Encryption in Java
Next, we'll write a Java program to encrypt the text "Hello world" using the BlowFish
algorithm and the secret key we generated in step 1.
Import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.KeyStore;
import java.io.FileInputStream;
public class BlowFishEncryption {
public static void main(String[] args) throws Exception
{
KeyStore ks = KeyStore.getInstance("JCEKS");
FileInputStream fis = new FileInputStream("mykeystore.jck");
ks.load(fis, "keystore-password".toCharArray());
Key key = ks.getKey("mykey", "key-password".toCharArray());
String plaintext = "Hello world";
byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8);
Compile and run the Java program to encrypt the text "Hello world". The output will be a
hexadecimal string representing the encrypted text.
javac BlowFishEncryption.java
java BlowFishEncryption
Output: C2E2C050D83FD116
Output:
Original message: 123456789
Encrypted message:
4895022605174390876949854878055987974521778862663678143955606143682
0085761884622544762299006286604095683730075475192295988997179015644
2379787501437713126795032599574396738837391441505678956444749053636
9044961680475306722031251925920625403064531381094158785239887176458
0358085233994435541412188529030368922690
Decrypted message: 123456789
6) Implement the Diffie-Hellman Key Exchange mechanism using HTML and
JavaScript. Consider the end user as one of the parties (Alice) and the JavaScript
application as other party (bob).
<!DOCTYPE html>
<html>
<head>
<title>Diffie-Hellman Key Exchange</title>
<script type="text/javascript">
// Alice's variables
var aliceSecretKey;
var alicePublicKey;
// Bob's variables
var bobSecretKey;
var bobPublicKey;
// Shared variables
var sharedSecretKey;
function generateKeys() {
// Generate Alice's secret key
aliceSecretKey = Math.floor(Math.random() * 100) + 1;
function displayKeys() {
document.getElementById("aliceSecretKey").innerHTML = aliceSecretKey;
document.getElementById("alicePublicKey").innerHTML = alicePublicKey;
document.getElementById("bobSecretKey").innerHTML = bobSecretKey;
document.getElementById("bobPublicKey").innerHTML = bobPublicKey;
document.getElementById("sharedSecretKey").innerHTML = sharedSecretKey;
}
</script>
</head>
<body>
<h1>Diffie-Hellman Key Exchange</h1>
<button onclick="generateKeys(); displayKeys();">Generate Keys</button>
<br><br>
<table>
<tr>
<th>Alice</th>
<th>Bob</th>
<th>Shared Secret Key</th>
</td>
<td>
Secrete Key:<span id=”aliceSecretKey”></span><br>
Public Key:<span id=”alicePublicKey”></span>
</td>
<td>
<span id=”sharedSecretKey”></span>
</td>
</tr>
</table>
</body>
</html>
Output:
7) Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;