EXPERIMENT 1
1. Write a C program that contains a string (char pointer) with a value \Hello World’. The
program should XOR each character in this string with 0 and displays the result.
PROGRAM:
#include<stdlib.h>
main()
{
char str[]="Hello World";
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:
Hello World
2. Write a C program that contains a string (char pointer) with a value ‘Hello world’.
The program should AND or and XOR each character in this string with 127 and display the
result.
PROGRAM:
#include <stdio.h>
#include<stdlib.h>
void main()
{
char str[]="Hello World";
char str1[11];
char str2[11];
int i,len;
len = strlen(str);
for(i=0;i<len;i++)
{
str1[i] = (str[i]&127) | (str[i]^127);
printf("%d",str1[i]);
}
printf("\n");
for(i=0;i<len;i++)
{
str2[i] = (str[i]&127) & (str[i]^127);
printf("%d",str2[i]);
}
printf("\n");
}
OUTPUT:
127127127127127127127127127127127
00000000000
3. Write a Java program to perform encryption and decryption using the following
Algorithms
a. Ceaser cipher b. Substitution cipher c. Hill Cipher
PROGRAM:
a) Ceaser Cipher
import [Link];
import [Link];
import [Link];
import [Link];
public class CeaserCipher {
public static void main(String[] args) throws IOException {
String str = "Hello World";
int key=5;
String encrypted = encrypt(str, key);
[Link]("\nEncrypted String is: " +encrypted);
String decrypted = decrypt(encrypted, key);
[Link]("\nDecrypted String is: " +decrypted);
[Link]("\n");
}
public static String encrypt(String str, int key) {
String encrypted = "";
for(int i = 0; i < [Link](); i++) {
int c = [Link](i);
if ([Link](c)) {
c = c + (key % 26);
if (c > 'Z')
c = c - 26;
}
else if ([Link](c)) {
c = c + (key % 26);
if (c > 'z')
c = c - 26;
}
encrypted += (char) c;
}
return encrypted;
}
public static String decrypt(String str, int key) {
String decrypted = "";
for(int i = 0; i < [Link](); i++) {
int c = [Link](i);
if ([Link](c)) {
c = c - (key % 26);
if (c < 'A')
c = c + 26;
}
else if ([Link](c)) {
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return decrypted;
}
}
OUTPUT:
Encrypted String is: Mjqqt Btwqi
Decrypted String is: Hello World
b) Substitution Cipher
PROGRAM:
import [Link].*;
import [Link].*;
public class SubstitutionCipher {
public static void main(String[] args) throws IOException {
String str = "hello world";
String a = "abcdefghijklmnopqrstuvwxyz ";
String b = "zyxwvutsrqponmlkjihgfedcba ";
String encrypt = "";
String decrypt = "";
char c;
for(int i=0;i<[Link]();i++)
{
c = [Link](i);
int j = [Link](c);
encrypt = encrypt+[Link](j);
}
[Link]("The encrypted data is: " +encrypt);
for(int i=0;i<[Link]();i++)
{
c = [Link](i);
int j = [Link](c);
decrypt = decrypt+[Link](j);
}
[Link]("The decrypted data is: " +decrypt);
}
}
OUTPUT:
The encrypted data is: svool dliow
The decrypted data is: hello world
c) Hill Cipher
PROGRAM:
import [Link].*;
import [Link].*;
import [Link].*;
public class HillCipher {
static int[][] decrypt = new int[3][1];
static int[][] b = new int[3][3];
static int[][] mes = new int[3][1];
static int[][] res = new int[3][1];
static int a[][] = {{1,2,3},{0,1,4},{5,6,0}};
public static void main(String[] args) throws IOException {
getkeymes();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int k=0;k<3;k++) {
res[i][j]=res[i][j]+a[i][k]*mes[k][j]; }
[Link]("\nEncrypted string is : ");
for(int i=0;i<3;i++) {
[Link]((char)(res[i][0]%26+97));
res[i][0]=res[i][0];
}
inverse();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int k=0;k<3;k++) {
decrypt[i][j] = decrypt[i][j]+b[i][k]*res[k][j]; }
[Link]("\nDecrypted string is : ");
for(int i=0;i<3;i++){
[Link]((char)(decrypt[i][0]%26+97));
}
[Link]("\n");
}
public static void getkeymes() throws IOException {
String msg = "cse";
for(int i=0;i<3;i++)
mes[i][0] = [Link](i)-97;
}
public static void inverse() {
int p, q;
int[][] c = a;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
//a[i][j]=[Link]();
if(i==j)
b[i][j]=1;
else b[i][j]=0;
}
for(int k=0;k<3;k++) {
for(int i=0;i<3;i++) {
p = c[i][k];
q = c[k][k];
for(int j=0;j<3;j++) {
if(i!=k) {
c[i][j] = c[i][j]*q-p*c[k][j];
b[i][j] = b[i][j]*q-p*b[k][j];
}}}}
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
b[i][j] = b[i][j]/c[i][i]; }
[Link]("");
[Link]("\nInverse Matrix is : ");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++)
[Link](b[i][j] + " ");
[Link]("\n"); }
}
}
OUTPUT:
Encrypted string is : yio
Inverse Matrix is :
-24 18 5
20 -15 -4
-5 4 1
Decrypted string is : cse
4. Write a C/JAVA program to implement the DES algorithm logic.
PROGRAM:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link] ;
public class DES {
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;
String inputMessage,encryptedData,decryptedMessage;
public DES() {
try {
generateSymmetricKey();
inputMessage= "CSE-D";
byte[] ibyte = [Link]();
byte[] ebyte=encrypt(raw, ibyte);
String encryptedData = new String(ebyte);
[Link]("Encrypted message "+encryptedData);
byte[] dbyte= decrypt(raw,ebyte);
String decryptedMessage = new String(dbyte);
[Link]("Decrypted message "+decryptedMessage);
catch(Exception e) {
[Link](e);
void generateSymmetricKey() {
try {
Random r = new Random();
int num = [Link](10000);
String knum = [Link](num);
byte[] knumb = [Link]();
skey=getRawKey(knumb);
skeyString = new String(skey);
[Link]("DES Symmetric key = "+skeyString);
catch(Exception e) {
[Link](e);
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = [Link]("DES");
SecureRandom sr = [Link]("SHA1PRNG");
[Link](seed);
[Link](56, sr);
SecretKey skey = [Link]();
raw = [Link]();
return raw;
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
Cipher cipher = [Link]("DES");
[Link](Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = [Link](clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
Cipher cipher = [Link]("DES");
[Link](Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = [Link](encrypted);
return decrypted;
public static void main(String args[]) {
DES des = new DES();
OUTPUT:
DES Symmetric key = Ly�aTp�
Encrypted message ��߸��/
Decrypted message CSE-D
5. Write a C/JAVA program to implement the Blowfish algorithm logic.
PROGRAM:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link] ;
public class BLOWFISH {
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;
String inputMessage,encryptedData,decryptedMessage;
public BLOWFISH() {
try {
generateSymmetricKey();
inputMessage= "CSE-D";
byte[] ibyte = [Link]();
byte[] ebyte=encrypt(raw, ibyte);
String encryptedData = new String(ebyte);
[Link]("Encrypted message "+encryptedData);
byte[] dbyte= decrypt(raw,ebyte);
String decryptedMessage = new String(dbyte);
[Link]("Decrypted message "+decryptedMessage);
catch(Exception e) {
[Link](e);
}
void generateSymmetricKey() {
try {
Random r = new Random();
int num = [Link](10000);
String knum = [Link](num);
byte[] knumb = [Link]();
skey=getRawKey(knumb);
skeyString = new String(skey);
[Link]("BLOWFISH Symmetric key = "+skeyString);
catch(Exception e) {
[Link](e);
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = [Link]("BLOWFISH");
SecureRandom sr = [Link]("SHA1PRNG");
[Link](seed);
[Link](32, sr);
SecretKey skey = [Link]();
raw = [Link]();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "BLOWFISH");
Cipher cipher = [Link]("BLOWFISH");
[Link](Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = [Link](clear);
return encrypted;
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "BLOWFISH");
Cipher cipher = [Link]("BLOWFISH");
[Link](Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = [Link](encrypted);
return decrypted;
public static void main(String args[]) {
BLOWFISH blowfish = new BLOWFISH();
OUTPUT:
BLOWFISH Symmetric key = ��4~
Encrypted message Z�����0!
Decrypted message CSE-D
6. Write a C/JAVA program to implement the Rijndael algorithm logic.
PROGRAM:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link] ;
public class AES {
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;
String inputMessage,encryptedData,decryptedMessage;
public AES() {
try {
generateSymmetricKey();
inputMessage= "CSE-D";
byte[] ibyte = [Link]();
byte[] ebyte=encrypt(raw, ibyte);
String encryptedData = new String(ebyte);
[Link]("Encrypted message "+encryptedData);
byte[] dbyte= decrypt(raw,ebyte);
String decryptedMessage = new String(dbyte);
[Link]("Decrypted message "+decryptedMessage);
catch(Exception e) {
[Link](e);
void generateSymmetricKey() {
try {
Random r = new Random();
int num = [Link](10000);
String knum = [Link](num);
byte[] knumb = [Link]();
skey=getRawKey(knumb);
skeyString = new String(skey);
[Link]("AES Symmetric key = "+skeyString);
catch(Exception e) {
[Link](e);
}
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = [Link]("AES");
SecureRandom sr = [Link]("SHA1PRNG");
[Link](seed);
[Link](128, sr);
SecretKey skey = [Link]();
raw = [Link]();
return raw;
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = [Link]("AES");
[Link](Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = [Link](clear);
return encrypted;
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = [Link]("AES");
[Link](Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = [Link](encrypted);
return decrypted;
public static void main(String args[]) {
AES aes = new AES();
OUTPUT:
AES Symmetric key = "��漡�r69Qf2w�
Encrypted message �\I��u�L,g�*�
Decrypted message CSE-D
7. Write the RC4 logic in Java Using Java cryptography; encrypt the text “Hello world”
using Blowfish.
PROGRAM:
// RC4 key generation with Blowfish encryption
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link] ;
public class BLOWFISH {
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;
String inputMessage,encryptedData,decryptedMessage;
public BLOWFISH() {
try {
generateSymmetricKey();
inputMessage= " CSED ";
byte[] ibyte = [Link]();
byte[] ebyte=encrypt(raw, ibyte);
String encryptedData = new String(ebyte);
[Link]("Encrypted message "+encryptedData);
byte[] dbyte= decrypt(raw,ebyte);
String decryptedMessage = new String(dbyte);
[Link]("Decrypted message "+decryptedMessage);
catch(Exception e) {
[Link](e);
void generateSymmetricKey() {
try {
Random r = new Random();
int num = [Link](10000);
String knum = [Link](num);
byte[] knumb = [Link]();
skey=getRawKey(knumb);
skeyString = new String(skey);
[Link]("RC4 Symmetric key = "+skeyString);
catch(Exception e) {
[Link](e);
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = [Link]("RC4");
SecureRandom sr = [Link]("SHA1PRNG");
[Link](seed);
[Link](40, sr);
SecretKey skey = [Link]();
raw = [Link]();
return raw;
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "BLOWFISH");
Cipher cipher = [Link]("BLOWFISH");
[Link](Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = [Link](clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "BLOWFISH");
Cipher cipher = [Link]("BLOWFISH");
[Link](Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = [Link](encrypted);
return decrypted;
public static void main(String args[]) {
BLOWFISH blowfish = new BLOWFISH();
OUTPUT:
RC4 Symmetric key = �^•
Encrypted message ���9�If
Decrypted message CSED
8) Write a Java program to implement RSA algorithm.
PROGRAM:
import [Link].*;
import [Link].*;
public class RSA
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int p,q,n,z,d=0,e,i;
[Link]("Enter the number to be encrypted and decrypted: ");
int msg=[Link]();
int c;
BigInteger msgback;
[Link]("Enter 1st prime number p: ");
p=[Link]();
[Link]("Enter 2nd prime number q: ");
q=[Link]();
n=p*q;
z=(p-1)*(q-1);
for(e=2;e<z;e++)
{
if(gcd(e,z)==1)
{
break;
}
}
[Link]("Public key (e,n): ");
[Link]("("+e+","+n+")");
for(i=0;i<=9;i++)
{
int x=1+(i*z);
if(x%e==0)
{
d=x/e;
break;
}
}
c=(int)([Link](msg,e))%n;
[Link]("Encrypted message is : ");
[Link](c);
BigInteger N = [Link](n);
BigInteger C = [Link](c).toBigInteger();
msgback = ([Link](d)).mod(N);
[Link]("Derypted message is : ");
[Link](msgback);
}
static int gcd(int e, int z)
{
if(e==0)
return z;
else
return gcd(z%e,e);
}
}
OUTPUT:
Enter the number to be encrypted and decrypted: 24
Enter 1st prime number p: 5
Enter 2nd prime number q: 11
Public key (e,n): (3,55)
Encrypted message is : 19
Derypted message is : 24
9. Implement the Diffie-Hellman Key Exchange mechanism using HTML and
JavaScript.
PROGRAM:
<html>
<body>
<script>
var q,g,a, b;
p=prompt("Enter the p value");
g=prompt("Enter the Alpha value - Primitive Root of p");
a=prompt("Enter the Private key of Alice");
b=prompt("Enter the Private key of Bob");
q=parseInt(p);
alpha=parseInt(g);
Xa=parseInt(a);
Xb=parseInt(b);
Ya = [Link](alpha,Xa)%q;
Yb = [Link](alpha,Xb)%q;
K_A = [Link](Yb,Xa)%q;
K_B = [Link](Ya,Xb)%q;
if(K_A==K_B)
[Link]("ALice and Bob can communicate with each other!!!
<br> Secret Key K:");
[Link](K_A);
else
alert("ALice and Bob cannot communicate with each other!!! <br> Please
enter correct alpha value(Primitive root of P)" );
</script>
</body>
</html>
OUTPUT:
Enter the p value 23
Enter the Alpha value - Primitive Root of p 5
Enter the Private key of Alice 6
Enter the Private key of Bob 15
ALice and Bob can communicate with each other!!!
Secret Key K: 2
10. 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[] args) {
try {
MessageDigest md = [Link]("SHA1");
String input = "abc";
[Link]([Link]());
byte[] output = [Link]();
[Link]();
[Link]("SHA1(\""+input+"\") = " +bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
[Link]([Link]());
output = [Link]();
[Link]();
[Link]("SHA1(\"" +input+"\") = " +bytesToHex(output));
[Link](""); }
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:
SHA1("abc") = A9993E364706816ABA3E25717850C26C9CD0D89D
SHA1("abcdefghijklmnopqrstuvwxyz") =
32D10C7B8CF96570CA04CE37F2A19D84240D3A89
11. Calculate the message digest of a text using the MD5 algorithm in JAVA.
PROGRAM:
import [Link].*;
public class MD5
{
public static void main(String[] args) {
try {
MessageDigest md = [Link]("MD5");
String input = "abc";
[Link]([Link]());
byte[] output = [Link]();
[Link]();
[Link]("MD5(\""+input+"\") = " +bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
[Link]([Link]());
output = [Link]();
[Link]();
[Link]("MD5(\"" +input+"\") = " +bytesToHex(output));
[Link](""); }
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:
MD5("abc") = 900150983CD24FB0D6963F7D28E17F72
MD5("abcdefghijklmnopqrstuvwxyz") = C3FCD3D76192E4007DFB496CCA67E13B