Java Encryption jobs

Source: Internet
Author: User

The ancient Roman Emperor Caesar used the following methods to encrypt military intelligence during the war: please write a program that uses the above algorithm to encrypt or decrypt the English string entered by the user.

Programming Ideas:

First, the user enters a string that needs to be encrypted, then defines the function to get the string length, converts the string to a single character through A For loop, "+3" for each character, moves backwards by 3, each character "3", moves forward 3, defines str2, and assigned each new word to str2, Get the encrypted English string str2, define the STR3, the newly obtained each word assigned to STR3, get the decrypted English string str3, output the encrypted substring str2, output the decrypted sub-string STR3

Program Source code:

Package Demo;import Java.util.scanner;public class Mi {public static void main (string[] args) {//TODO auto-generated method stub Scanner SC An=new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Telegram requiring encryption is:"); String str;str=scan.nextline ();//nextline function accepts data int length;length=str.length (); String Str1=new string (); String Str2=new string (); String Str3=new string (), char chuan;char qian,hou;for (int i=0;i<length;i++) {str1=str.substring (i,i+1); chuan= Str1.charat (0); qian= (char) (chuan+3); hou= (char) (chuan-3); str2=qian+str2;str3=hou+str3;} SYSTEM.OUT.PRINTLN ("The Encrypted Telegram is:" +STR2); System.out.println ("The Decrypted Telegram is:" +STR3);}}

Program Run Result:

Java Encryption jobs

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.