SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
import java.util.Scanner;
public class Vigenere {
public static String encrypt(String text, final String key)
{
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
j = ++j % key.length();
}
return res;
}
public static String decrypt(String text, final String key)
{
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
j = ++j % key.length();
}
return res;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a multi-word message with punctuation:");
String message = in.nextLine();
System.out.println("Enter a single word key with no punctuation:");
String key = in.nextLine();
String encodedMsg = encrypt(message, key);
System.out.println("The encoded message is:");
System.out.println(encodedMsg);
String decodedMsg = decrypt(encodedMsg, key);
System.out.println("The decoded message is:");
System.out.println(decodedMsg);
}
}
OUTPUT:
Enter a multi-word message with punctuation:
The eagle has landed.
Enter a single word key with no punctuation:
LINKED
The encoded message is:
EPROEJWMUKWOLVQOH
The decoded message is:
THEEAGLEHASLANDED
Solution
import java.util.Scanner;
public class Vigenere {
public static String encrypt(String text, final String key)
{
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
j = ++j % key.length();
}
return res;
}
public static String decrypt(String text, final String key)
{
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
j = ++j % key.length();
}
return res;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a multi-word message with punctuation:");
String message = in.nextLine();
System.out.println("Enter a single word key with no punctuation:");
String key = in.nextLine();
String encodedMsg = encrypt(message, key);
System.out.println("The encoded message is:");
System.out.println(encodedMsg);
String decodedMsg = decrypt(encodedMsg, key);
System.out.println("The decoded message is:");
System.out.println(decodedMsg);
}
}
OUTPUT:
Enter a multi-word message with punctuation:
The eagle has landed.
Enter a single word key with no punctuation:
LINKED
The encoded message is:
EPROEJWMUKWOLVQOH
The decoded message is:
THEEAGLEHASLANDED

Weitere ähnliche Inhalte

Ähnlich wie import java.util.Scanner; public class Vigenere {    public .pdf

Network security
Network securityNetwork security
Network securitybabyangle
 
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfCountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfpremsrivastva8
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfRohitkumarYadav80
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
import java.util.Scanner; public class Palin { public static v.pdf
import java.util.Scanner; public class Palin { public static v.pdfimport java.util.Scanner; public class Palin { public static v.pdf
import java.util.Scanner; public class Palin { public static v.pdfLAMJM
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)Tak Lee
 
!DOCTYPE htmlhtml lang=enhead meta charset=utf.docx
!DOCTYPE htmlhtml lang=enhead  meta charset=utf.docx!DOCTYPE htmlhtml lang=enhead  meta charset=utf.docx
!DOCTYPE htmlhtml lang=enhead meta charset=utf.docxkatherncarlyle
 
manipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptxmanipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptxShowribabuKanta
 
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docxlab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docxDIPESH30
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdfanupamfootwear
 

Ähnlich wie import java.util.Scanner; public class Vigenere {    public .pdf (16)

Network security
Network securityNetwork security
Network security
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfCountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
import java.util.Scanner; public class Palin { public static v.pdf
import java.util.Scanner; public class Palin { public static v.pdfimport java.util.Scanner; public class Palin { public static v.pdf
import java.util.Scanner; public class Palin { public static v.pdf
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
week-6x
week-6xweek-6x
week-6x
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
JAVA.pdf
JAVA.pdfJAVA.pdf
JAVA.pdf
 
!DOCTYPE htmlhtml lang=enhead meta charset=utf.docx
!DOCTYPE htmlhtml lang=enhead  meta charset=utf.docx!DOCTYPE htmlhtml lang=enhead  meta charset=utf.docx
!DOCTYPE htmlhtml lang=enhead meta charset=utf.docx
 
manipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptxmanipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptx
 
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docxlab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 
String
StringString
String
 
Ann
AnnAnn
Ann
 

Mehr von Ankitagarwaleleraipu

A is correct. Public and private community strings are used within S.pdf
A is correct. Public and private community strings are used within S.pdfA is correct. Public and private community strings are used within S.pdf
A is correct. Public and private community strings are used within S.pdfAnkitagarwaleleraipu
 
4 ansit is periodic signal andSolution4 ansit is periodic .pdf
4 ansit is periodic signal andSolution4 ansit is periodic .pdf4 ansit is periodic signal andSolution4 ansit is periodic .pdf
4 ansit is periodic signal andSolution4 ansit is periodic .pdfAnkitagarwaleleraipu
 
1. The smart phone, computer, navigation system and digital scanner .pdf
1. The smart phone, computer, navigation system and digital scanner .pdf1. The smart phone, computer, navigation system and digital scanner .pdf
1. The smart phone, computer, navigation system and digital scanner .pdfAnkitagarwaleleraipu
 
1-They have terrestrial adaptation and dominant sporophytic phase..pdf
1-They have terrestrial adaptation and dominant sporophytic phase..pdf1-They have terrestrial adaptation and dominant sporophytic phase..pdf
1-They have terrestrial adaptation and dominant sporophytic phase..pdfAnkitagarwaleleraipu
 
What does absense of MHC Class I indicateAlmost all nucleated cel.pdf
What does absense of MHC Class I indicateAlmost all nucleated cel.pdfWhat does absense of MHC Class I indicateAlmost all nucleated cel.pdf
What does absense of MHC Class I indicateAlmost all nucleated cel.pdfAnkitagarwaleleraipu
 
The probabilities must add up to one. Add the ones you have and su.pdf
The probabilities must add up to one. Add the ones you have and su.pdfThe probabilities must add up to one. Add the ones you have and su.pdf
The probabilities must add up to one. Add the ones you have and su.pdfAnkitagarwaleleraipu
 
The most likely F1 will be the one that is made from 4 and 5. This i.pdf
The most likely F1 will be the one that is made from 4 and 5. This i.pdfThe most likely F1 will be the one that is made from 4 and 5. This i.pdf
The most likely F1 will be the one that is made from 4 and 5. This i.pdfAnkitagarwaleleraipu
 
Subculturing or passage is the method of preserving or continuing th.pdf
Subculturing or passage is the method of preserving or continuing th.pdfSubculturing or passage is the method of preserving or continuing th.pdf
Subculturing or passage is the method of preserving or continuing th.pdfAnkitagarwaleleraipu
 
public class Interest{double interest(double rate, double amount){.pdf
public class Interest{double interest(double rate, double amount){.pdfpublic class Interest{double interest(double rate, double amount){.pdf
public class Interest{double interest(double rate, double amount){.pdfAnkitagarwaleleraipu
 
Phototaxisthe Phenomenon of movement of motile unicellular organis.pdf
Phototaxisthe Phenomenon of movement of motile unicellular organis.pdfPhototaxisthe Phenomenon of movement of motile unicellular organis.pdf
Phototaxisthe Phenomenon of movement of motile unicellular organis.pdfAnkitagarwaleleraipu
 
Miller indices                     It is defined as by considerin.pdf
Miller indices                     It is defined as by considerin.pdfMiller indices                     It is defined as by considerin.pdf
Miller indices                     It is defined as by considerin.pdfAnkitagarwaleleraipu
 
Marijuana is also called as cannabis and it is a preparation of cann.pdf
Marijuana is also called as cannabis and it is a preparation of cann.pdfMarijuana is also called as cannabis and it is a preparation of cann.pdf
Marijuana is also called as cannabis and it is a preparation of cann.pdfAnkitagarwaleleraipu
 
IHD = 1 Therefore there can be only 1 -bond or ring. The two signals.pdf
IHD = 1 Therefore there can be only 1 -bond or ring. The two signals.pdfIHD = 1 Therefore there can be only 1 -bond or ring. The two signals.pdf
IHD = 1 Therefore there can be only 1 -bond or ring. The two signals.pdfAnkitagarwaleleraipu
 
I have the answer ready in paint but i am not able to upload. 1) F.pdf
I have the answer ready in paint but i am not able to upload. 1) F.pdfI have the answer ready in paint but i am not able to upload. 1) F.pdf
I have the answer ready in paint but i am not able to upload. 1) F.pdfAnkitagarwaleleraipu
 
homedesigninsertanimationsdevelopmentSolutionhomedes.pdf
homedesigninsertanimationsdevelopmentSolutionhomedes.pdfhomedesigninsertanimationsdevelopmentSolutionhomedes.pdf
homedesigninsertanimationsdevelopmentSolutionhomedes.pdfAnkitagarwaleleraipu
 
C is correct. DHCP is a statefull method of configuring IPv6 address.pdf
C is correct. DHCP is a statefull method of configuring IPv6 address.pdfC is correct. DHCP is a statefull method of configuring IPv6 address.pdf
C is correct. DHCP is a statefull method of configuring IPv6 address.pdfAnkitagarwaleleraipu
 

Mehr von Ankitagarwaleleraipu (20)

A is correct. Public and private community strings are used within S.pdf
A is correct. Public and private community strings are used within S.pdfA is correct. Public and private community strings are used within S.pdf
A is correct. Public and private community strings are used within S.pdf
 
4 ansit is periodic signal andSolution4 ansit is periodic .pdf
4 ansit is periodic signal andSolution4 ansit is periodic .pdf4 ansit is periodic signal andSolution4 ansit is periodic .pdf
4 ansit is periodic signal andSolution4 ansit is periodic .pdf
 
3030Solution3030.pdf
3030Solution3030.pdf3030Solution3030.pdf
3030Solution3030.pdf
 
5Solution5.pdf
5Solution5.pdf5Solution5.pdf
5Solution5.pdf
 
1. The smart phone, computer, navigation system and digital scanner .pdf
1. The smart phone, computer, navigation system and digital scanner .pdf1. The smart phone, computer, navigation system and digital scanner .pdf
1. The smart phone, computer, navigation system and digital scanner .pdf
 
1-They have terrestrial adaptation and dominant sporophytic phase..pdf
1-They have terrestrial adaptation and dominant sporophytic phase..pdf1-They have terrestrial adaptation and dominant sporophytic phase..pdf
1-They have terrestrial adaptation and dominant sporophytic phase..pdf
 
(1)Solution(1).pdf
(1)Solution(1).pdf(1)Solution(1).pdf
(1)Solution(1).pdf
 
What does absense of MHC Class I indicateAlmost all nucleated cel.pdf
What does absense of MHC Class I indicateAlmost all nucleated cel.pdfWhat does absense of MHC Class I indicateAlmost all nucleated cel.pdf
What does absense of MHC Class I indicateAlmost all nucleated cel.pdf
 
The probabilities must add up to one. Add the ones you have and su.pdf
The probabilities must add up to one. Add the ones you have and su.pdfThe probabilities must add up to one. Add the ones you have and su.pdf
The probabilities must add up to one. Add the ones you have and su.pdf
 
The most likely F1 will be the one that is made from 4 and 5. This i.pdf
The most likely F1 will be the one that is made from 4 and 5. This i.pdfThe most likely F1 will be the one that is made from 4 and 5. This i.pdf
The most likely F1 will be the one that is made from 4 and 5. This i.pdf
 
Subculturing or passage is the method of preserving or continuing th.pdf
Subculturing or passage is the method of preserving or continuing th.pdfSubculturing or passage is the method of preserving or continuing th.pdf
Subculturing or passage is the method of preserving or continuing th.pdf
 
public class Interest{double interest(double rate, double amount){.pdf
public class Interest{double interest(double rate, double amount){.pdfpublic class Interest{double interest(double rate, double amount){.pdf
public class Interest{double interest(double rate, double amount){.pdf
 
Phototaxisthe Phenomenon of movement of motile unicellular organis.pdf
Phototaxisthe Phenomenon of movement of motile unicellular organis.pdfPhototaxisthe Phenomenon of movement of motile unicellular organis.pdf
Phototaxisthe Phenomenon of movement of motile unicellular organis.pdf
 
Miller indices                     It is defined as by considerin.pdf
Miller indices                     It is defined as by considerin.pdfMiller indices                     It is defined as by considerin.pdf
Miller indices                     It is defined as by considerin.pdf
 
Marijuana is also called as cannabis and it is a preparation of cann.pdf
Marijuana is also called as cannabis and it is a preparation of cann.pdfMarijuana is also called as cannabis and it is a preparation of cann.pdf
Marijuana is also called as cannabis and it is a preparation of cann.pdf
 
IHD = 1 Therefore there can be only 1 -bond or ring. The two signals.pdf
IHD = 1 Therefore there can be only 1 -bond or ring. The two signals.pdfIHD = 1 Therefore there can be only 1 -bond or ring. The two signals.pdf
IHD = 1 Therefore there can be only 1 -bond or ring. The two signals.pdf
 
I have the answer ready in paint but i am not able to upload. 1) F.pdf
I have the answer ready in paint but i am not able to upload. 1) F.pdfI have the answer ready in paint but i am not able to upload. 1) F.pdf
I have the answer ready in paint but i am not able to upload. 1) F.pdf
 
homedesigninsertanimationsdevelopmentSolutionhomedes.pdf
homedesigninsertanimationsdevelopmentSolutionhomedes.pdfhomedesigninsertanimationsdevelopmentSolutionhomedes.pdf
homedesigninsertanimationsdevelopmentSolutionhomedes.pdf
 
eSolutione.pdf
eSolutione.pdfeSolutione.pdf
eSolutione.pdf
 
C is correct. DHCP is a statefull method of configuring IPv6 address.pdf
C is correct. DHCP is a statefull method of configuring IPv6 address.pdfC is correct. DHCP is a statefull method of configuring IPv6 address.pdf
C is correct. DHCP is a statefull method of configuring IPv6 address.pdf
 

Kürzlich hochgeladen

Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Kürzlich hochgeladen (20)

Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

import java.util.Scanner; public class Vigenere {    public .pdf

  • 1. import java.util.Scanner; public class Vigenere { public static String encrypt(String text, final String key) { String res = ""; text = text.toUpperCase(); for (int i = 0, j = 0; i < text.length(); i++) { char c = text.charAt(i); if (c < 'A' || c > 'Z') continue; res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A'); j = ++j % key.length(); } return res; } public static String decrypt(String text, final String key) { String res = ""; text = text.toUpperCase(); for (int i = 0, j = 0; i < text.length(); i++) { char c = text.charAt(i); if (c < 'A' || c > 'Z') continue; res += (char) ((c - key.charAt(j) + 26) % 26 + 'A'); j = ++j % key.length(); } return res; } public static void main(String[] args) {
  • 2. Scanner in = new Scanner(System.in); System.out.println("Enter a multi-word message with punctuation:"); String message = in.nextLine(); System.out.println("Enter a single word key with no punctuation:"); String key = in.nextLine(); String encodedMsg = encrypt(message, key); System.out.println("The encoded message is:"); System.out.println(encodedMsg); String decodedMsg = decrypt(encodedMsg, key); System.out.println("The decoded message is:"); System.out.println(decodedMsg); } } OUTPUT: Enter a multi-word message with punctuation: The eagle has landed. Enter a single word key with no punctuation: LINKED The encoded message is: EPROEJWMUKWOLVQOH The decoded message is: THEEAGLEHASLANDED Solution import java.util.Scanner; public class Vigenere { public static String encrypt(String text, final String key) { String res = ""; text = text.toUpperCase(); for (int i = 0, j = 0; i < text.length(); i++) { char c = text.charAt(i);
  • 3. if (c < 'A' || c > 'Z') continue; res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A'); j = ++j % key.length(); } return res; } public static String decrypt(String text, final String key) { String res = ""; text = text.toUpperCase(); for (int i = 0, j = 0; i < text.length(); i++) { char c = text.charAt(i); if (c < 'A' || c > 'Z') continue; res += (char) ((c - key.charAt(j) + 26) % 26 + 'A'); j = ++j % key.length(); } return res; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter a multi-word message with punctuation:"); String message = in.nextLine(); System.out.println("Enter a single word key with no punctuation:"); String key = in.nextLine(); String encodedMsg = encrypt(message, key); System.out.println("The encoded message is:"); System.out.println(encodedMsg); String decodedMsg = decrypt(encodedMsg, key); System.out.println("The decoded message is:"); System.out.println(decodedMsg); }
  • 4. } OUTPUT: Enter a multi-word message with punctuation: The eagle has landed. Enter a single word key with no punctuation: LINKED The encoded message is: EPROEJWMUKWOLVQOH The decoded message is: THEEAGLEHASLANDED