Cycle2 Cns Record
Cycle2 Cns Record
AIM:
ALGORITHM DESCRIPTION:
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:
import java.math.BigInteger;
import java.util.Random;
import java.io.*;
class rsaAlg
{
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);
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:
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
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;
stdin:
Standard input is empty
stdout:
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.