Beruflich Dokumente
Kultur Dokumente
Page No: 1
G NATA RAJU
Program No:
Page No: 2
Program No:
Page No: 3
Ceaser Cipher
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CeaserCipher {
static Scanner sc=new Scanner(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.print("Enter any String: ");
String str = br.readLine();
System.out.print("\nEnter the Key: ");
int key = sc.nextInt();
String encrypted = encrypt(str, key);
System.out.println("\nEncrypted String is: " +encrypted);
String decrypted = decrypt(encrypted, key);
System.out.println("\nDecrypted String is: " +decrypted);
System.out.println("\n");
}
public static String encrypt(String str, int key) {
String encrypted = "";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
G NATA RAJU
Program No:
Page No: 4
Program No:
Page No: 5
Substitution Cipher
PROGRAM:
import java.io.*;
import java.util.*;
public class SubstitutionCipher {
static Scanner sc = new Scanner(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
// TODO code application logic here
String a = "abcdefghijklmnopqrstuvwxyz";
String b = "zyxwvutsrqponmlkjihgfedcba";
System.out.print("Enter any string: ");
String str = br.readLine();
String decrypt = "";
char c;
for(int i=0;i<str.length();i++)
{
c = str.charAt(i);
int j = a.indexOf(c);
decrypt = decrypt+b.charAt(j);
}
System.out.println("The encrypted data is: " +decrypt);
}
}
Output:
Enter any string: aceho
The encrypted data is: zxvsl
G NATA RAJU
Program No:
Page No: 6
Hill Cipher
PROGRAM:
import java.io.*;
import java.util.*;
import java.io.*;
public class HillCipher {
static float[][] decrypt = new float[3][1];
static float[][] a = new float[3][3];
static float[][] b = new float[3][3];
static float[][] mes = new float[3][1];
static float[][] res = new float[3][1];
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws IOException {
// TODO code application logic here
getkeymes();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int k=0;k<3;k++) {
res[i][j]=res[i][j]+a[i][k]*mes[k][j]; }
System.out.print("\nEncrypted string is : ");
for(int i=0;i<3;i++) {
System.out.print((char)(res[i][0]%26+97));
res[i][0]=res[i][0];
}
inverse();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int k=0;k<3;k++) {
decrypt[i][j] = decrypt[i][j]+b[i][k]*res[k][j]; }
System.out.print("\nDecrypted string is : ");
for(int i=0;i<3;i++){
System.out.print((char)(decrypt[i][0]%26+97));
}
G NATA RAJU
Program No:
Page No: 7
Program No:
Page No: 8
G NATA RAJU
Program No:
Page No: 9
Program No:
Page No: 10
Program No:
Page No: 11
G NATA RAJU
Program No:
Page No: 12
}}
OUTPUT:
Initialization Vector of the Cipher: dI1MXzW97oQ=
Contents of inputFile.txt: Hello World
Contents of outputFile.txt: J NI
G NATA RAJU
Program No:
Page No: 13
Program No:
Page No: 14
9. RC4 logic
AIM: Write the RC4 logic in Java.
PROGRAM:
OUTPUT:
Program No:
Page No: 15
Program No:
Page No: 16
G NATA RAJU
Program No:
Page No: 17
AIM: Implement the Diffie-Hellman Key Exchange mechanism using HTML and
JavaScript. Consider the end user as one of the parties (Alice) and the JavaScript
application as other party (bob).
PROGRAM:
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.DHPublicKeySpec;
public class DiffeHellman {
public final static int pValue = 47;
public final static int gValue = 71;
public final static int XaValue = 9;
public final static int XbValue = 14;
public static void main(String[] args) throws Exception {
// TODO code application logic here
BigInteger p = new BigInteger(Integer.toString(pValue));
BigInteger g = new BigInteger(Integer.toString(gValue));
BigIntegerXa = new BigInteger(Integer.toString(XaValue));
BigIntegerXb = new BigInteger(Integer.toString(XbValue));
createKey();
intbitLength = 512; // 512 bits
SecureRandomrnd = new SecureRandom();
p = BigInteger.probablePrime(bitLength, rnd);
g = BigInteger.probablePrime(bitLength, rnd);
createSpecificKey(p, g);
}
public static void createKey() throws Exception {
KeyPairGeneratorkpg = KeyPairGenerator.getInstance("DiffieHellman");
G NATA RAJU
Program No:
Page No: 18
G NATA RAJU
Program No:
Page No: 19
Program No:
Page No: 20
15. MD5
AIM: Calculate the message digest of a text using the SHA-1 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();
G NATA RAJU
Program No:
Page No: 21
G NATA RAJU
Program No:
Page No: 22
Program
m No:
Pag
ge No: 23
APP
PLICATIO
ONS & NETWO
N
ORK SEC
CURITY
Y LAB
my_passwo
m
ord is the password
d, that prottects your key store file. You will
w
h
have
to entter it every
y time you want to sign a docu
ument.
validity is the numb
v
ber of days
s your certtificate willl stay valid
d. You can
n enter
m
more
than 365.
RSA is the algorithm
R
m used to generate
g
th
he cryptog
graphic key
ys, corresp
ponding
to
o your certificate.
2048 is the
2
e length off the crypttographic keys.
k
The more the length the
e
stronger th
he signaturre.
pkcs12 is the
p
t
formatt of the key
y store file
e. PKCS#12 (a.k.a PF
FX) key sttores
can be und
derstood by
y a lot of d
different programs a
and you ca
an also imp
port a
P
PKCS#12
file
fi in yourr Windows
s key store (just doub
ble click itt and follow
w the
in
nstruction
ns).
For additional inform
F
mation abo
out parame
eters by ce
ertificate generation
g
n please
see the official keytoo
ol docume
entation.
During the certificate
D
e generatio
on process
s you will be
b promptted to ente
er some
in
nformation
n about yo
ou. This in
nformation
n will be sa
aved in you
ur certifica
ate. At
th
he end you
u have to confirm
c
th
he entered information.
T
That's
how
w it looks liike on my computer:
At the
e end you will
w find th
he new key
y store file
e my_keystore.pfx in your currrent
directo
ory. You can registe
er this key store with
h DigiSigne
er and use
e your certtificate
to sign
n PDF documents.
OUTPUT:
Program No:
Page No: 24
Program No:
Page No: 25
x:
y:
G NATA RAJU
Program No:
Page No: 26
sig.initVerify(keyPair.getPublic());
sig.update(data);
System.out.println(sig.verify(signatureBytes));
}
}
OUTPUT:
Signature:
imwaKe99tkM6H6hiiP0rubmb/MrYJZLiwLdRSjslF2KlA5B23az5M2LKftQFCB+NH
Ce5F5/YfN8OsNSNLtucrrZTah0SrdWSzdGCOfYLdUZmPQ72j1SkLhYspsTsUb/U6
FPSYT4QebNSYobDtjKujkHdRimHI9TO4lLuqVQRdWU= true
G NATA RAJU