C String Processing Gadget
- 2021-12-11 08:39:20
- OfStack
I was addicted to security when I was just in college. At that time, I wanted to write a small program to deal with strings.
But there was not much time at that time, so I delayed it until this winter vacation.
There is nothing to do in winter vacation, so I will write a small program to practice my hands, and review 1 form and foundation by the way.
The following functions are implemented:
Convert to uppercase
Convert to lowercase
Inversion string
Matches the number of occurrences of a string
Regular matching
base64 Encryption
base64 Decryption
ROT13 Encryption and Decryption
MD5 32-bit encryption
The program is still very rudimentary, with no robustness and no input verification.
Create BUG with your heart
Also, please don't spit out my variable naming and method naming. If you don't learn Pinyin from primary school, you can't understand it:)
Because 1 started to do this in the blind test project.
I'm too lazy to translate
Conversion to uppercase and lowercase has its own methods
Console.WriteLine(s.ToUpper());// Convert to uppercase
Console.WriteLine(s.ToLower());// Convert to lowercase
Output reverse string
public static void fanxiang(string s)
{
char[] arrey = s.ToCharArray();
StringBuilder s1 = new StringBuilder("");
for (int i = arrey.Length - 1; i >= 0; i--)
{
s1.Append(Convert.ToString(arrey[i]));
}
Console.WriteLine(" The reverse string is {0}",s1);
}
View the number of a 1 short string in it
public static void pipei(string s)
{
int count = 0;
int i;
Console.WriteLine(" Please enter a short string ");
string s2 = Console.ReadLine();
while ((i=s.IndexOf(s2)) >= 0)
{
count++;
s = s.Substring(i + s2.Length);
}
Console.WriteLine(" Appears in the string {0} Times {1}", count, s2);
}
Regular matching
I haven't learned the knowledge of regular classes, and I read a lot on the Internet. Most of them talk about regular classes instead of regular classes. At that time, it took about one day to write this card, and now this one still has BUG.
When there is no match result, or the match is empty? Causes multiple line breaks. I also forgot how the BUG was tested at that time.
Which garden friend has an idea can say 1 time.
public static void zzpipei(string s)
{
Console.WriteLine(" Please enter a regular expression ");
string zz = Console.ReadLine();
Regex re = new Regex(zz);
string s2 = "";
if (re.IsMatch(s))
{
Console.WriteLine(" Match Successful ");
MatchCollection mc = re.Matches(s);
foreach (Match ma in mc)
{
s2 += ma.Value;
s2 += ("\r\n");
}
Console.WriteLine("1 Behavior 1 Matching results ");
Console.WriteLine(s2);
}
else
{ Console.WriteLine(" No matching result "); }
}
base64 Encryption
The method used is also self-contained, and the encryption of Chinese characters is different from that of some websites.
public static void basejiami(string s)
{
byte[] bytes = Encoding.Default.GetBytes(s);
Console.WriteLine(" String base64 Encrypt to {0}", Convert.ToBase64String(bytes));
}
base64 Decryption
public static void basejiemi(string s)
{
byte[] bytes = Convert.FromBase64String(s);
Console.WriteLine(" String base64 Decrypt to {0}", Encoding.Default.GetString(bytes));
}
ROT13 Encryption and Decryption
ROT13 is a simple permutation code. ROT13 is also a variant of Caesar encryption developed in ancient Rome in the past.
ROT13 replaces 13 bits backward, that is, A to N, B to O, and so on.
Caesar password is to replace 3 digits backward. This method can also be changed under 1 Caesar password blasting, and this method is case sensitive.
ROT13 is the reverse of itself; 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 true cryptographic security, so it should not be applied to the purpose of security. It is often used as a typical example of weak encryption.
public static void rotjm(string s)
{
string jmzf = "";// Decrypt the encrypted string
char[] arrey = s.ToCharArray();
Console.WriteLine(" String length is {0}", arrey.Length);
for (int i = 0; i < arrey.Length; i++)
{
int zfcode = (int)arrey[i];
if (zfcode >= 97 && zfcode <= 109)
zfcode = zfcode + 13;
else if (zfcode >= 110 && zfcode <= 122)
zfcode = zfcode - 13;
else if (zfcode >= 65 && zfcode <= 77)
zfcode = zfcode + 13;
else if (zfcode >= 78 && zfcode <= 90)
zfcode = zfcode - 13;
jmzf = jmzf + (char)zfcode;
}
Console.WriteLine(" The result is {0}", jmzf);
}
Replace string
public static void thzf(string s)
{
Console.WriteLine(" Please enter the string you want to replace ");
string str1 = Console.ReadLine();
Console.WriteLine(" Please enter the string you want to replace with ");
string str2 = Console.ReadLine();
Console.WriteLine(s.Replace(str1, str2));
}
32-bit MD5 encryption
public static void md5jm(string s)
{
MD5 md5 = new MD5CryptoServiceProvider();
// Encode a character into a sequence of bytes
byte[] data = System.Text.Encoding.Default.GetBytes(s);
byte[] md5data = md5.ComputeHash(data);
md5.Clear();
// Traversing cryptographic arrays , Encrypt bytes, which is 32 Bit encryption
string str = "";
for (int i = 0; i < md5data.Length; i++)
{
str += md5data[i].ToString("x").PadLeft(2, '0');
}
Console.WriteLine(" The encryption result is {0}",str);
}
My program, using. NET framework 4.0.
Download