Self-lazy, look at the feeling of the reliable
XOR operation:
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
The ASCII encoding of the character ' A ' is 65:00000000 01000001
Take integers 7:00000000 00000000 00000000 00000111
After XOR operation: 00000000 00000000 00000000 01000110
The simple cryptographic algorithm code is as follows:
public class Test {public
static final int KEY = 7;
public static void Main (string[] args) {
String str = "Hello world!";
StringBuffer str2 = new StringBuffer (); Stores the encrypted string
stringbuffer STR3 = new StringBuffer (); Stores the decrypted string
//Encryption procedure for
(int i=0;i<str.length (); i++)
{
char c = (char) (Str.charat (i) ^ KEY);
Str2.append (c);
}
Decryption process for
(int i=0;i<str2.length (); i++)
{
char c = (char) (Str2.charat (i) ^ KEY);
Str3.append (c);
}
System.out.println ("Original string is:" + str);
System.out.println ("Encrypted string is:" + str2);
System.out.println ("Decrypted string is:" + STR3);}
}
Output:
The original string is: Hello world!
The encrypted string is: Obkkh ' phukc&
The decrypted string is: Hello world!