Using the relationship between strings and arrays learned earlier, use a string password as the password to encrypt another string source String . The operation process is as follows:
1) Store the password password in a character array:
char[]p=password.toCharArray();
2) Assume that the length of the array p is n, then the string sourceString to be encrypted is divided into groups of n characters in sequence, and the characters in each group are added using the corresponding characters of the array a.
Note : The number of characters in the last group can be less than n.
For example: n characters in a certain group are a0a1...an-1, then the encryption result of the group of characters c0c1...cn-1 is obtained as follows:
c0 = (char)(a0 + p[0]), c1 = (char)(a1 + p[1]), cn-1 = (char)(an-1 + p[n-1]).
3) Finally, convert the character array c into a string to obtain the ciphertext of sourceString.
The decryption algorithm of the above encryption algorithm is to perform a subtraction operation on the ciphertext.