Substitution Cipher (Java)

import java.util.*; class SubCipher { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println(“Enter the plain text”); String pl=sc.nextLine(); pl=pl.toUpperCase(); System.out.println(pl); System.out.println(“Enter the shift factor”); int n=sc.nextInt(); System.out.println(“Cipher Text:”); char a[]= pl.toCharArray(); for(int i=0;i<a.length;i++) { a[i]=(char)(n+(int)a[i]); } for(int i=0;i<a.length;i++) { System.out.print(a[i]); } System.out.println(); } } Continue reading Substitution Cipher (Java)

Transposition Cipher (Java)

import java.util.*;
class TransCipher
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the plain text”);
String pl=sc.nextLine();
String demo=””;
String s=””;
int start=0;
for(int i=0;i<pl.length();i++)
{
if(pl.charAt(i)==’ ‘)
{
s=s+pl.substring(start,i);
start=i+1;
}
} Continue reading “Transposition Cipher (Java)”

Product Cipher (Java)

import java.util.*;

class ProductCipher {
public static void main(String args[]) {
System.out.println(“Enter the input to be encrypted:”);
String substitutionInput = new Scanner(System.in).nextLine();
System.out.println(“Enter a number:”);
int n = new Scanner(System.in).nextInt();

// Substitution encryption
StringBuffer substitutionOutput = new StringBuffer();
for(int i=0 ; i<substitutionInput.length() ; i++) {
char c = substitutionInput.charAt(i);
substitutionOutput.append((char) (c+5));
} Continue reading “Product Cipher (Java)”

Diffie Hellman Key Exchange Algorithm (Java)

import java.util.*; class DiffieHellman { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println(“Enter the value of Xa & Xb”); int Xa=sc.nextInt(); int Xb=sc.nextInt(); System.out.println(“Enter a Prime no. p”); int p=sc.nextInt(); System.out.println(“Enter Primitive Root a, such that a<p”); int a=sc.nextInt(); int Ya=(int)((Math.pow(a,Xa))%p); int Yb=(int)((Math.pow(a,Xb))%p); int Ka=(int)((Math.pow(Yb,Xa))%p); int Kb=(int)((Math.pow(Ya,Xb))%p); if(Ka==Kb) { System.out.println(“Transmission successful”); } else { System.out.println(“Transmission failed”); } } } Continue reading Diffie Hellman Key Exchange Algorithm (Java)