Custom Base16 encryption and base16 Encryption
Custom Base16 encryption principle
Base16 encryption is a little different from Base64 encryption. Of course, the previous conversion is the same. It is to convert the input string into a byte sequence according to the default encoding, this byte sequence actually contains the stored ASCII code,Second,Convert each ASCII code to an eight-bit binary code. Each eight-bit binary code is split into four-bit binary codes. Then, convert each four-bit binary codes to a decimal number. Finally, these strings are encrypted strings after the ciphertext characters are indexed in decimal numbers.
Example: abc
ASCII code: 97 98 99
Binary: 01100001 01100010 01100011
Split into four groups: 0110 0001 0110 0010 0110 0011
Decimal: 6 1 6 2 6 3
Based on the characters in the decimal index ciphertext subscript, the Code is as follows:
/// <Summary> /// custom Base16 encryption /// </summary> /// <param name = "str"> encrypted string </param> /// <param name = "autoCode"> Custom ciphertext, 16 elements, which can be numbers, characters, and special characters. If this parameter is not specified, the default ciphertext is used, decryption is the same as encrypted ciphertext </param> // <returns> </returns> public static string AutoBase16Encrypt (string str, string [] autoCode) {string innerStr = string. empty; StringBuilder strEn = new StringBuilder (); if (autoCode = null | autoCode. length <16) autoCode = new string [] {"a", "2", "B", "g", "E", "5", "f ", "6", "C", "8", "o", "9", "Z", "p", "k", "M"}; System. collections. arrayList arr = new System. collections. arrayList (System. text. encoding. default. getBytes (str); for (int I = 0; I <arr. count; I ++) {byte data = (byte) arr [I]; int v1 = data> 4; strEn. append (autoCode [v1]); int v2 = (data & 0x0f) <4)> 4; strEn. append (autoCode [v2]);} return strEn. toString ();}
Customize Base16 decryption principles
In fact, the decryption principle is also very simple. First, split the encrypted string into characters, and then find the lower mark values of the first and second characters based on the characters. Convert the first base value to an 8-bit binary value, move the value four places to the left, and combine the base value of the second character into a byte, Which is saved in the byte array. Finally, convert the saved byte array to a string based on the default encoding. (I have added an unknown explanation for decryption)
/// <Summary> /// customize Base16 decryption /// </summary> /// <param name = "str"> decrypt the string </param> /// <param name = "autoCode"> Custom ciphertext, 16 elements, which can be numbers, characters, and special characters. If this parameter is not specified, the default ciphertext is used, decryption is the same as encrypted ciphertext </param> // <returns> </returns> public static string AutoBase16Decrypt (string str, string [] autoCode) {int k = 0; string dnStr = string. empty; int strLength = str. length; if (autoCode = null | autoCode. length <16) autoCode = new string [] {"a", "2", "B", "g", "E", "5", "f ", "6", "C", "8", "o", "9", "Z", "p", "k", "M "}; byte [] data = new byte [strLength/2]; for (int I = 0, j = 0; I <data. length; I ++, j ++) {byte s = 0; int index1 = autoCode. toList (). indexOf (str [j]. toString (); j + = 1; int index2 = autoCode. toList (). indexOf (str [j]. toString (); s = (byte) (s ^ index1); s = (byte) (s <4); s = (byte) (s ^ index2 ); data [k] = s; k ++;} dnStr = Encoding. default. getString (data); return dnStr ;}
Base16 ciphertext
Finally, let's talk about this ciphertext. This ciphertext is a string array. The total number of elements cannot be less than 16. Of course, more than 16 elements cannot be used. These 16 characters are completely customized, this is more flexible. Finally, let's look at a random ciphertext function.
/// <Summary> /// random ciphertext /// </summary> /// <returns> </returns> public string [] RandomEncrypt () {string [] code = new string [16]; Random random = new Random (); int j = 0; for (int I = 0; 1 <2; I ++) {char ch = (char) random. next (1,128); if (code. toList (). indexOf (ch. toString () <0 & (ch> = '0' & ch <= '9 ') | (ch> = 'A' & ch <= 'Z '))) {code [j] = ch. toString (); j ++; } If (! Array. Exists (code, string. IsNullOrEmpty) & code. Length = 16) break;} return code ;}
Summary: The Base16 encryption and decryption I wrote is actually very simple, the principle is also very simple, suitable for beginners to learn experience, of course, this encryption and decryption can be expanded, if you have any new ideas or ideas, please let us know. Thank you.