C # string processing tool,

Source: Internet
Author: User

C # string processing tool,

I was addicted to security when I first went to college, and I was always trying to write a small program to process strings.

However, there was not much time to wait until the winter vacation.

During the winter vacation, I had nothing to do, so I wrote a small program to train my hands. By the way, I would like to review the forms and basics.

 

The following functions are implemented:

 

Convert to uppercase

Convert to lowercase

Reverse string

Number of times a string is matched

Regular Expression matching

Base64 Encryption

Base64 decryption

ROT13 encryption and decryption

MD5 32-bit encryption

 

The program is still very simple, with no robustness or input validation.

Carefully create bugs

Also, please do not mention the names of variables and Methods. If you have not learned pinyin since elementary school, you will certainly not understand it :)

This was done in the blind test project at the beginning.

I am too lazy to translate

 

 

There are built-in methods for converting to uppercase and lowercase letters.

1 Console. WriteLine (s. ToUpper (); // converts it to uppercase. 2 Console. WriteLine (s. ToLower (); // converts it to lowercase.

 


Output reverse string

1 public static void fanxiang (string s) 2 {3 char [] arrey = s. toCharArray (); 4 StringBuilder s1 = new StringBuilder (""); 5 for (int I = arrey. length-1; I> = 0; I --) 6 {7 s1.Append (Convert. toString (arrey [I]); 8} 9 Console. writeLine ("reverse string: {0}", s1); 10}

 

View the number of short strings

1 public static void pipei (string s) 2 {3 int count = 0; 4 int I; 5 Console. writeLine ("enter a short string"); 6 string s2 = Console. readLine (); 7 while (I = s. indexOf (s2)> = 0) 8 {9 count ++; 10 s = s. substring (I + s2.Length); 11} 12 Console. writeLine ("{0} Times in string {1}", count, s2); 13}

 

Regular Expression matching

I have never learned about regular expressions. I have read many online articles about regular expressions instead of regular expressions. At that time, I wrote this for about a day, and now there is still a BUG.

When no matching result exists, or is the matching null? Multiple line breaks may occur. I also forgot how the BUG was tested.

Which garden friends have the idea to talk about.

1 public static void zzpipei (string s) 2 {3 Console. writeLine ("enter regular expression"); 4 string zz = Console. readLine (); 5 Regex re = new Regex (zz); 6 string s2 = ""; 7 if (re. isMatch (s) 8 {9 Console. writeLine ("matched"); 10 MatchCollection mc = re. matches (s); 11 foreach (Match ma in mc) 12 {13 s2 + = ma. value; 14 s2 + = ("\ r \ n"); 15} 16 Console. writeLine ("one row is a matching result"); 17 Console. writeLine (s2); 18} 19 else20 {Console. writeLine ("no matching result");} 21}


Base64 Encryption

The method is also self-built, and the encryption of Chinese characters is different from that of some websites.

1 public static void basejiami (string s) 2 {3 byte [] bytes = Encoding. default. getBytes (s); 4 Console. writeLine ("string base64 encrypted to {0}", Convert. toBase64String (bytes); 5}


Base64 decryption

1 public static void basejiemi (string s) 2 {3 byte [] bytes = Convert. fromBase64String (s); 4 Console. writeLine ("string base64 decryption to {0}", Encoding. default. getString (bytes); 5}


ROT13 encryption and decryption

ROT13 is a simple replacement code. ROT13 is also a variant of Caesar encryption developed in ancient Rome.

ROT13 is to replace 13 digits backward, that is, A is converted to N, B is converted to O, and so on.

The Caesar password is replaced by three digits. This method can be used to crack the password of Caesar, and the method is case sensitive.

ROT13 is its own inverse. That is to say, to restore ROT13, apply the same encryption algorithm, so the same operation can be re-encrypted and decrypted.

This algorithm does not provide real cryptography preservation, so it should not be applied to the purpose of preservation. It is often used as a typical example of weak encryption.

1 public static void rotjm (string s) 2 {3 4 string jmzf = ""; // decrypted encrypted string 5 6 char [] arrey = s. toCharArray (); 7 Console. writeLine ("String Length: {0}", arrey. length); 8 for (int I = 0; I <arrey. length; I ++) 9 {10 int zfcode = (int) arrey [I]; 11 12 if (zfcode> = 97 & zfcode <= 109) 13 zfcode = zfcode + 13; 14 else if (zfcode >=110 & zfcode <= 122) 15 zfcode = zfcode-13; 16 else if (zfcode >=65 & zfcode <= 77) 17 zfcode = zfcode + 13; 18 else if (zfcode >=78 & zfcode <= 90) 19 zfcode = zfcode-13; 20 21 22 jmzf = jmzf + (char) zfcode; 23} 24 Console. writeLine ("Result: {0}", jmzf); 25}


Replace string

1 public static void thzf (string s) 2 {3 Console. writeLine ("Enter the string to be replaced"); 4 string str1 = Console. readLine (); 5 Console. writeLine ("Enter the string you want to replace"); 6 string str2 = Console. readLine (); 7 Console. writeLine (s. replace (str1, str2); 8}


32-bit MD5 Encryption

1 public static void md5jm (string s) 2 {3 MD5 md5 = new MD5CryptoServiceProvider (); 4 // encode the character into a byte sequence of 5 bytes [] data = System. text. encoding. default. getBytes (s); 6 byte [] md5data = md5.ComputeHash (data); 7 md5.Clear (); 8 // traverses the encrypted array, encrypted bytes, this method is a 32-bit encrypted 9 string str = ""; 10 for (int I = 0; I <md5data. length; I ++) 11 {12 str + = md5data [I]. toString ("x "). padLeft (2, '0'); 13} 14 Console. writeLine ("encrypted result: {0}", str); 15}

 

Use. NET framework 4.0 in my program.

Password: wMfxNo

Link: https://share.weiyun.com/f43946c18309caa80e5b536031cdf90a

 

Reprinted please contact

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.