Caesar Encryption Problem Program

Source: Internet
Author: User

Design idea: The process of encrypting is to move the letter in the alphabet two bits backwards, i.e. A programming D, string +3, decrypted string -3 ;

Program Flowchart:

Source:

Wang Zhewen Caesar problem encryption and decryption key: Encryption is a string +3, decryption time-3

Package string11;

Import Java.util.Scanner;

public class Jiami

{

public static void Main (string[] args) throws Exception

{

TODO auto-generated Method Stub

System.out.println ("[A encryption] [J decryption],please Choose one");

Scanner s=new Scanner (system.in);//Create Scanner Object

String S1=s.nextline ();//Get the bank string

if (S1.equalsignorecase ("a"))//Determine the size of S1 and a

{

int key;

System.out.println ("Please enter clear text:");

Scanner sc=new Scanner (system.in);

String Ss=sc.nextline ();

System.out.println ("Please enter key:");

Scanner scc=new Scanner (system.in);

Key=scc.nextint ();//convert input characters to int type

Encryption (Ss,key);//Call encryption method

}

else if (s1.equalsignorecase ("J"));

{

int key;

System.out.println ("Please enter ciphertext:");

Scanner sc=new Scanner (system.in);

String SS =sc.nextline ();

System.out.println ("Please enter key:");

Scanner scc=new Scanner (system.in);

Key=scc.nextint ();

Decrypt (Ss,key);//Call encryption method

}

}

Encryption Program

public static void encryption (String str,int t)

{

String string= "";

int i;

char c;

For (I=1;i<str.length (); i++)

{

C=str.charat (i);

if (c>= ' a ' &&c<= ' z ')//If a character in a character is lowercase

{

C+=t% 26;///Move 26 bit

if (c< ' a ')

c+=26;//to the Zou world

if (c> ' z ')

c-=26;//Right over bounds

}

else if (c>= ' a ' &&c<= ' Z ')//If one of the characters in the character is uppercase

{

C+=t% 26;

if (c< ' A ')

c+=26;//to the Zou world

if (c> ' Z ')

c-=26;//Right over bounds

}

String +=c;//the encrypted character into a string

}

SYSTEM.OUT.PRINTLN (str + "after encryption:" + string);

}

public static void Decrypt (String str,int N)

{

int t;

T=integer.parseint ("-" +n);

String string= "";

int i;

For (I=0;i<str.length (); i++)

{

Char C=str.charat (i);

if (c>= ' a ' &&c<= ' z ')//If a character in a character is lowercase

{

C+=t% 26;//Move 26 bit

if (c< ' a ')

c+=26;//to the Zou world

if (c> ' z ')

c-=26;//Right over bounds

}

else if (c>= ' a ' &&c<= ' Z ')//If one of the characters in the character is uppercase

{

C+=t% 26;

if (c< ' A ')

c+=26;//to the Zou world

if (c> ' Z ')

c-=26;//Right over bounds

}

String +=c;//the encrypted character into a string

}

SYSTEM.OUT.PRINTLN (str + "after decryption:" + string);

}

}

Caesar Encryption Problem Program

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.