Home  >  Article  >  Java  >  Java implements encryption and decryption of strings

Java implements encryption and decryption of strings

黄舟
黄舟Original
2017-06-04 09:28:052747browse

This article mainly introduces JavaStringencryption and decryption in detail. It encrypts and decrypts the value of each character entered by the user. It has certain reference value. Interested friends can Refer to the

requirements:

* Encrypt the value of each character entered by the user, and output the decrypted string
* Right The encrypted string input by the user is decrypted and output

Implementation code:

import java.util.Scanner;
/**
 * 要求:
 *  1.对用户输入的每个字符的值进行加密,将解密后的字符串输出
 *  2.对用户输入的已加密字符串进行解密并输出
 * @author Administration
 *
 */
public class Encryption {

 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.println("请输入一个英文字符串或揭秘字符串");
  //获取用户输入
   String password = input.nextLine();
   //讲获取的字符串转成字符数组
   char[] c = password.toCharArray();
   //使用for循环给字符数组加密
   for(int i=0;i

Running result:

*Encryption Process:

Please enter an English string or a secret string:
I Love You
The result after encryption or decryption is as follows:
乩一乬无狠gai久一乹无乕

##*Decryption process:

Please enter an English string or a secret string:

The result after encryption or decryption is as follows:
I Love You


Principle analysis:

The key technology in this example is XOR operation.

Principle: If a character (or value) A is XORed with a value B to obtain C, then C and B can be used to perform subsequent operations to restore A.

The above is the detailed content of Java implements encryption and decryption of strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn