Let's simply encrypt the string

First of all, the so-called encryption here means that the value from the back end to the front end is different from th...

First of all, the so-called encryption here means that the value from the back end to the front end is different from the value from the back end to the front end

I'll stick the code first

/** * Image encryption, simple image encryption only for numbers and uppercase and lowercase letters * The number 0 corresponds to the number 9, the lowercase letter a corresponds to the lowercase letter z, and the uppercase letter B corresponds to the uppercase letter Y */ public class ImageEncryptionUtil { /** * The value corresponding to the number 0 in the ASCII table */ private static final int MIN_NUMBER_ASCII = 48; /** * The corresponding value of the number 9 in the ascii table */ private static final int MAX_NUMBER_ASCII = 57; /** * The corresponding value of the capital letter A in the ASCII table */ private static final int MIN_POWER_CASE_ASCII = 65; /** * The corresponding value of the capital letter Z in the ASCII table */ private static final int MAX_POWER_CASE_ASCII = 90; /** * The value of the lowercase letter a in the ASCII table */ private static final int MIN_LOWER_CASE_ASCII = 97; /** * The value of the lowercase letter z in the ASCII table */ private static final int MAX_LOWER_CASE_ASCII = 122; /** * encryption * * @param str String that needs to be encrypted * @param overturn Flip string * @return Returns an encrypted string */ public static String encrypt(String str, boolean overturn) { return disorganize(init(str, overturn)); } /** * encryption * The default is not to flip strings * * @param str String that needs to be encrypted * @return Returns an encrypted string */ public static String encrypt(String str) { return disorganize(init(str, false)); } /** * decrypt * * @param str String to decrypt * @return Returns the decrypted string */ public static String decrypt(String str) { return init(restore(str), false); } /** * decrypt * * @param str String to decrypt * @param overturn Flip string * @return Returns the decrypted string */ public static String decrypt(String str, boolean overturn) { return init(restore(str), overturn); } /** * Encryption master method * * @param str String that needs to be encrypted * @param overturn Flip string * @return Returns an encrypted string */ private static String init(String str, boolean overturn) { StringBuilder builder = new StringBuilder(); if (overturn) { for (int i = 0; i < str.length(); i++) { builder.append(transform(str.charAt(str.length() - i - 1))); } } else { for (int i = 0; i < str.length(); i++) { builder.append(transform(str.charAt(i))); } } return builder.toString(); } /** * Convert characters * * @param current ASCII code of characters to be converted * @return Returns the converted character */ private static char transform(int current) { if (isInRange(MIN_NUMBER_ASCII, MAX_NUMBER_ASCII, current)) { return (char) (MIN_NUMBER_ASCII + MAX_NUMBER_ASCII - current); } else if (isInRange(MIN_POWER_CASE_ASCII, MAX_POWER_CASE_ASCII, current)) { return (char) (MIN_POWER_CASE_ASCII + MAX_POWER_CASE_ASCII - current); } else if (isInRange(MIN_LOWER_CASE_ASCII, MAX_LOWER_CASE_ASCII, current)) { return (char) (MIN_LOWER_CASE_ASCII + MAX_LOWER_CASE_ASCII - current); } return (char) current; } /** * Judge whether the value is within the specified range * * @param min Range min * @param max Range Max * @param current Value to be judged * @return Return results */ private static boolean isInRange(int min, int max, int current) { return Math.max(min, current) == Math.min(max, current); } /** * Scramble string * * @param str String to be scrambled * @return Returns the scrambled string */ private static String disorganize(String str) { StringBuilder builder = new StringBuilder(); // Set the loop length to half the length of the string and round up int max = (int) Math.ceil(str.length() / 2.0); // Judge whether the length is odd boolean isOdd = (str.length() % 2 == 1); for (int i = 0; i < max; i++) { // Intercept and add characters from scratch builder.append(str.charAt(i)); // It ends directly when it is the last cycle and is odd if (i == max - 1 && isOdd) { break; } // Intercept and add characters from the end builder.append(str.charAt(str.length() - i - 1)); } return builder.toString(); } /** * Restore string * * @param str String to restore * @return Returns the restored string */ private static String restore(String str) { StringBuilder builder = new StringBuilder(); // The length of the string is used as the loop length int max = str.length(); // Judge whether the string length is odd boolean isOdd = (max % 2 == 1); // Set the span of i to 2 int duration = 2; // First, take the value of even digits from scratch and add for (int i = 0; i < max; i += duration) { builder.append(str.charAt(i)); } // Then take the value of odd digits from the end and add for (int i = 0; i < max - 1; i += duration) { builder.append(str.charAt(str.length() - i - (isOdd ? 2 : 1))); } return builder.toString(); } }

This encryption is called image encryption. The process is as follows:

First of all, all encryption algorithms are based on ASCII code, because ASCII code is the code we first came into contact with when learning computer technology (it should be binary to be accurate), so I will take this as the standard for encryption this time;

Suppose I need to encrypt the string "123abc", first encrypt the character '1'. I regard the character '1' as a number, the range of the number is 0-9, and the number 1 is the second digit from the beginning, so I take the second digit 8 from the end as the encrypted character of the number;

Similarly, I regard the character 'a' as a lowercase letter, the range of lowercase letters is a-z, and the letter A is the first letter from the beginning, so I take the first letter z from the end as the encrypted character of the letter A.

After that, the string "123abc" should be "876zyx" after encryption, but the structure of this string is basically the same as that before encryption, so I scramble it (in fact, it's not very messy...):

First, take the first character from the beginning of the encrypted string, then the first character from the end, then the second character from the beginning, and then the second character from the end

In this way, the string "123abc" becomes "8x7y6z" after a series of operations. Although it still looks very simple, it is not so obvious when the string to be encrypted is a randomly generated string (I have sent the code to generate a random string before, direct connection: According to different requirements (combination of numbers, lowercase letters and uppercase letters, new Chinese characters, combination of special characters, numbers and uppercase and lowercase letters), a blog CSDN blog with a random number _hjh70983704 of specified digits is generatedhttps://blog.csdn.net/hjh70983704/article/details/120623863?spm=1001.2014.3001.5501 )For example, the string "Vnyi0QE4B61a" is encrypted to "Ezm8b3rY95JV"“

ps: from the perspective of practicality and readability, this encryption only involves the encryption of numbers 0-9, lowercase letters A-Z and uppercase letters A-Z

Method call:

int strLength = 12; int strType = RandomStringUtil.FIGURE_LOWER_CASE_POWER_CASE; // Original string String str = RandomStringUtil.createRandomString(strLength, strType); System.out.println(str); // Encrypted string str = ImageEncryptionUtil.encrypt(str); System.out.println(str); // Decrypted string str = ImageEncryptionUtil.decrypt(str); System.out.println(str);

This method also includes whether to reverse the string after encryption. For example, if the string "123abc" is set to reverse, it returns "cba321". If the reverse is set in encryption, it must also be set in decryption:

int strLength = 12; int strType = RandomStringUtil.FIGURE_LOWER_CASE_POWER_CASE; // Original string String str = RandomStringUtil.createRandomString(strLength, strType); System.out.println(str); // Encrypted string str = ImageEncryptionUtil.encrypt(str, true); System.out.println(str); // Decrypted string str = ImageEncryptionUtil.decrypt(str, true); System.out.println(str);

19 November 2021, 11:14 | Views: 5378

Add new comment

For adding a comment, please log in
or create account

0 comments