0% found this document useful (0 votes)
31 views11 pages

Cycle2 Cns Record

Uploaded by

Damurnath raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views11 pages

Cycle2 Cns Record

Uploaded by

Damurnath raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

EX.No.

:2(a) DATA ENCRYPTION STANDARD (DES)

AIM:

To develop a program to implement Data Encryption Standard for encryption


and decryption.

ALGORITHM DESCRIPTION:

The Data Encryption Standard (DES) is a symmetric-key block cipher published by


the National Institute of Standards and Technology (NIST).
DES is an implementation of a Feistel Cipher. It uses 16 round Feistel structure. The
block size is 64-bit.
Though, key length is 64-bit, DES has an effective key length of 56 bits, since 8 of the
64 bits of the key are not used by the encryption algorithm (function as check bits only).

PROGRAM:
DES :-

import javax.swing.*;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random ;

class DES {
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;
String inputMessage,encryptedData,decryptedMessage;
public DES() {
try
{ generateSymmetricKey
();
inputMessage=JOptionPane.showInputDialog(null,"Enter message to encrypt");
byte[] ibyte = inputMessage.getBytes();
byte[] ebyte=encrypt(raw, ibyte);
String encryptedData = new String(ebyte);
System.out.println("Encrypted message "+encryptedData);
JOptionPane.showMessageDialog(null,"Encrypted Data "+"\n"+encryptedData);
byte[] dbyte= decrypt(raw,ebyte);
String decryptedMessage = new String(dbyte);
System.out.println("Decrypted message "+decryptedMessage);
JOptionPane.showMessageDialog(null,"Decrypted Data "+"\n"+decryptedMessage);
}
catch(Exception e)
{ System.out.println(e
);
}
}
void generateSymmetricKey()
{ try {
Random r = new Random();
intnum = r.nextInt(10000);
String knum = String.valueOf(num);
byte[] knumb = knum.getBytes();
skey=getRawKey(knumb);
skeyString = new String(skey);
System.out.println("DES Symmetric key = "+skeyString);
}
catch(Exception e)
{ System.out.println(e
);
}
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGeneratorkgen = KeyGenerator.getInstance("DES");
SecureRandomsr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(56, sr);
SecretKeyskey = kgen.generateKey();
raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpecskeySpec = new SecretKeySpec(raw, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpecskeySpec = new SecretKeySpec(raw, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,
skeySpec); byte[] decrypted =
cipher.doFinal(encrypted); return decrypted;
}
public static void main(String args[]) {
DES des = new DES();
}
}

OUTPUT:

RESULT:
Thus the program was executed and verified successfully.
EX.No.:2(b) RSA ALGORITHM

AIM:
Develop a program to implement RSA algorithm for encryption and decryption. This
cryptosystem is one the initial system. It remains most employed cryptosystem even today. The
system was invented by three scholars Ron Rivest, Adi Shamir, and Len Adleman and hence,
it is termed as RSA cryptosystem. The two aspects of the RSA cryptosystem, firstly generation
of key pair and secondly encryption-decryption algorithms

ALGORITHM DESCRIPTION:

Generation of RSA Key Pair


Each person or a party who desires to participate in communication using
encryption needs to generate a pair of keys, namely public key and private key.
The process followed in the generation of keys is described below −
Generate the RSA modulus (n)
Select two large primes, p and q.
Calculate n=p*q. For strong unbreakable encryption, let n be a large number,
typically a minimum of 512 bits.
Find Derived Number (e)
Number e must be greater than 1 and less than (p − 1)(q − 1).
There must be no common factor for e and (p − 1)(q − 1) except for 1. In other
words two numbers e and (p – 1)(q – 1) are coprime.
Form the public key
The pair of numbers (n, e) form the RSA public key and is made public.
Interestingly, though n is part of the public key, difficulty in factorizing a large
prime number ensures that attacker cannot find in finite time the two primes (p &
q) used to obtain n. This is strength of RSA.
Generate the private key
Private Key d is calculated from p, q, and e. For given n and e, there is
unique number d.
Number d is the inverse of e modulo (p - 1)(q – 1). This means that d is the
number less than (p - 1)(q - 1) such that when multiplied by e, it is equal to 1
modulo (p - 1)(q - 1).
This relationship is written mathematically as follows ed = 1 mod (p − 1)(q − 1)
The Extended Euclidean Algorithm takes p, q, and e as input and gives d as output.
PROGRAM :

import java.math.BigInteger;
import java.util.Random;
import java.io.*;
class rsaAlg
{

private BigInteger p, q, n, phi, e, d; /* public key components */


private int bitLen = 1024;

private int blkSz = 256; /* block size in bytes */


private Random rand;
/* convert bytes to string */
private static String bytesToString(byte[] encrypted)
{
String str = "";
for (byte b : encrypted)
{
str += Byte.toString(b);
}
return str;
}
/* encrypt message */
public byte[] encrypt(byte[] msg)
{
return (new BigInteger(msg)).modPow(e, n).toByteArray();
}
/* decrypt message */
public byte[] decrypt(byte[] msg)
{
return (new BigInteger(msg)).modPow(d, n).toByteArray();
}

/* calculate public key components p, q, n, phi, e, d


*/ public rsaAlg()
{
rand = new Random();

p = BigInteger.probablePrime(bitLen, rand);
q = BigInteger.probablePrime(bitLen, rand);
n = p.multiply(q);

phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); e

= BigInteger.probablePrime(bitLen/2, rand);

while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 &&


e.compareTo(phi) < 0)
{
e.add(BigInteger.ONE);
}
d = e.modInverse(phi);
}
public rsaAlg (BigInteger e, BigInteger d, BigInteger n)
{
this.e = e;

this.d = d;

this.n = n;

}
public static void main (String[] args) throws java.lang.Exception
{
rsaAlg rsaObj = new rsaAlg();
String msg = "Hello world! Security Laboratory";
System.out.println("simulation of RSA algorithm");
System.out.println("message(string) : " + msg);
System.out.println("message(bytes) : " +
bytesToString(msg.getBytes()));
/* encrypt test message */
byte[] ciphertext = rsaObj.encrypt(msg.getBytes());
System.out.println("ciphertext(bytes) : " + bytesToString(ciphertext));
/* decrypt ciphertext */
byte[] plaintext = rsaObj.decrypt(ciphertext);
System.out.println("plaintext(bytes) : " + bytesToString(plaintext));
System.out.println("plaintext(string) : " + new String(plaintext));
}}

stdin:
Standard input is empty

stdout:
SIMULATION OF RSA ALGORITHM
message(string) : Hello world! Security Laboratory
message(bytes) :
721011081081113211911111410810033328310199117114105116121327697981111149711611
1114121

ciphertext(bytes) : 0-119-42-9610376-981195-8810516-1281042-9-38-23-74856644-
2510705766-94-2310310452-2674-46125-13561120-2616122-111-507-117-5621-962-57-127-9-
93-537810108-50355612715733-31-10510732-106-6050-8666-2612570113357436822-46-
7169-402411558-42-11141-50-872210997-61-84-2627-84-36-55-56-1255440196847-60-
625450-19-35123-901541-126-109-7-5300-117618634-51-67-90-113121-88-10234124-61198-
24-26741167-568553-43-38873646-1105-21-77-1116358-120-47-98-1-110-97-31-125-113108-
91-998312234-27-29-11278-6011241-9944-6676-20-9225-12796-48-52-75-117-27-884-815-
648511153383676-158-116-8573120-87-4467104-9784108111-54830-61-90-38-11223-119-
4210510-18-20-4090-85-104-8561-120-49-12-120108-23-48-4945-102
plaintext(bytes) :
721011081081113211911111410810033328310199117114105116121327697981111149711611
1114121
plaintext(string) : Hello world! Security Laboratory

RESULT:
Thus the program was executed and verified successfully.
EX.No.:2(c) DIFFIEE HELLMAN KEY EXCHANGE ALGORITHM

AIM:

Develop a program to implement Diffie Hellman Key Exchange Algorithm for


encryption and Decryption.

ALGORITHM DESCRIPTION:
Diffie–Hellman key exchange (D–H) is a specific method of securely exchanging
cryptographic keys over a public channel and was one of the first public-key protocols. T
he Diffie–Hellman key exchange method allows two parties that have no prior knowledge
of each other to jointly establish a shared secret key over an insecure channel.
This key can then be used to encrypt subsequent communications using a symmetric key
cipher.

ALGORITHM

Global Public Elements:


Let q be a prime number and where < q and is a primitive root of q. 1.
User A Key Generation:
Select private XA where XA < q
XA
Calculate public YA where YA = mod q
2. User B Key Generation:
Select private XB where XB < q
Calculate public YB where YB = XB mod q
3. Calculation of Secret Key by User A
XA
K = (YB) mod q
4. Calculation of Secret Key by User B:

PROGRAM :
import java.util.*;
class diffieHellmanAlg
{
public static void main (String[] args) throws java.lang.Exception
{
int p = 11; /* publicly known (prime number) */
int g = 2; /* publicly known (primitive root) */
int x = 9; /* only Alice knows this secret */
int y = 4; /* only Bob knows this secret */
double aliceSends = (Math.pow(g,x))%p;

double bobComputes = (Math.pow(aliceSends,y))%p;


double bobSends = (Math.pow(g,y))%p;

double aliceComputes = (Math.pow(bobSends,x))%p;


double sharedSecret = (Math.pow(g,(x*y)))%p;
System.out.println("simulation of Diffie-Hellman key exchange algorithm");
System.out.println("aliceSends : " + aliceSends);
System.out.println("bobComputes : " + bobComputes);
System.out.println("bobSends : " + bobSends);
System.out.println("aliceComputes : " + aliceComputes);
System.out.println("sharedSecret : " + sharedSecret);
/* shared secrets should match and equality is transitive */

if ((aliceComputes == sharedSecret) && (aliceComputes == bobComputes))


System.out.println("success: shared secrets matches! " + sharedSecret);
else
System.out.println("error: shared secrets does not match");
}
}

stdin:
Standard input is empty

stdout:

simulation of Diffie-Hellman key exchange algorithm


aliceSends : 6.0

bobComputes : 9.0
bobSends : 5.0
aliceComputes : 9.0
sharedSecret : 9.0
success: shared secrets matches! 9.0

RESULT:
Thus the program was executed and verified successfully.

You might also like