Program:
import java.io.*;
import java.math.BigInteger;
class Diffie
public static void main(String[]args)throws IOException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter prime number:");
BigInteger p=new BigInteger(br.readLine());
System.out.print("Enter primitive root of "+p+":");
BigInteger g=new BigInteger(br.readLine());
System.out.println("Enter value for x less than "+p+":");
BigInteger x=new BigInteger(br.readLine());
BigInteger R1=g.modPow(x,p);
System.out.println("R1="+R1);
System.out.print("Enter value for y less than "+p+":");
BigInteger y=new BigInteger(br.readLine());
BigInteger R2=g.modPow(y,p);
System.out.println("R2="+R2);
BigInteger k1=R2.modPow(x,p);
System.out.println("Key calculated at Sender's side:"+k1);
BigInteger k2=R1.modPow(y,p);
System.out.println("Key calculated at Receiver's side:"+k2);
System.out.println("deffie hellman secret key Encryption has Taken");
}
Output:
Program:
import java.io.*;
import java.util.*;
public class RailFenceCipher {
public static String encrypt(String plainText, int rails) {
char[][] railMatrix = new char[rails][plainText.length()];
for (int i = 0; i < rails; i++) {
for (int j = 0; j < plainText.length(); j++) {
railMatrix[i][j] = '\n'; } }
int dir = -1;
int row = 0, col = 0;
for (int i = 0; i < plainText.length(); i++) {
if (row == 0 || row == rails - 1) {
dir = -dir; }
railMatrix[row][col++] = plainText.charAt(i);
row += dir;}
StringBuilder cipherText = new StringBuilder();
for (int i = 0; i < rails; i++) {
for (int j = 0; j < plainText.length(); j++) {
if (railMatrix[i][j] != '\n') {
cipherText.append(railMatrix[i][j]);}} }
return cipherText.toString();}
public static String decrypt(String cipherText, int rails) {
char[][] railMatrix = new char[rails][cipherText.length()];
for (int i = 0; i < rails; i++) {
for (int j = 0; j < cipherText.length(); j++) {
railMatrix[i][j] = '\n'; } }
int dir = -1;
int row = 0, col = 0;
for (int i = 0; i < cipherText.length(); i++) {
if (row == 0 || row == rails - 1) {
dir = -dir;}
railMatrix[row][col++] = '*';
row += dir;}
int index = 0;
for (int i = 0; i < rails; i++) {
for (int j = 0; j < cipherText.length(); j++) {
if (railMatrix[i][j] == '*' && index < cipherText.length()) {
railMatrix[i][j] = cipherText.charAt(index++);}}}
StringBuilder plainText = new StringBuilder();
row = 0;
col = 0;
for (int i = 0; i < cipherText.length(); i++) {
if (row == 0 || row == rails - 1) {
dir = -dir; }
if (row < 0) {
row = 1; }
if (railMatrix[row][col] != '\n') {
plainText.append(railMatrix[row][col++]);}
row += dir;}
return plainText.toString();}
public static void main(String[] args) {
String text;
System.out.println("Enter the Plain text");
Scanner sc=new Scanner(System.in);
text=sc.nextLine();
System.out.println("Enter the Depth");
int rails=sc.nextInt();
String encrypted = encrypt(text, rails);
System.out.println("Encrypted: " + encrypted);
String decrypted = decrypt(encrypted, rails);
System.out.println("Decrypted: " +text);
Output:
Program:
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();
int num = 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 {
KeyGenerator kgen = KeyGenerator.getInstance("DES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(56, sr);
SecretKey skey = kgen.generateKey();
raw = skey.getEncoded();
return raw;
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = 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 {
SecretKeySpec skeySpec = 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:
Program:
import java.security.*;
public class SHA1 {
public static void main(String[] a)
try
MessageDigest md = MessageDigest.getInstance("SHA1");
String input = "srm";
md.update(input.getBytes());
byte[] output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") = " +bytesToHex(output));
input = "vec"; md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") = " +bytesToHex(output));
input = "valliammai";
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: