diff --git a/19_KhushaliPanchal/Practicals/AES.java b/19_KhushaliPanchal/Practicals/AES.java new file mode 100644 index 0000000..444babd --- /dev/null +++ b/19_KhushaliPanchal/Practicals/AES.java @@ -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))); + } +} diff --git a/19_KhushaliPanchal/Practicals/Ceaser_Cipher.java b/19_KhushaliPanchal/Practicals/Ceaser_Cipher.java new file mode 100644 index 0000000..6ecf0c7 --- /dev/null +++ b/19_KhushaliPanchal/Practicals/Ceaser_Cipher.java @@ -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 '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 '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); + } +} \ No newline at end of file diff --git a/19_KhushaliPanchal/Practicals/DES.java b/19_KhushaliPanchal/Practicals/DES.java new file mode 100644 index 0000000..d9159c9 --- /dev/null +++ b/19_KhushaliPanchal/Practicals/DES.java @@ -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; + } + +} \ No newline at end of file diff --git a/19_KhushaliPanchal/Practicals/Firewall/ClientFirewall.java b/19_KhushaliPanchal/Practicals/Firewall/ClientFirewall.java new file mode 100644 index 0000000..7299c73 --- /dev/null +++ b/19_KhushaliPanchal/Practicals/Firewall/ClientFirewall.java @@ -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(); + } +} \ No newline at end of file diff --git a/19_KhushaliPanchal/Practicals/Firewall/Forwarder.java b/19_KhushaliPanchal/Practicals/Firewall/Forwarder.java new file mode 100644 index 0000000..5243f4b --- /dev/null +++ b/19_KhushaliPanchal/Practicals/Firewall/Forwarder.java @@ -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(); + } + +} \ No newline at end of file diff --git a/19_KhushaliPanchal/Practicals/Firewall/ServerFireWall.java b/19_KhushaliPanchal/Practicals/Firewall/ServerFireWall.java new file mode 100644 index 0000000..9022cc4 --- /dev/null +++ b/19_KhushaliPanchal/Practicals/Firewall/ServerFireWall.java @@ -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(); + + } +} \ No newline at end of file diff --git a/19_KhushaliPanchal/Practicals/MD5.java b/19_KhushaliPanchal/Practicals/MD5.java new file mode 100644 index 0000000..1b3541d --- /dev/null +++ b/19_KhushaliPanchal/Practicals/MD5.java @@ -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)); + } +} diff --git a/19_KhushaliPanchal/Practicals/NAT/NAT.java b/19_KhushaliPanchal/Practicals/NAT/NAT.java new file mode 100644 index 0000000..67f35e1 --- /dev/null +++ b/19_KhushaliPanchal/Practicals/NAT/NAT.java @@ -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; + } + } +} diff --git a/19_KhushaliPanchal/Practicals/NAT/NATClient.java b/19_KhushaliPanchal/Practicals/NAT/NATClient.java new file mode 100644 index 0000000..bc5a718 --- /dev/null +++ b/19_KhushaliPanchal/Practicals/NAT/NATClient.java @@ -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!"); + } + } +} diff --git a/19_KhushaliPanchal/Practicals/NAT/NATEven.java b/19_KhushaliPanchal/Practicals/NAT/NATEven.java new file mode 100644 index 0000000..80e2e3e --- /dev/null +++ b/19_KhushaliPanchal/Practicals/NAT/NATEven.java @@ -0,0 +1,25 @@ +import java.util.*; +//package Assign2.que4; +import java.io.*; +import java.net.*; + +public class NATEven +{ + public static void main(String args[]) throws IOException + { + ServerSocket ds=new ServerSocket(1234); + while(true) + { + String es=""; + Socket sk=ds.accept(); + BufferedReader br=new BufferedReader(new InputStreamReader(sk.getInputStream())); + es=br.readLine(); + if(es.equalsIgnoreCase("exit")) + { + System.out.println("Exiting!"); + break; + } + System.out.println("Even number: "+es+" successfully received!"); + } + } +} diff --git a/19_KhushaliPanchal/Practicals/NAT/NATOdd.java b/19_KhushaliPanchal/Practicals/NAT/NATOdd.java new file mode 100644 index 0000000..890d91b --- /dev/null +++ b/19_KhushaliPanchal/Practicals/NAT/NATOdd.java @@ -0,0 +1,26 @@ +import java.util.*; +//package Assign2.que4; + +import java.io.*; +import java.net.*; + +public class NATOdd +{ + public static void main(String args[]) throws IOException + { + ServerSocket ds=new ServerSocket(1235); + while(true) + { + String es=""; + Socket sk=ds.accept(); + BufferedReader br=new BufferedReader(new InputStreamReader(sk.getInputStream())); + es=br.readLine(); + if(es.equalsIgnoreCase("exit")) + { + System.out.println("Exiting!"); + break; + } + System.out.println("Odd number: "+es+" successfully received!"); + } + } +} diff --git a/19_KhushaliPanchal/Practicals/P_Box.java b/19_KhushaliPanchal/Practicals/P_Box.java new file mode 100644 index 0000000..f7580e2 --- /dev/null +++ b/19_KhushaliPanchal/Practicals/P_Box.java @@ -0,0 +1,39 @@ +import java.util.Scanner; +class P_Box{ + public String doEncryption(String s){ + byte p[]=new byte[8]; + byte pTemp[]=new byte[8]; + pTemp=s.getBytes(); + p[0]=pTemp[4]; + p[1]=pTemp[0]; + p[2]=pTemp[5]; + p[3]=pTemp[7]; + p[4]=pTemp[1]; + p[5]=pTemp[3]; + p[6]=pTemp[2]; + p[7]=pTemp[6]; + return(new String(p)); + } + public String doDecryption(String s){ + byte p[]=new byte[8]; + byte pTemp[]=new byte[8]; + pTemp=s.getBytes(); + p[0]=pTemp[1]; + p[1]=pTemp[4]; + p[2]=pTemp[6]; + p[3]=pTemp[5]; + p[4]=pTemp[0]; + p[5]=pTemp[2]; + p[6]=pTemp[7]; + p[7]=pTemp[3]; + return(new String(p)); + } + public static void main(String args[]){ + Scanner sc=new Scanner(System.in); + System.out.print("\nEnterString(Only 8 Characters) : "); + String plaintext=sc.nextLine(); + P_Box p_box=new P_Box(); + System.out.println("Encrypted Text : " + p_box.doEncryption(plaintext)); + System.out.println("Decrypted Text : " + p_box.doDecryption(p_box.doEncryption(plaintext))); + } +} diff --git a/19_KhushaliPanchal/Practicals/PasswordEncryption/Client.java b/19_KhushaliPanchal/Practicals/PasswordEncryption/Client.java new file mode 100644 index 0000000..658a17b --- /dev/null +++ b/19_KhushaliPanchal/Practicals/PasswordEncryption/Client.java @@ -0,0 +1,51 @@ +package Assign2.que1; +import java.net.*; +import java.io.*; +import java.util.Scanner; +class Client{ + public static void main(String args[]) throws Exception{ + Socket s=new Socket("127.0.0.1",1234); + + Scanner sc=new Scanner(System.in); + System.out.println("Enter Password : "); + String msg=sc.nextLine(); + String encrypted = encryption(msg); + + DataOutputStream out=new DataOutputStream(s.getOutputStream()); + out.writeUTF(encrypted); + s.close(); + } + public static String encryption(String msg) + { + String encrypted=""; + char ch; + for(int i=0;i 'a' && ch < 'z') + { + ch = (char)(ch + 3); + if(ch > 'z') + { + ch = (char)(ch - 'z' + 'a'-1); + } + encrypted= encrypted + ch; + } + else if(ch >= 'A' && ch <= 'Z') + { + ch = (char)(ch + 3); + if(ch > 'Z') + { + ch = (char)(ch - 'Z' + 'A' - 1); + } + encrypted = encrypted + ch; + } + else + { + encrypted = encrypted + ch; + } + } + return encrypted; + } + +} \ No newline at end of file diff --git a/19_KhushaliPanchal/Practicals/PasswordEncryption/Server.java b/19_KhushaliPanchal/Practicals/PasswordEncryption/Server.java new file mode 100644 index 0000000..7e74016 --- /dev/null +++ b/19_KhushaliPanchal/Practicals/PasswordEncryption/Server.java @@ -0,0 +1,70 @@ +package Assign2.que1; +import java.io.*; +import java.net.*; +import java.util.Scanner; +import java.sql.*; +class Server{ + public static void main(String args[]) throws Exception{ + ServerSocket ss=new ServerSocket(1234); + Socket s=ss.accept(); + + System.out.print("From Client : "); + DataInputStream in=new DataInputStream(s.getInputStream()); + String encrypted = in.readUTF(); + String decrypted = decryption(encrypted); + String finalResult = checkPassword(decrypted); + if(finalResult.equals(decrypted)) + System.out.println("Password Matched"); + else + System.out.println("Password doesn't Matched"); + + s.close(); + ss.close(); + } + public static String decryption(String encrypted) + { + String decrypted=""; + char ch; + for(int i=0;i 'a' && ch < 'z') + { + ch = (char)(ch - 3); + if(ch > 'z') + { + ch = (char)(ch + 'z' - 'a'+1); + } + decrypted= decrypted + ch; + } + else if(ch >= 'A' && ch <= 'Z') + { + ch = (char)(ch - 3); + if(ch > 'Z') + { + ch = (char)(ch + 'Z' - 'A' + 1); + } + decrypted = decrypted + ch; + } + else + { + decrypted = decrypted + ch; + } + } + return decrypted; + } + + public static String checkPassword(String decrypted)throws Exception + { + String pwd = null; + Class.forName("com.mysql.cj.jdbc.Driver"); + Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ns","root",""); + Statement st = con.createStatement(); + ResultSet rs = st.executeQuery("select * from password where pws ='" + decrypted +"';"); + while(rs.next()){ + pwd = rs.getString("pws"); + } + return pwd; + } + +} \ No newline at end of file diff --git a/19_KhushaliPanchal/Practicals/Que2/Client2.java b/19_KhushaliPanchal/Practicals/Que2/Client2.java new file mode 100644 index 0000000..c207b8b --- /dev/null +++ b/19_KhushaliPanchal/Practicals/Que2/Client2.java @@ -0,0 +1,51 @@ +package Assign2.que1; +import java.net.*; +import java.io.*; +import java.util.Scanner; +class Client2{ + public static void main(String args[]) throws Exception{ + Socket s=new Socket("127.0.0.1",1234); + + Scanner sc=new Scanner(System.in); + System.out.println("Enter Password : "); + String msg=sc.nextLine(); + String encrypted = encryption(msg); + + DataOutputStream out=new DataOutputStream(s.getOutputStream()); + out.writeUTF(encrypted); + s.close(); + } + public static String encryption(String msg) + { + String encrypted=""; + char ch; + for(int i=0;i 'a' && ch < 'z') + { + ch = (char)(ch + 3); + if(ch > 'z') + { + ch = (char)(ch - 'z' + 'a'-1); + } + encrypted= encrypted + ch; + } + else if(ch >= 'A' && ch <= 'Z') + { + ch = (char)(ch + 3); + if(ch > 'Z') + { + ch = (char)(ch - 'Z' + 'A' - 1); + } + encrypted = encrypted + ch; + } + else + { + encrypted = encrypted + ch; + } + } + return encrypted; + } + +} \ No newline at end of file diff --git a/19_KhushaliPanchal/Practicals/Que2/Server2.java b/19_KhushaliPanchal/Practicals/Que2/Server2.java new file mode 100644 index 0000000..10a733a --- /dev/null +++ b/19_KhushaliPanchal/Practicals/Que2/Server2.java @@ -0,0 +1,81 @@ +package Assign2.que1; +import java.io.*; +import java.math.*; +import java.net.*; +import java.util.Scanner; +import java.sql.*; +import java.security.*; +class Server2{ + public static void main(String args[]) throws Exception{ + ServerSocket ss=new ServerSocket(1234); + Socket s=ss.accept(); + + System.out.print("From Client : "); + DataInputStream in=new DataInputStream(s.getInputStream()); + String encrypted = in.readUTF(); + String decrypted = decryption(encrypted); + String hashed = doHashing(decrypted); + String finalResult = checkPassword(decrypted); + if(finalResult.equals(decrypted)) + System.out.println("Password Matched"); + else + System.out.println("Password doesn't Matched"); + + s.close(); + ss.close(); + } + public static String decryption(String encrypted) + { + String decrypted=""; + char ch; + for(int i=0;i 'a' && ch < 'z') + { + ch = (char)(ch - 3); + if(ch > 'z') + { + ch = (char)(ch + 'z' - 'a'+1); + } + decrypted= decrypted + ch; + } + else if(ch >= 'A' && ch <= 'Z') + { + ch = (char)(ch - 3); + if(ch > 'Z') + { + ch = (char)(ch + 'Z' - 'A' + 1); + } + decrypted = decrypted + ch; + } + else + { + decrypted = decrypted + ch; + } + } + return decrypted; + } + private static String doHashing(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 String checkPassword(String decrypted)throws Exception + { + String pwd = null; + Class.forName("com.mysql.cj.jdbc.Driver"); + Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ns","root",""); + Statement st = con.createStatement(); + ResultSet rs = st.executeQuery("select * from password where pws ='" + decrypted +"';"); + while(rs.next()){ + pwd = rs.getString("pws"); + } + return pwd; + } + +} \ No newline at end of file diff --git a/19_KhushaliPanchal/Practicals/RSA.java b/19_KhushaliPanchal/Practicals/RSA.java new file mode 100644 index 0000000..4ffe851 --- /dev/null +++ b/19_KhushaliPanchal/Practicals/RSA.java @@ -0,0 +1,38 @@ +import java.security.*; +import javax.crypto.*; +import javax.crypto.spec.*; +import java.util.*; +class RSA{ + public KeyPairGenerator keygenerator; + public KeyPair myKey; + Cipher c; + public RSA() throws Exception{ + keygenerator = KeyPairGenerator.getInstance("RSA"); + keygenerator.initialize(1024) ; + myKey = keygenerator.generateKeyPair(); + c = Cipher.getInstance("RSA"); + + } + public byte[] doEncryption(String s) throws Exception { + c.init(Cipher.ENCRYPT_MODE,myKey.getPublic()); + byte[] text = s.getBytes(); + byte[] textEncrypted = c.doFinal(text); + return(textEncrypted); + } + public String doDecryption(byte[] s)throws Exception{ + c.init(Cipher.DECRYPT_MODE,myKey.getPrivate()); + byte[] textDecrypted = c.doFinal(s); + return(new String(textDecrypted)); + } + + public static void main(String[] argv) throws Exception{ + Scanner sc = new Scanner(System.in); + System.out.println("Enter Plain Text:"); + String text = sc.nextLine(); + RSA d=new RSA(); + byte[] str=d.doEncryption(text); + System.out.println("Encrypted String : "+str); + System.out.println("Decrypted String : "+d.doDecryption(str)); + + } +} diff --git a/19_KhushaliPanchal/Practicals/SHA.java b/19_KhushaliPanchal/Practicals/SHA.java new file mode 100644 index 0000000..95e5f9a --- /dev/null +++ b/19_KhushaliPanchal/Practicals/SHA.java @@ -0,0 +1,21 @@ +import java.util.Scanner; +import java.math.*; +import java.security.*; +class SHA{ + private String doEncryption(String text) throws Exception{ + MessageDigest md=MessageDigest.getInstance("SHA-1"); + 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{ + SHA sha=new SHA(); + Scanner sc=new Scanner(System.in); + System.out.print("\nEnter Message: "); + String text=sc.nextLine(); + System.out.print("\nHash Text: " + sha.doEncryption(text)); + } +} diff --git a/19_KhushaliPanchal/Practicals/SHA256.java b/19_KhushaliPanchal/Practicals/SHA256.java new file mode 100644 index 0000000..730b7ca --- /dev/null +++ b/19_KhushaliPanchal/Practicals/SHA256.java @@ -0,0 +1,37 @@ +import java.util.*; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +class SHA256{ + public static byte[] getSHA(String input) throws NoSuchAlgorithmException + { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + return md.digest(input.getBytes(StandardCharsets.UTF_8)); + } + + public static String toHexString(byte[] hash) + { + BigInteger number = new BigInteger(1, hash); + StringBuilder hexString = new StringBuilder(number.toString(16)); + while (hexString.length() < 32) + { + hexString.insert(0, '0'); + } + + return hexString.toString(); + } + + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + System.out.println("Enter Text"); + try + { + String s1 = sc.nextLine(); + System.out.println("HashCode Generated by SHA-256 for:"); + System.out.println("\n" + s1 + " : " + toHexString(getSHA(s1))); + + }catch (Exception e) { e.printStackTrace();} + } +} diff --git a/19_KhushaliPanchal/Practicals/Substitution_Cipher.java b/19_KhushaliPanchal/Practicals/Substitution_Cipher.java new file mode 100644 index 0000000..f11bb0c --- /dev/null +++ b/19_KhushaliPanchal/Practicals/Substitution_Cipher.java @@ -0,0 +1,67 @@ +import java.util.Scanner; +class Substitution_Cipher{ + Scanner sc; + String plain_text,cipher_text,decrypted_text; + int shift; + Substitution_Cipher(){ + sc=new Scanner(System.in); + cipher_text=""; + decrypted_text=""; + shift=0; + } + void getData(){ + System.out.print("Enter Plain Text : "); + plain_text=sc.nextLine(); + plain_text=plain_text.trim(); + System.out.print("\nEnterShift : "); + shift=sc.nextInt(); + } + void encryptData(){ + char ch; + for(int i=0;i='A' &&ch<='Z'){ + ch=(char)(ch+shift); + if(ch>'Z'){ + ch=(char)(ch-'Z'+'A'-1); + } + cipher_text=cipher_text+ch; + } + else if(ch>='a' &&ch<='z'){ + ch=(char)(ch+shift); + if(ch>'z'){ + ch=(char)(ch-'z'+'a'-1); + } + cipher_text=cipher_text+ch; + } + } + System.out.println("Cipher Text : "+cipher_text); + } + void decryptData(){ + char ch; + for(int i=0;i='A' &&ch<='Z'){ + ch=(char)(ch-shift); + if(ch<'A'){ + ch=(char)(ch-'A'+'Z'+1); + } + decrypted_text=decrypted_text+ch; + } + else if(ch>='a' &&ch<='z'){ + ch=(char)(ch+shift); + if(ch<'a'){ + ch=(char)(ch-'a'+'z'+1); + } + decrypted_text=decrypted_text+ch; + } + } + System.out.println("Decrypted Text : "+decrypted_text); + } + public static void main(String args[]){ + Substitution_Cipher cc=new Substitution_Cipher(); + cc.getData(); + cc.encryptData(); + cc.decryptData(); + } +} diff --git a/19_KhushaliPanchal/Practicals/Transposition.java b/19_KhushaliPanchal/Practicals/Transposition.java new file mode 100644 index 0000000..1cf4b82 --- /dev/null +++ b/19_KhushaliPanchal/Practicals/Transposition.java @@ -0,0 +1,83 @@ +import java.util.*; +class Transposition{ + public static void main(String args[])throws Exception{ + Scanner sc=new Scanner(System.in); + System.out.print("\nEnterkey(of unique alphabets): "); + String k=sc.nextLine(); + char[] key=k.toCharArray(); + char[] temp_key=new char[key.length]; + System.arraycopy(key,0,temp_key,0,key.length); + Arrays.sort(temp_key); + System.out.print("\nEnter string: "); + String t=sc.nextLine(); + char[] str=t.toCharArray(); + for(int i=0;i