Java Classroom assignment Encryption and decryption

Source: Internet
Author: User

1. Design Ideas

First enter a string as prompted

Use Charat () to break down each character of a string, convert it to an int type and add 3, and then convert to type int minus 3 and then to char type

New defines a string variable that links the converted char to the output

2. Flowchart

3. Source code

Package test;
Import Java.util.Scanner;
Class Cipher
{
Scanner input=new Scanner (system.in);
void Incipher ()
{
System.out.println ("Please enter the string you want to encrypt:");
String Str=input.nextline ();
String str2= "";
for (int i=0;i<str.length (); i++)
{
int n= (int) Str.charat (i) +3;
str2=str2+ (char) n;
}
System.out.println ("The string after encryption is:" +STR2);
}
void Outcipher ()
{
System.out.println ("Please enter the string you want to decrypt:");
String Str=input.nextline ();
String str2= "";
for (int i=0;i<str.length (); i++)
{
int n= (int) Str.charat (i)-3;
str2=str2+ (char) n;
}
System.out.println ("Decrypted string is:" +STR2);
}
}
public class Classtest5
{
public static void Main (string[] args)
{
Cipher c=new Cipher ();
Scanner input=new Scanner (system.in);
int choice;
Do
{
System.out.println ("Select if you want to encrypt or decrypt: 1. Encrypt 2. Decrypt");
int Choise=input.nextint ();
if (choise==1)
{
C.incipher ();
}
else if (choise==2)
{
C.outcipher ();
}
Else
SYSTEM.OUT.PRINTLN ("Input Error! ");
System.out.println ("Do you want to continue?" 1. Yes 2. No ");
Choice=input.nextint ();
}while (choice==1);

}

}

4.

Java Classroom assignment Encryption and decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.