C # customize Base16 encoding and decoding,

Source: Internet
Author: User

C # customize Base16 encoding and decoding,

 

I. Custom Base16 encoding principles

Base16 encoding is a bit different from Base64 encoding. Of course, the previous conversion is the same. It is to convert the input string into a byte sequence based on 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, the ciphertext underlying characters are indexed in decimal numbers to combine these strings into encoded strings.

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

According to the characters in the decimal index ciphertext subscript, the following code is encoded.

/// <Summary> /// custom Base16 encoding // </summary> /// <param name = "str"> string to be encoded </param>/ // <param name = "autoCode"> Custom Base16 encoded array, 16 elements, which can be numbers, characters, and special characters. If not specified, use the default Base16 encoding array, decoding is the same as encoding the Base16 encoding array </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 ();}

 

 

II,Custom Base16 decoding principle

In fact, the decoding principle is also very simple. First, split the encoded 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 explained the decoding in detail.) The following is the decoded code.

/// <Summary> /// customize Base16 decoding /// </summary> /// <param name = "str"> string to be decoded </param>/ // <param name = "autoCode"> Custom Base16 encoded array, 16 elements, which can be numbers, characters, and special characters. If not specified, use the default Base16 encoding array, decoding is the same as encoding the Base16 encoding array </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 ;}

Iii. Base16 encoding array Parsing

Finally, let's talk about this encoding array. This encoding array 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, we will show you a function of a random encoding array.

/// <Summary> /// random-encoded array /// </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 encoding and decoding I wrote is actually very simple, and the principle is also very simple. It is suitable for beginners to learn and experience. Of course, this encoding and decoding can be expanded, if you have any new ideas or ideas, please let us know. Thank you.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.