Encryption and decryption of Java strings
•
Java
public static String encryptAndDencrypt(String value,char secret) {
byte[] bt = value.getBytes(); // 将需要加密的内容转换为字节数组
for (int i = 0; i < bt.length; i++) {
bt[i] = (byte) (bt[i] ^ (int) secret); // 通过异或运算进行加密
}
String newresult = new String(bt,bt.length); // 将加密后的字符串保存到 newresult 变量中
return newresult;
}
public static void main(String[] args) {
Scanner scan = new Scanner(system.in);
char secret = '8'; // 加密文字符
System.out.println("请输入您想加密的内容:");
String pass = scan.next();
System.out.println("原字符串内容:" + pass);
String encryptResult = encryptAndDencrypt(pass,secret);
System.out.println("加密后的内容:" + encryptResult);
String uncryptResult = encryptAndDencrypt(encryptResult,secret);
System.out.println("解密后的内容:" + uncryptResult);
}
请输入您想加密的内容: hello,java 原字符串内容:hello,java 加密后的内容:P]TTWRYNY 解密后的内容:hello,java
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
