Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Khushali Panchal #9

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 19_KhushaliPanchal/Practicals/AES.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.Scanner;
class AES{
private byte[] key;
AES(){
key="kHFlksfddsaKHBDS".getBytes();
}
private byte[] encrypt(String plainText) throws Exception{
SecretKeySpec sKey = new SecretKeySpec(key,"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,sKey);
return cipher.doFinal(plainText.getBytes());
}
private byte[] decrypt(String cipherText) throws Exception{
SecretKeySpec sKey=new SecretKeySpec(key,"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE,sKey);
return cipher.doFinal(cipherText.getBytes());
}
public static void main(String args[]) throws Exception{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Plain Text : ");
String plainText=sc.nextLine();
AES aes=new AES();
String cipherText=new String(aes.encrypt(plainText));
System.out.println("Encrypted Text : " + cipherText);
System.out.println("Decrypted Text : " + new String(aes.decrypt(cipherText)));
}
}
84 changes: 84 additions & 0 deletions 19_KhushaliPanchal/Practicals/Ceaser_Cipher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import java.util.*;
public class Ceaser_Cipher
{
public static String encryption(String msg, int shift)
{
String encrypted="";
char ch;
for(int i=0;i<msg.length();i++)
{
ch = msg.charAt(i);
if(ch > 'a' && ch < 'z')
{
ch = (char)(ch + shift);
if(ch > 'z')
{
ch = (char)(ch - 'z' + 'a'-1);
}
encrypted= encrypted + ch;
}
else if(ch >= 'A' && ch <= 'Z')
{
ch = (char)(ch + shift);
if(ch > 'Z')
{
ch = (char)(ch - 'Z' + 'A' - 1);
}
encrypted = encrypted + ch;
}
else
{
encrypted = encrypted + ch;
}
}
return encrypted;
}

public static String decryption(String encrypted,int shift)
{
String decrypted="";
char ch;
for(int i=0;i<encrypted.length();i++)
{
ch = encrypted.charAt(i);
if(ch > 'a' && ch < 'z')
{
ch = (char)(ch - shift);
if(ch > 'z')
{
ch = (char)(ch + 'z' - 'a'+1);
}
decrypted= decrypted + ch;
}
else if(ch >= 'A' && ch <= 'Z')
{
ch = (char)(ch - shift);
if(ch > 'Z')
{
ch = (char)(ch + 'Z' - 'A' + 1);
}
decrypted = decrypted + ch;
}
else
{
decrypted = decrypted + ch;
}
}
return decrypted;
}
public static void main(String args[])
{
String msg,encrypted="",decrypted="";
int shift = 3;
Scanner sc = new Scanner(System.in);

System.out.println("Enter text in plain text.");
msg = sc.next();

encrypted = encryption(msg,shift);
System.out.println("Encrypted Message: " + encrypted);

decrypted=decryption(encrypted,shift);
System.out.println("Decrypted Message: " + decrypted);
}
}
71 changes: 71 additions & 0 deletions 19_KhushaliPanchal/Practicals/DES.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.util.*;
import java.util.Base64;
import java.util.Base64;;

public class DES {

private static Cipher ecipher;
private static Cipher dcipher;

private static SecretKey key;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

try {
key = KeyGenerator.getInstance("DES").generateKey();

ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");

ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
System.out.println("Enter Plain Text:");
String text = sc.nextLine();
String encrypted = encrypt(text);
String decrypted = decrypt(encrypted);
System.out.println("Decrypted: " + decrypted);

}
catch (Exception e) {
e.printStackTrace();
return;
}


}

public static String encrypt(String str)
{
try {
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
enc = Base64.getEncoder().encode(enc);
String str1 = Base64.getEncoder().encodeToString(enc);
System.out.println("Encrypted String.\n" + str1);
return new String(enc);
}catch (Exception e) {e.printStackTrace();}

return null;

}

public static String decrypt(String str)
{
try {
byte[] dec = Base64.getDecoder().decode(str.getBytes());
byte[] utf8 = dcipher.doFinal(dec);

return new String(utf8, "UTF8");
}catch (Exception e) {e.printStackTrace();}
return null;
}

}
19 changes: 19 additions & 0 deletions 19_KhushaliPanchal/Practicals/Firewall/ClientFirewall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Assign2.que3;
import java.util.*;
import java.io.*;
import java.net.*;
public class ClientFirewall
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
Socket socket = new Socket("127.0.0.1",5000);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
String number = "";
System.out.println("Enter String:");
number = sc.nextLine();
out.writeBytes(number + "\n");
System.out.println(number + " sent to forwerder:");
socket.close();
}
}
37 changes: 37 additions & 0 deletions 19_KhushaliPanchal/Practicals/Firewall/Forwarder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package Assign2.que3;
import java.io.*;
import java.net.*;
import java.util.*;

public class Forwarder
{
public static void main(String args[])throws Exception
{
int number = 0;
String msg = null;
ServerSocket server = new ServerSocket(5000);
Socket socket = server.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str = null;
str = input.readLine();
System.out.println("Recieved From Client:");
System.out.println(str);

server.close();
socket.close();
Socket socket1 = new Socket("127.0.0.1",4000);
DataOutputStream out = new DataOutputStream(socket1.getOutputStream());
if(!str.contains("Terrorist"))
{
System.out.println("This text is safe");
out.writeBytes(str);
}
else
{
System.out.println("This Text cannot forwarded");
}

socket1.close();
}

}
23 changes: 23 additions & 0 deletions 19_KhushaliPanchal/Practicals/Firewall/ServerFireWall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package Assign2.que3;
import java.io.*;
import java.util.*;
import java.net.*;

public class ServerFireWall
{
public static void main(String args[])throws Exception
{
int num = 0,temp;
ServerSocket server = new ServerSocket(4000);
Socket socket = server.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String received = null;
received = input.readLine();
num = Integer.parseInt(received);
System.out.println("Successfully Recieved");
System.out.println(num);
server.close();
socket.close();

}
}
21 changes: 21 additions & 0 deletions 19_KhushaliPanchal/Practicals/MD5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.Scanner;
import java.math.*;
import java.security.*;
class MD5{
private String doEncryption(String text) throws Exception{
MessageDigest md=MessageDigest.getInstance("MD5");
byte[] msg=md.digest(text.getBytes());
BigInteger bigInt=new BigInteger(1,msg);
String hashValue=bigInt.toString(16);
while(hashValue.length()<32)
hashValue+=0+hashValue;
return hashValue;
}
public static void main(String args[]) throws Exception{
MD5 MD5=new MD5();
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter Message: ");
String text=sc.nextLine();
System.out.print("\nHash Text: " + MD5.doEncryption(text));
}
}
53 changes: 53 additions & 0 deletions 19_KhushaliPanchal/Practicals/NAT/NAT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//package Assign2.que4;
import java.util.*;
import java.io.*;
import java.net.*;

public class NAT
{
public static void main(String args[]) throws IOException
{
ServerSocket ds=new ServerSocket(1233);
Socket sk=ds.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(sk.getInputStream()));
while(true)
{
String s;
s=br.readLine();
if(s.equalsIgnoreCase("exit"))
{
System.out.println("Exiting!");
Socket sk1=new Socket("127.0.0.1",1234);
Socket sk2=new Socket("127.0.0.1",1235);
DataOutputStream dos1=new DataOutputStream(sk1.getOutputStream());
DataOutputStream dos2=new DataOutputStream(sk2.getOutputStream());
dos1.writeBytes(s+"\n");
dos2.writeBytes(s+"\n");
break;
}
else
{
System.out.println("Number successfully received by Firewall: "+s);
int pn=checkString(s);
Socket sk1=new Socket("127.0.0.1",pn);
DataOutputStream dos=new DataOutputStream(sk1.getOutputStream());
dos.writeBytes(s+"\n");
}
}
}

public static int checkString(String s)
{
int n=Integer.parseInt(s);
if(n%2==0)
{
System.out.println(n+" is Even Number and forwarded to 1234");
return 1234;
}
else
{
System.out.println(n+" is ODD Number and forwarded to 1235");
return 1235;
}
}
}
28 changes: 28 additions & 0 deletions 19_KhushaliPanchal/Practicals/NAT/NATClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//package Assign2.que4;
import java.util.*;
import java.io.*;
import java.net.*;

public class NATClient
{
public static void main(String args[]) throws IOException
{
Scanner sc=new Scanner(System.in);
Socket ds=new Socket("127.0.0.1",1233);
DataOutputStream dos=new DataOutputStream(ds.getOutputStream());
String s;
while(true)
{
s="";
System.out.println("Enter Number: ");
s=sc.nextLine();
dos.writeBytes(s+"\n");
if(s.equalsIgnoreCase("exit"))
{
System.out.println("Exiting!");
break;
}
System.out.println("String: "+s+" sent Successfully!");
}
}
}
Loading