CNS-Lab-Final (1)
CNS-Lab-Final (1)
#include<stdlib.h>
#include<string.h>
int main()
char str1[11];
int i,len;
len=strlen(str);
for(i=0;i<len;i++)
str1[i]=str[i]^0;
printf("%c",str1[i]);
printf("\n");
Output:
./a.out
Hello World
PROGRAM:
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
char str1[11];
char str2[11];
char str3[11];
int i,len;
len = strlen(str);
for(i=0;i<len;i++)
str1[i] = str[i]&127;
-printf("%c",str1[i]);
printf("\n");
for(i=0;i<len;i++)
str3[i] = str2[i]^127;
printf("%c",str3[i]);
printf("\n");
Output:
Hello World
�}�(�*
b) Substitution Cipher
PROGRAM:
import java.io.*;
import java.util.*;
public class SubstitutionCipher {
static Scanner sc = new Scanner(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
// TODO code application logic here
String a = "abcdefghijklmnopqrstuvwxyz";
String b = "zyxwvutsrqponmlkjihgfedcba";
System.out.print("Enter any string: ");
String str = br.readLine();
String text = "";
char c;
for(int i=0;i<str.length();i++)
{
c = str.charAt(i);
int j = a.indexOf(c);
text = text+b.charAt(j);
}
System.out.println("The encrypted data is: " +text);
}
}
Output:
Enter any string: aceho
The encrypted data is: zxvsl
a) Hill Cipher
PROGRAM:
class Hill
{
static void getKeyMatrix(String key, int keyMatrix[][])
{
int k = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
keyMatrix[i][j] = (key.charAt(k)) % 65;
k++;
}
}
}
String CipherText="";
for (int i = 0; i < 3; i++)
CipherText += (char)(cipherMatrix[i][0] + 65);
System.out.print(" Ciphertext:" + CipherText);
}
public static void main(String[] args)
{
String message = "HAI";
String key = "GYBNQKURP";
HillCipher(message, key);
}
}
Output:
Ciphertext:YPA
import java.util.*;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
class DES{
myCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] myEncryptedBytes=myCipher.doFinal(myMessage);
myCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] myDecryptedBytes=myCipher.doFinal(myEncryptedBytes);
OUTPUT:
Encrypted - ~?0?2??↑*U??>▲????⌂?↑☻??W⌂?yR?
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, UnsupportedEncodingException {
cipher.init(Cipher.ENCRYPT_MODE, KS);
encodeToString(cipher.doFinal(msg.getBytes("UTF-8")));
return encryptedtext;
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
decode(encryptedtext);
cipher.init(Cipher.DECRYPT_MODE, KS);
String decryptedString =
return decryptedString;
OUTPUT
Message: Nirula
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
int i;
strbuf.append("0");
return strbuf.toString();
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
}
}
OUTPUT:
Program:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
// create a key
cipher.init(Cipher.ENCRYPT_MODE, secretkey);
// encrypt message
cipher.init(Cipher.DECRYPT_MODE, secretkey);
// decrypt message
OUTPUT:
8. RSA Algorithm
AIM: Write a Java program to implement RSA Algoithm.
PROGRAM:
import java.util.*;
import java.math.*;
class RSA
int p,q,n,z,d=0,e,i;
int msg=sc.nextInt();
double c;
BigInteger msgback;
p=sc.nextInt();
q=sc.nextInt();
n=p*q;
z=(p-1)*(q-1);
break;
for(i=0;i<=9;i++)
int x=1+(i*z);
d=x/e;
break;
c=(Math.pow(msg,e))%n;
System.out.println(c);
BigInteger N = BigInteger.valueOf(n);
BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println(msgback);
}
if(e==0)
return z;
else
return gcd(z%e,e);
OUTPUT:
11
13
the value of e = 7
Encrypted message is : -
85.0
Derypted message is : -
6
9. Diffie-Hellman
AIM: Implement the Diffie-Hellman Key Exchange mechanism. Consider the end user as one of
the parties (Alice) and the other party (bob).
PROGRAM:
import java.util.*;
class DiffieHellmanAlgorithmExample {
System.out.println("Both the users should be agreed upon the public keys G and P");
G = sc.nextLong();
P = sc.nextLong();
a = sc.nextLong();
b = sc.nextLong();
x = calculatePower(G, a, P);
y = calculatePower(G, b, P);
ka = calculatePower(y, a, P);
kb = calculatePower(x, b, P);
}
// create calculatePower() method to find the value of x ^ y mod P
long result = 0;
if (y == 1){
return x;
else{
return result;
OUTPUT
Both the users should be agreed upon the public keys G and P
10. SHA-1
AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
PROGRAM:
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
return hashtext;
catch (NoSuchAlgorithmException e) {
// Driver code
public static void main(String args[]) throws
NoSuchAlgorithmException
String s1 = "Nirul";
String s2 = "abcdefghijklmnopqrstuvwxyz";
OUTPUT:
Nirula : 6b205d4a49b77372135846f6425b18f1a3b29a25
abcdefghijklmnopqrstuvwxyz : 32d10c7b8cf96570ca04ce37f2a19d84240d3a89
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
// digest() method is called to calculate message digest
return hashtext;
catch (NoSuchAlgorithmException e) {
// Driver code
OUTPUT: