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<c.length;i++){ c[i] = (char)(c[i]^20000); } //输出加密或者解密结果 System.out.println("加密或者解密之后的结果如下:"); System.out.println(new String(c)); } }
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
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!