A) Authentication
A) Authentication
A) Authentication
a)authentication:
import java.io.*;
import java.util.*;
String user1="arun";
String pass1="1234";
System.out.println("username");
String username=sc.nextLine();
System.out.println("password");
String password=sc.nextLine();
System.out.println("validuser");
else{
b)autherization.
import java.io.*;
import java.util.*;
System.out.println("username");
System.out.println("password");
if(uname.equals(user1)&&pass.equals(pass1)){
System.out.println("Authentication Successfull, welcome user");
while(myReader.hasNextLine()){
System.out.println(data);
myReader.close();
else if(uname.equals(admin)&&pass.equals(apass)){
System.out.println("Welcome admin");
switch(choice){
case 1:
while(myReader.hasNextLine()){
nextLine = myReader.nextLine();
myReader.close();
break;
case 2:
mywriter.close();
break;
else {
}
Expno:2 active and passive attack
Program:
import java.io.*;
import java.util.*;
String content=input.nextLine();
int x = input.nextInt();
switch(x){
case 1 -> {
String data;
mywriter.write(content);
mywriter.close();
//Reading file2
File obj=new File("file.txt");
while(myReader.hasNextLine()){
data=myReader.nextLine();
System.out.println(data);
if(content.equals(data)){
System.out.println("Passive Attack");
else{
System.out.println("Active Attack");
}}
myReader.close();
case 2 -> {
while(myReader1.hasNextLine()){
String data=myReader1.nextLine();
System.out.println(data);
if(content.equals(data)){
System.out.println("Passive Attack");
else{
System.out.println("Active Attack");
myReader1.close();
1)modular arithmetic:
import java.io.*;
import java.util.*;
{
while(1>0){
int s=sc.nextInt();
int n=sc.nextInt();
switch(n)
case 1:
for(int i=0;i<s;i++){
for(int j=0;j<s;j++){
int c=i+j;
if(c>(s-1))
c=c%s;
System.out.print("\t"+c);
else
{
System.out.print("\t"+c);
System.out.print("\n");
break;
case 2:
for(int i=0;i<s;i++){
for(int j=0;j<s;j++){
int c=i-j;
if(c>=0){
if(c>(s-1))
c=c%s;
System.out.print("\t"+c);
else
{
System.out.print("\t"+c);
else{
c=c+s;
if(c>(s-1))
c=c%s;
System.out.print("\t"+c);
else
System.out.print("\t"+c);
System.out.print("\n");
}
break;
case 3:
for(int i=1;i<s;i++){
for(int j=1;j<s;j++){
int c=i*j;
if(c>(s-1))
c=c%s;
if(c==0){
c=1;
System.out.print("\t"+c);
else{
System.out.print("\t"+c);
else
{
System.out.print("\t"+c);
System.out.print("\n");
break;
default :
b)Euclidian algorithm:
import java.io.*;
import java.util.*;
while(true){
System.out.println("\n");
System.out.println("Enter the value of a");
int a=sc.nextInt();
int b=sc.nextInt();
for(int i=1;i<1000;i++){
if(a==0&&b==0){
break;
if(a>b){
int r=a%b;
if(r>0){
a=b;
b=r;
if(r==0){
System.out.print(",");
System.out.print(+b);
System.out.print(")");
System.out.print("="+b);
break;
else{
System.out.println("step"+i);
System.out.println("swap is processing");
int c=a;
a=b;
b=c;
}
Expno:4 encryption and decryption using ceaser cipher(substitution technique)
Program:
import java.io.*;
import java.util.*;
import java.awt.Point;
cc1(){
char
a[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','
X','Y','Z'};
System.out.println("CEASER CIPHER");
String pt=sc.nextLine();
int key=sc.nextInt();
for(int i=0;i<pt.length();i++){
cs[i]=pt.charAt(i);
for(char c:cs){
System.out.print("\t"+c);
System.out.print("\n");
for(int i=0;i<cs.length;i++){
for(int j=0;j<a.length;j++){
if(cs[i]==a[j]){
int s=j+key;
if(s<a.length){
System.out.println(cs[i]+"+"+key+"="+a[s]);
else{
int k=a.length;
s=s-k;
System.out.println(cs[i]+"+"+key+"="+a[s]);
}
}
System.out.println("-----------------------------------------------");
System.out.println("Decrypt");
System.out.println("-----------------------------------------------");
for(int i=0;i<cs.length;i++){
for(int j=0;j<a.length;j++){
if(cs[i]==a[j]){
int s=j+key;
int o=s-key;
if(s<a.length){
System.out.println(a[s]+"-"+key+"="+a[o]);
else{
int k=a.length;
s=s-k;
System.out.println(a[s]+"+"+key+"="+a[o]);
}
}
s = s.toUpperCase().replaceAll("[^A-Z]", "");
String s1="ABCDEFGHIKLMNOPQRSTUVWXYZ";
String s2=key.concat(s1);
for(int i=0;i<sb1.length();i++){
ch[i]=sb1.charAt(i);
}
int n=0;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(n<26)
ci[i][j]=ch[n];
System.out.print("\t"+ci[i][j]);
n++;
System.out.print("\n");
char c = s.charAt(i);
charTable[k / 5][k % 5] = c;
char a = txt.charAt(i);
if (row1 == row2) {
} else {
int tmp = col1;
col1 = col2;
col2 = tmp;
txt.setCharAt(i, charTable[row1][col1]);
txt.setCharAt(i + 1, charTable[row2][col2]);
return txt.toString();
if (i == sb.length() - 1) {
sb.insert(i + 1, 'X');
}
private static String decode(String s) {
{ 2, 2, 1 } };
int x, y, z;
a = key.charAt(x % 26);
b = key.charAt(y % 26);
c = key.charAt(z % 26);
ret = "" + a + b + c;
return ret;
int x, y, z;
ret = "" + a + b + c;
return ret;
text = text.toUpperCase();
char c = text.charAt(i);
continue;
j = ++j % key.length();
return res;
text = text.toUpperCase();
char c = text.charAt(i);
continue;
}
res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
j = ++j % key.length();
return res;
while (1>0){
int g=sc.nextInt();
switch(g){
case 1:
break;
case 2:
System.out.println("PLAYFAIR CIPHER");
createTbl(key, chgJtoI);
break;
case 3:
String msg;
int n;
System.out.println("HILL CIPHER");
msg =sT.nextLine();
n = msg.length() % 3;
if (n != 0) {
msg += 'X';
break;
case 4:
System.out.println("VIGNERECIPHER");
break;
Program:
import java.io.*;
import java.util.*;
int m=3;
s1=s1.replaceAll("\\s","");
int k=0;
for(int i=0;i<s1.length();i++){
ch[i]=s1.charAt(i);
for(int i=0;i<=10;i++){
for(int j=1;j<=m;j++){
if(k< ch.length){
ci[i][j]=ch[k];
System.out.print(ch[k]);
k=k+1;
else{
break;
}
System.out.print("\t");
System.out.print("\n");
System.out.print("Encrypt:");
for(int j=1;j<=m;j++){
for(int i=0;i<ci.length;i++){
System.out.print(ci[i][j]);
System.out.print("\n");
System.out.print("Decrypt:");
for(int i=0;i<ci.length;i++){
for(int j=1;j<=m;j++){
System.out.print(ci[i][j]);
}
}
while(true){
int g=sc.nextInt();
if(g==1){
else if(g==2){
else{
class second{
second(){
String pl = "i am ramchandru";
int q=5;
pl=pl.toUpperCase();
pl=pl.replaceAll("\\s","");
//matrix
int h=1;
String s1="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String s2=pl.concat(s1);
for(int i=0;i<s2.length();i++){
cha[i]=s2.charAt(i);
String key="6521374";
for(int i=0;i<key.length();i++){
kay[i]=key.charAt(i);
for(char l:kay)
System.out.print("\t"+l);
}
System.out.print("\n");
int n=0;
for(int i=0;i<q;i++){
for(int j=0;j<kay.length;j++){
if(n<s2.length())
ci[i][j]=cha[n];
System.out.print("\t"+ci[i][j]);
n++;
System.out.print("\n");
System.out.print("\n");
System.out.print("\n");
for(int i=0;i<q;i++){
for(int j=0;j<kay.length;j++){
int a=Character.getNumericValue(kay[j]);
if(a==h){
for(int k=0;k<kay.length;k++){
so[i][k]=ci[k][j];
System.out.print("\t"+so[i][k]);
System.out.print("\n");
h=h+1;
int o=1;
for(int i=0;i<q;i++){
for(int j=0;j<kay.length;j++){
int a=Character.getNumericValue(kay[j]);
if(a==o){
for(int k=0;k<kay.length;k++){
so[i][k]=ci[k][j];
System.out.print(so[i][k]);
o=o+1;
Expno:6 steganography
Program:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
File f = null;
// Read image
try {
f = new File(
"C:\\Users\\BRAJASANKAR
R\\Pictures\\DSC_0445.JPG");
img = ImageIO.read(f);
catch (IOException e) {
System.out.println(e);
}
// create BufferedImage object of same width and
img.getWidth(), img.getHeight(),
BufferedImage.TYPE_INT_RGB);
// image to it
graphics.drawImage(img, 0, 0, null);
graphics.drawString(watermark, img.getWidth() / 5,
img.getHeight() / 3);
graphics.dispose();
try {
catch (IOException e) {
System.out.println(e);
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.util.*;
try{
Cipher desCipher;
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
//byte[] text = "Secret Information ".getBytes();
String b=v.nextLine();
byte[] text=b.getBytes();
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
catch(NoSuchAlgorithmException e){
e.printStackTrace();
catch(NoSuchPaddingException e){
e.printStackTrace();
catch(InvalidKeyException e){
e.printStackTrace();
}
catch(IllegalBlockSizeException e){
e.printStackTrace();
catch(BadPaddingException e){
e.printStackTrace();
Program:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;
public class AES {
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
public static String encrypt(String strToEncrypt, String secret) {
try {
setKey(secret);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return
Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("U
TF8")));
catch (Exception e) {
return null;
try {
setKey(secret);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new
String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
catch (Exception e) {
return null;
String originalString=s.nextLine();
Program:
import java.util.*;
class ECB{
int h=sc.nextInt();
int i;
int q=1;
int e=0;
for(i=0;i<h;i++){
System.out.println("Enter the string"+(i+1)+"=:");
p[q][i]=sc.next();
q++;
String key=so.nextLine();
System.out.println(key);
System.out.println();
char[]
alpha1={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','
W','X','Y','Z'};
for(i=0;i<26;i++){
alpha[i+1]=alpha1[i];
/*encryption */
q=1;
for(int j=0;j<h;j++)
{
p[q][j]=p[q][j].toUpperCase();
for(i=0;i<p[q][j].length();i++)
check[i]=p[q][j].charAt(i);
for(int k=1;k<=26;k++)
if(alpha[k]==check[i])
key=key.toUpperCase();
for(int l=0;l<key.length();l++)
ks[l]=key.charAt(l);
for(int w=1;w<=26;w++)
if(alpha[w]==ks[l])
int o=k+w;
if(o<=26){
c[q][i]=alpha[o];
else{
o=o%26;
c[q][i]=alpha[o];
break;
break;
break;}
q++;
System.out.println("encrypted values:");
for(int j=1;j<=h;j++){
for(i=0;i<p.length;i++){
System.out.print(c[j][i]);
System.out.println("");
} /*encryption done */
/*decryption start */
System.out.print("\n");
for(q=1;q<=h;q++){
for(i=0;i<c.length;i++)
for(int k=1;k<=26;k++)
if(alpha[k]==c[q][i])
key=key.toUpperCase();
for(int l=0;l<key.length();l++)
{
ks[l]=key.charAt(l);
for(int w=1;w<=26;w++)
if(alpha[w]==ks[l])
k=k+26;
if(k<=26){
c[q][i]=alpha[k];
else{
int o=k-w;
if(o<=26){
c[q][i]=alpha[o];
else{
o=o%26;
c[q][i]=alpha[o];
}
}
break;
break;
System.out.println("Decrypted:");
for(int j=1;j<=h;j++){
for(i=0;i<p.length;i++){
System.out.print(c[j][i]);
System.out.println("");
}
Expno:10 Pseudo randam number(PSR)
Program:
import java.util.Random;
import java.util.Scanner;
int i;
int count=0;
int b=sc.nextInt();
int v=sc.nextInt();
for(i=1;i<=b;i++)
if(b%i==0)
count++;
}
if(count==2){
System.out.println("");
for(i=0;i<b;i++){
System.out.println(rand.nextInt(v)+"\t");
else{
Program:
import java.util.Random;
import java.util.Scanner;
public class psr {
int i;
int count=0;
int b=sc.nextInt();
int v=sc.nextInt();
for(i=1;i<=b;i++)
if(b%i==0)
count++;
if(count==2){
System.out.println("");
for(i=0;i<b;i++){
System.out.println(rand.nextInt(v)+"\t");
else{
Program:
import java.util.*;
import java.math.*;
class RSA
int p,q,n,z,d=0,e,i;
System.out.println("Enter the number to be encrypted and
decrypted");
int msg=sc.nextInt();
double c;
BigInteger msgback;
p=sc.nextInt();
q=sc.nextInt();
n=p*q;
z=(p-1)*(q-1);
for(e=2;e<z;e++)
break;
}
System.out.println("the value of e = "+e);
for(i=0;i<=9;i++)
int x=1+(i*z);
d=x/e;
break;
c=(Math.pow(msg,e))%n;
System.out.println(c);
BigInteger N = BigInteger.valueOf(n);
BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
if(e==0)
return z;
else
return gcd(z%e,e);
Program:
import java.util.*;
int q=sc.nextInt();
int halfa=sc.nextInt();
int xa=sc.nextInt();
int xb=sc.nextInt();
long ya,yb;
long s=1L;
long s1=1L,s2=1L,s3=1L;
System.out.println("halfa:"+halfa);
System.out.println("xa"+xa);
System.out.println("xb"+xb);
for(int i=1;i<=xa;i++){
s=halfa*s;
ya=s%q;
System.out.println(ya+"="+halfa+"^"+xa+"mod"+q);
for(int i=1;i<=xb;i++){
s1=halfa*s1;
yb=s1%q;
System.out.println(yb+"="+halfa+"^"+xb+"mod"+q);
for(int i=1;i<=xa;i++){
s2=s2*yb;
long k=s2%q;
System.out.println(k+"="+yb+"^"+xa+"mod"+q);
long de;
for(int i=1;i<=xb;i++){
s3=s3*ya;
de=s3%q;
System.out.println(de+"="+ya+"^"+xb+"mod"+q);
}
Expno:14 Secure Hash Algorithm (SHA)
Program:
import java.security.*;
class sha1
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
System.out.println("Algorithm=" + md.getAlgorithm());
System.out.println("Provider=" + md.getProvider());
System.out.println("ToString=" + md.toString());
md.update(input.getBytes());
byte[]output = md.digest();
System.out.println();
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\")="+bytesToHex(output));
input="abcdefghijklmnopqrstuvwxyz";
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);
char hexDigit[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
for(byte aB:b){
buf.append(hexDigit[(aB>>4)&0x0f]);
buf.append(hexDigit[aB&0x0f]);
}
return buf.toString();
Program:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.Scanner;
keyPairGen.initialize(2048);
sign.initSign(privKey);
sign.update(bytes);
"UTF8"));
}}