SlideShare a Scribd company logo
1 of 5
[Type here] [Type here] Khushal Choudhary 8606
PRACTICAL NO. 1
Aim: Implementing Substitution Ciphers
a) Caesar Cipher
Code:
// Practical 1 A
import java.util.Scanner;
public class practical1a
{
int total_alphabets=26,key=3;
String encrypted=" ",decrypted=" ";
char ch; public String encrypt
(String plain)
{
for(int i=0;i<plain.length();i++)
{
ch=plain.charAt(i);
if(ch==' ') encrypted+="-";
else if(ch>='A' && ch<='Z')
{
if(ch<='Z'-key)
{
encrypted+=String.valueOf((char)(ch+key));
}
else {
encrypted+=String.valueOf((char)(ch-
(total_alphabetskey)));
}
} else
{
encrypted+=String.valueOf(ch);
}
}
return encrypted;
}
public String decrypt (String cipher)
{
for(int i=0;i<cipher.length();i++)
{
ch=cipher.charAt(i);
if(ch==' ') decrypted+="-";
else if(ch>='A' && ch<='Z')
{
if(ch>='A'+key)
{
[Type here] [Type here] Khushal Choudhary 8606
decrypted+=String.valueOf((char)(ch-key));
}
else {
decrypted+=String.valueOf((char)(ch+(total_alphabetskey)));
}
} else
{
decrypted+=String.valueOf(ch);
}
}
return decrypted;
}
public static void main(String[] args)
{
String plaintext, ciphertext;
practical1a cc= new practical1a();
System.out.println("Enter text");
plaintext=new Scanner(System.in).nextLine().toUpperCase();
ciphertext=cc.encrypt(plaintext);
System.out.println("Encrypted text="+ciphertext);
plaintext=cc.decrypt(ciphertext);
System.out.println("Decrypted text="+plaintext);
} }
Output:
Modified Caesar Cipher
Code:
[Type here] [Type here] Khushal Choudhary 8606
}
} else
{
decrypted+=String.valueOf(ch);
}
}
return decrypted;
}
public static void main(String[] args)
{
String plaintext, ciphertext;
int key; practical1b cc= new
practical1b();
System.out.println("Enter text");
plaintext=new Scanner(System.in).nextLine().toUpperCase();
System.out.println("Enter key "); key=new
Scanner(System.in).nextInt();
ciphertext=cc.encrypt(plaintext,key);
System.out.println("Encrypted text="+ciphertext);
plaintext=cc.decrypt(ciphertext,key);
System.out.println("Decrypted text="+plaintext);
}
}
Output:
Monoalphabetic
[Type here] [Type here] Khushal Choudhary 8606
Code:
}
Output:
d) Poly-Alphabetic
Code:
import java.util.*;
public class Poly
{
public static void main(String[]args)
{
String plaintext="HOWAREYOU";
String secretkey="HELLOHELL";
System.out.println("Plain text before
encryption:"+plaintext);
String encryptedtext=encrypt(plaintext,secretkey);
System.out.println("Encrypted text after
encryption:"+encryptedtext);
String
decryptedtext=decrypt(encryptedtext,secretkey);
System.out.println("Decrypted text after
decryption:"+decryptedtext);
}
private static String encrypt(String plaintext, String
secretkey) {
StringBuffer encryptedString=new
StringBuffer(); int encryptedInt;
for(int i=0;i<plaintext.length();i++)
{ int
plaintextInt=(int)(plaintext.charAt(i));
int secretkeyInt=(int)(secretkey.charAt(i));
encryptedInt=(plaintextInt+secretkeyInt)%26;
encryptedString.append((char)((encryptedInt)+(int)'A'));
}
return encryptedString.toString();
}
private static String decrypt(String decryptedtext, String
secretkey) {
[Type here] [Type here] Khushal Choudhary 8606
StringBuffer decryptedString=new
StringBuffer(); int decryptedInt;
for(int i=0;i<decryptedtext.length();i++)
{
int
decryptedtextInt=(int)(decryptedtext.charAt(i));
int secretkeyInt=(int)(secretkey.charAt(i));
decryptedInt=decryptedtextInt-secretkeyInt;
if(decryptedInt<0)
Output:

More Related Content

Similar to 8606 ins prac 1.docx

import java.util.ArrayList; import java.util.Scanner;public clas.pdf
import java.util.ArrayList; import java.util.Scanner;public clas.pdfimport java.util.ArrayList; import java.util.Scanner;public clas.pdf
import java.util.ArrayList; import java.util.Scanner;public clas.pdfshettysachin2005
 
#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docxgertrudebellgrove
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File Rahul Chugh
 
Data structure
Data structureData structure
Data structureMarkustec
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptxadityaraj7711
 
#include stdio.h #include stdlib.h #include unistd.h #in.pdf
#include stdio.h #include stdlib.h #include unistd.h #in.pdf#include stdio.h #include stdlib.h #include unistd.h #in.pdf
#include stdio.h #include stdlib.h #include unistd.h #in.pdfcontact34
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1kkkseld
 
Add a 3rd field help that contains a short help string for each of t.pdf
Add a 3rd field help that contains a short help string for each of t.pdfAdd a 3rd field help that contains a short help string for each of t.pdf
Add a 3rd field help that contains a short help string for each of t.pdfinfo245627
 
Help with this substitution program- line 3 of the output is different.pdf
Help with this substitution program- line 3 of the output is different.pdfHelp with this substitution program- line 3 of the output is different.pdf
Help with this substitution program- line 3 of the output is different.pdfgaurav444u
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)teach4uin
 
talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013ericupnorth
 

Similar to 8606 ins prac 1.docx (14)

import java.util.ArrayList; import java.util.Scanner;public clas.pdf
import java.util.ArrayList; import java.util.Scanner;public clas.pdfimport java.util.ArrayList; import java.util.Scanner;public clas.pdf
import java.util.ArrayList; import java.util.Scanner;public clas.pdf
 
#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Data structure
Data structureData structure
Data structure
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
 
#include stdio.h #include stdlib.h #include unistd.h #in.pdf
#include stdio.h #include stdlib.h #include unistd.h #in.pdf#include stdio.h #include stdlib.h #include unistd.h #in.pdf
#include stdio.h #include stdlib.h #include unistd.h #in.pdf
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
C programms
C programmsC programms
C programms
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Add a 3rd field help that contains a short help string for each of t.pdf
Add a 3rd field help that contains a short help string for each of t.pdfAdd a 3rd field help that contains a short help string for each of t.pdf
Add a 3rd field help that contains a short help string for each of t.pdf
 
Help with this substitution program- line 3 of the output is different.pdf
Help with this substitution program- line 3 of the output is different.pdfHelp with this substitution program- line 3 of the output is different.pdf
Help with this substitution program- line 3 of the output is different.pdf
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013
 

Recently uploaded

Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiSuhani Kapoor
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 

Recently uploaded (20)

Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 

8606 ins prac 1.docx

  • 1. [Type here] [Type here] Khushal Choudhary 8606 PRACTICAL NO. 1 Aim: Implementing Substitution Ciphers a) Caesar Cipher Code: // Practical 1 A import java.util.Scanner; public class practical1a { int total_alphabets=26,key=3; String encrypted=" ",decrypted=" "; char ch; public String encrypt (String plain) { for(int i=0;i<plain.length();i++) { ch=plain.charAt(i); if(ch==' ') encrypted+="-"; else if(ch>='A' && ch<='Z') { if(ch<='Z'-key) { encrypted+=String.valueOf((char)(ch+key)); } else { encrypted+=String.valueOf((char)(ch- (total_alphabetskey))); } } else { encrypted+=String.valueOf(ch); } } return encrypted; } public String decrypt (String cipher) { for(int i=0;i<cipher.length();i++) { ch=cipher.charAt(i); if(ch==' ') decrypted+="-"; else if(ch>='A' && ch<='Z') { if(ch>='A'+key) {
  • 2. [Type here] [Type here] Khushal Choudhary 8606 decrypted+=String.valueOf((char)(ch-key)); } else { decrypted+=String.valueOf((char)(ch+(total_alphabetskey))); } } else { decrypted+=String.valueOf(ch); } } return decrypted; } public static void main(String[] args) { String plaintext, ciphertext; practical1a cc= new practical1a(); System.out.println("Enter text"); plaintext=new Scanner(System.in).nextLine().toUpperCase(); ciphertext=cc.encrypt(plaintext); System.out.println("Encrypted text="+ciphertext); plaintext=cc.decrypt(ciphertext); System.out.println("Decrypted text="+plaintext); } } Output: Modified Caesar Cipher Code:
  • 3. [Type here] [Type here] Khushal Choudhary 8606 } } else { decrypted+=String.valueOf(ch); } } return decrypted; } public static void main(String[] args) { String plaintext, ciphertext; int key; practical1b cc= new practical1b(); System.out.println("Enter text"); plaintext=new Scanner(System.in).nextLine().toUpperCase(); System.out.println("Enter key "); key=new Scanner(System.in).nextInt(); ciphertext=cc.encrypt(plaintext,key); System.out.println("Encrypted text="+ciphertext); plaintext=cc.decrypt(ciphertext,key); System.out.println("Decrypted text="+plaintext); } } Output: Monoalphabetic
  • 4. [Type here] [Type here] Khushal Choudhary 8606 Code: } Output: d) Poly-Alphabetic Code: import java.util.*; public class Poly { public static void main(String[]args) { String plaintext="HOWAREYOU"; String secretkey="HELLOHELL"; System.out.println("Plain text before encryption:"+plaintext); String encryptedtext=encrypt(plaintext,secretkey); System.out.println("Encrypted text after encryption:"+encryptedtext); String decryptedtext=decrypt(encryptedtext,secretkey); System.out.println("Decrypted text after decryption:"+decryptedtext); } private static String encrypt(String plaintext, String secretkey) { StringBuffer encryptedString=new StringBuffer(); int encryptedInt; for(int i=0;i<plaintext.length();i++) { int plaintextInt=(int)(plaintext.charAt(i)); int secretkeyInt=(int)(secretkey.charAt(i)); encryptedInt=(plaintextInt+secretkeyInt)%26; encryptedString.append((char)((encryptedInt)+(int)'A')); } return encryptedString.toString(); } private static String decrypt(String decryptedtext, String secretkey) {
  • 5. [Type here] [Type here] Khushal Choudhary 8606 StringBuffer decryptedString=new StringBuffer(); int decryptedInt; for(int i=0;i<decryptedtext.length();i++) { int decryptedtextInt=(int)(decryptedtext.charAt(i)); int secretkeyInt=(int)(secretkey.charAt(i)); decryptedInt=decryptedtextInt-secretkeyInt; if(decryptedInt<0) Output: