You are on page 1of 14

1.

Modified Caesar-Cipher

AIM: Write a C program to Implement encryption and decryption substitution technique using Modified
Caesar-Cipher.
PROGRAM:
#include<stdio.h>
#include<conio.h>

int main()
{
char msg[100], ch;
int i, key;
printf("Enter a message to decrypt: ");
gets(msg);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; msg[i] != '\0'; ++i)
{
ch = msg[i];
if(ch>= 'a' &&ch<= 'z')
{
ch = ch - key;
if(ch< 'a')
{
ch = ch+26;
}
msg[i] = ch;
}
else if(ch>= 'A' &&ch<= 'Z')
{
ch = ch - key;
if(ch< 'A'){
ch = ch+26;
}
msg[i] = ch;
}
}
printf("Decrypted message: %s", msg);
return 0;
}

OUTPUT:
Enter a message to decrypt: HELLO
Enter key: 7
Decrypted message: AXEEH
2. One time Pad Algorithm
AIM: Write a Java program to Implement One Time Pad Cipher algorithm.
PROGRAM:
// Importing required classes
import java.io.*;

// Main class
public class OneTimePad {

// Method 1
// Returning encrypted text
public static String stringEncryption(String text,
String key)
{

// Initializing cipherText
String cipherText = "";

// Initialize cipher array of key length


// which stores the sum of corresponding no.'s
// of plainText and key.
int cipher[] = new int[key.length()];

for (int i = 0; i<key.length(); i++) {


cipher[i] = text.charAt(i) - 'A'
+ key.charAt(i)
- 'A';
}

// If the sum is greater than 25


// subtract 26 from it
// and store that resulting value
for (int i = 0; i<key.length(); i++) {
if (cipher[i] > 25) {
cipher[i] = cipher[i] - 26;
}
}

// Converting the no.'s into integers

// Convert these integers to corresponding


// characters and add them up to cipherText
for (int i = 0; i<key.length(); i++) {
int x = cipher[i] + 'A';
cipherText += (char)x;
}
// Returning the cipherText
return cipherText;
}

// Method 2
// Returning plain text
public static String stringDecryption(String s,
String key)
{
// Initializing plain text
String plainText = "";

// Initializing integer array of key length


// which stores difference
// of corresponding no.'s of
// each character of cipherText and key
int plain[] = new int[key.length()];

// Running for loop for each character


// subtracting and storing in the array
for (int i = 0; i<key.length(); i++) {
plain[i]
= s.charAt(i) - 'A'
- (key.charAt(i) - 'A');
}

// If the difference is less than 0


// add 26 and store it in the array.
for (int i = 0; i<key.length(); i++) {
if (plain[i] < 0) {
plain[i] = plain[i] + 26;
}
}

// Converting int to corresponding char


// add them up to plainText
for (int i = 0; i<key.length(); i++) {
int x = plain[i] + 'A';
plainText += (char)x;
}

// Returning plainText
return plainText;
}

// Method 3
// Main driver method
public static void main(String[] args)
{
// Declaring plain text
String plainText = "NAVRKR";

// Declaring key
String key = "MONKEY";

// Converting plain text to toUpperCase


// function call to stringEncryption
// with plainText and key as parameters
String encryptedText = stringEncryption(
plainText.toUpperCase(), key.toUpperCase());

// Printing cipher Text


System.out.println("Cipher Text - "
+ encryptedText);

// Calling above method to stringDecryption


// with encryptedText and key as parameters
System.out.println(
"Message - "
+ stringDecryption(encryptedText,
key.toUpperCase()));
}
}

OUTPUT:
Cipher Text - ZOIBOP
Message - NAVRKR
3. Rail Fence Transposition

AIM: Write a Java program to Implement Rail Fence transposition technique.


PROGRAM:
import java.util.*;
class RailFence{
public static void main(String args[])throws Exception
{
RailFenceBasic rf=new RailFenceBasic();
Scanner scn=new Scanner(System.in);
int depth;

String plainText,cipherText,decryptedText;

System.out.println("Enter plain text:");


plainText=scn.nextLine();

System.out.println("Enter depth for Encryption:");


depth=scn.nextInt();

cipherText=rf.Encryption(plainText,depth);
System.out.println("Encrypted text is:\n"+cipherText);

decryptedText=rf.Decryption(cipherText, depth);

System.out.println("Decrypted text is:\n"+decryptedText);

}
}

class RailFenceBasic{
int depth;
String Encryption(String plainText,int depth)throws Exception
{
int r=depth,len=plainText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;

String cipherText="";

for(int i=0;i< c;i++)


{
for(int j=0;j< r;j++)
{
if(k!=len)
mat[j][i]=plainText.charAt(k++);
else
mat[j][i]='X';
}
}
for(int i=0;i< r;i++)
{
for(int j=0;j< c;j++)
{
cipherText+=mat[i][j];
}
}
return cipherText;
}

String Decryption(String cipherText,int depth)throws Exception


{
int r=depth,len=cipherText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;

String plainText="";

for(int i=0;i< r;i++)


{
for(int j=0;j< c;j++)
{
mat[i][j]=cipherText.charAt(k++);
}
}
for(int i=0;i< c;i++)
{
for(int j=0;j< r;j++)
{
plainText+=mat[j][i];
}
}
return plainText;
}
}

OUTPUT:
Enter plain text:
NAVRKR
Enter depth for Encryption:
3
Encrypted text is:
NRAKVR
Decrypted text is:
NAVRKR
4. RSA Algorithm
AIM: Write a Java program to Implement RSA Algorithm.
PROGRAM:
import java.util.*;
import java.math.*;
class RSA
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int p,q,n,z,d=0,e,i;
System.out.println("Enter the number to be encrypted and decrypted");
int msg=sc.nextInt();
double c;
BigInteger msgback;
System.out.println("Enter 1st prime number p");
p=sc.nextInt();
System.out.println("Enter 2nd prime number q");
q=sc.nextInt();
n=p*q;
z=(p-1)*(q-1);
System.out.println("the value of z = "+z);
for(e=2;e<z;e++)
{
if(gcd(e,z)==1) // e is for public key exponent
{
break;
}
}
System.out.println("the value of e = "+e);
for(i=0;i<=9;i++)
{
int x=1+(i*z);
if(x%e==0) //d is for private key exponent
{
d=x/e;
break;
}
}
System.out.println("the value of d = "+d);
c=(Math.pow(msg,e))%n;
System.out.println("Encrypted message is : -");
System.out.println(c);
//converting int value of n to BigInteger
BigInteger N = BigInteger.valueOf(n);
//converting float value of c to BigInteger
BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println("Derypted message is : -");
System.out.println(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
3
Enter 2nd prime number q
11
the value of z = 20
the value of e = 3
the value of d = 7
Encrypted message is : -
30.0
Derypted message is : -
24
5. SHA-1
AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
PROGRAM:
import java.security.*;
public class SHA1 {
public static void main(String[] a) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
System.out.println("Message digest object info: ");
System.out.println(" Algorithm = " +md.getAlgorithm());
System.out.println(" Provider = " +md.getProvider());
System.out.println(" ToString = " +md.toString());
String input = "";
md.update(input.getBytes());
byte[] output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") = " +bytesToHex(output));
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") = " +bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\"" +input+"\") = " +bytesToHex(output));
System.out.println(""); }
catch (Exception e) {
System.out.println("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<b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]); }
return buf.toString(); }
}
OUTPUT:
Message digest object info:
Algorithm = SHA1
Provider = SUN version 11
ToString = SHA1 Message Digest from SUN, <initialized>

SHA1("") = DA39A3EE5E6B4B0D3255BFEF95601890AFD80709

SHA1("abc") = A9993E364706816ABA3E25717850C26C9CD0D89D

SHA1("abcdefghijklmnopqrstuvwxyz") = 32D10C7B8CF96570CA04CE37F2A19D84240D3A89
5. Message Digest Algorithm5 (MD5)
AIM: Calculate the message digest of a text using the MD5 algorithm in JAVA.
PROGRAM:
import java.security.*;
public class MD5 {
public static void main(String[] a) {
// TODO code application logic here
try {
MessageDigest md = MessageDigest.getInstance("MD5");
System.out.println("Message digest object info: ");
System.out.println(" Algorithm = " +md.getAlgorithm());
System.out.println(" Provider = " +md.getProvider());
System.out.println(" ToString = " +md.toString());
String input = "";
md.update(input.getBytes());
byte[] output = md.digest();
System.out.println();
System.out.println("MD5(\""+input+"\") = " +bytesToHex(output));
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("MD5(\""+input+"\") = " +bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("MD5(\"" +input+"\") = "
+bytesToHex(output)); System.out.println("");
}
catch (Exception e) {
System.out.println("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<b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]); }
return buf.toString(); } }

OUTPUT:
Message digest object info:
Algorithm = MD5
Provider = SUN version 11
ToString = MD5 Message Digest from SUN, <initialized>
MD5("") = D41D8CD98F00B204E9800998ECF8427E
MD5("abc") = 900150983CD24FB0D6963F7D28E17F72
MD5("abcdefghijklmnopqrstuvwxyz") = C3FCD3D76192E4007DFB496CCA67E13B
6. Digital Signature
AIM: To write a program to implement the digital signature scheme in java
PROGRAM:
import java.util.*;
import java.math.BigInteger;
class dsaAlg
{
final static BigInteger one = new BigInteger("1");
final static BigInteger zero = new BigInteger("0");
/* incrementally tries for next prime */
public static BigInteger getNextPrime(String ans)
{
BigInteger test = new BigInteger(ans);
while (!test.isProbablePrime(99))
{
test = test.add(one);
}
return test;
}
/* finds largest prime factor of n */
public static BigInteger findQ(BigInteger n)
{
BigInteger start = new BigInteger("2");
while (!n.isProbablePrime(99))
{
while (!((n.mod(start)).equals(zero)))
{
start = start.add(one);
}
n = n.divide(start);
}
return n;
}
/* finds a generator mod p */
public static BigInteger getGen(BigInteger p, BigInteger q, Random r)
{
BigInteger h = new BigInteger(p.bitLength(), r);
h = h.mod(p);
return h.modPow((p.subtract(one)).divide(q), p);
}
public static void main (String[] args) throws java.lang.Exception
{
Random randObj = new Random();
/* establish the global public key components */
BigInteger p = getNextPrime("10600"); /* approximate prime */
BigInteger q = findQ(p.subtract(one));
BigInteger g = getGen(p,q,randObj);
/* public key components */
System.out.println("Digital Signature Algorithm");
System.out.println("global public key components are:");
System.out.println("p is: " + p);
System.out.println("q is: " + q);
System.out.println("g is: " + g);
/* find the private key */
BigInteger x = new BigInteger(q.bitLength(), randObj);
x = x.mod(q);
/* corresponding public key */
BigInteger y = g.modPow(x,p);
/* random value message */
BigInteger k = new BigInteger(q.bitLength(), randObj);
k = k.mod(q);
/* randomly generated hash value and digital signature */
BigInteger r = (g.modPow(k,p)).mod(q);
BigInteger hashVal = new BigInteger(p.bitLength(), randObj);
BigInteger kInv = k.modInverse(q);
BigInteger s = kInv.multiply(hashVal.add(x.multiply(r)));
s = s.mod(q);
/* secret information */
System.out.println("secret information are:");
System.out.println("x (private) is: " + x);
System.out.println("k (secret) is: " + k);
System.out.println("y (public) is: " + y);
System.out.println("h (rndhash) is: " + hashVal);
System.out.println("Generating digital signature:");
System.out.println("r is : " + r);
System.out.println("s is : " + s);
/*verify the digital signature */
BigInteger w = s.modInverse(q);
BigInteger u1 = (hashVal.multiply(w)).mod(q);
BigInteger u2 = (r.multiply(w)).mod(q);
BigInteger v = (g.modPow(u1,p)).multiply(y.modPow(u2,p));
v = (v.mod(p)).mod(q);
System.out.println("verifying digital signature (checkpoints):");
System.out.println("w is : " + w);
System.out.println("u1 is : " + u1);
System.out.println("u2 is : " + u2);
System.out.println("v is : " + v);
if (v.equals(r))
{
System.out.println("success: digital signature is verified! " + r);
}
else
{
System.out.println("error: incorrect digital signature");
}
}
}
OUTPUT:
Digital Signature Algorithm
global public key components are:
p is: 10601
q is: 53
g is: 7615
secret information are:
x (private) is: 4
k (secret) is: 18
y (public) is: 8172
h (rndhash) is: 8680
Generating digital signature:
r is : 52
s is : 5
verifying digital signature (checkpoints):
w is : 32
u1 is : 40
u2 is : 21
v is : 52
success: digital signature is verified! 52

You might also like