Java small case--encrypt and decrypt a string

Source: Internet
Author: User

Requirements:
* Encrypt the value of each character entered by the user, and the decrypted string output
* Decrypt and output the encrypted string entered by the user

Implementation code:

ImportJava.util.Scanner;/*** Required: * 1. Encrypt the value of each character entered by the user, and output the decrypted string as * 2. Decrypts the encrypted string entered by the user and outputs *@authorAdministration **/ Public classEncryption { Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); System.out.println ("Please enter an English string or a secret string"); //Get user InputString Password =Input.nextline (); //talking about getting the string into a character array         Char[] C =Password.tochararray (); //encrypt a character array with a For loop          for(inti=0;i<c.length;i++) {C[i]= (Char) (c[i]^20000); }         //output encryption or decryption resultsSystem.out.println ("After encrypting or decrypting the result is as follows:"); System.out.println (NewString (c)); }}

Operation Result:

* Encryption Process:

Please enter an English string or a secret string: I Love you after encryption or decryption results are as follows: Planchette a 乬 long 乹 spent 乕

* Decryption Process:

Please enter an English string or a secret string: Planchette a 乬 long time a 乹 spent 乕 encryption or decryption results as follows: I Love you

Principle Analysis:

The key technique of this example is the XOR operation. Principle: If a character (or value) A and a value B are different or operation to get C, then use C and B for later operations can be restored to a.

Java small case--encrypt and decrypt a string

Related Article

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.