Java Third Course Assignment

Source: Internet
Author: User

One. String encryption

1. Design Ideas

Enter the English string, encrypt or decrypt the English string according to the encryption or decryption password, and finally output the encrypted or decrypted string

2. Program Flowchart

3. Source code

Package Kaisa;
Import Java.util.Scanner;
public class Kaisa
{
public static void Main (string[] args)
{
SYSTEM.OUT.PRINTLN ("Please select: 1. Encrypt 2. Decrypt");
Scanner c=new Scanner (system.in);
String Ss=c.nextline ();
if (Ss.equalsignorecase ("1"))//if statement to determine whether to encrypt or decrypt
{
System.out.println ("Please enter an English string to encrypt");
Scanner sc=new Scanner (system.in);
String S=sc.nextline ();
System.out.println ("Enter encryption password");
Scanner sc1=new Scanner (system.in);
int Key=sc1.nextint ();
Jiami (S,key);
}
else if (Ss.equalsignorecase ("2"));
{
System.out.println ("Please enter the English string to decrypt");
Scanner sc=new Scanner (system.in);
String S=sc.nextline ();
System.out.println ("Please enter the decryption password");
Scanner sc1=new Scanner (system.in);
int Key=sc1.nextint ();
Jiemi (S,key);
}
}

public static void Jiami (String str,int k)//cryptographic function
{
String string= "";
for (int i=0;i<str.length (); i++)
{
Char C=str.charat (i);
if (c>= ' a ' &&c<= ' Z ')
{
c+=k%26;
if (c< ' a ')
c+=26;
if (c> ' z ')
c-=26;
}
else if (c>= ' A ' &&c<= ' Z ')
{
c+=k%26;
if (c< ' A ')
c+=26;
if (c> ' Z ')
c-=26;
}
String+=c;
}
System.out.println (str+ "after encryption:" +string);
}

public static void Jiemi (String str,int N)//decryption function
{
int K=integer.parseint ("-" +n);
String string= "";
for (int i=0;i<str.length (); i++)
{
Char C=str.charat (i);
if (c>= ' a ' &&c<= ' Z ')
{
c+=k%26;
if (c< ' a ')
{
c+=26;
}
if (c> ' z ')
{
c-=26;
}
else if (c>= ' A ' &&c<= ' Z ')
{
c+=k%26;
if (c< ' A ')
{
c+=26;
}
if (c> ' Z ')
{
c-=26;
}
String+=c;
}
System.out.println (str+ "after encryption:" +string);
}
}

4. Results

Java Third Course Assignment

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.