C # Common operations reserved

Source: Internet
Author: User
Tags md5 encryption

How random increases the randomness of generating random numbers

An online exam system project that needs to be randomly drawn from the question bank, but if the direct random ran=new Randon (), ran. The method of Next (Nummin,nummax);

In the case of system execution too fast, resulting in only a series of continuous problems, the other problems are not taken, using the following methods to improve the number of random seeds

using the random function of C # to generate randomly, you will find that the result is always a value. The reason is that the random function requires a seed, which is specified by random (i), and the system automatically uses the current system time as a seed if the seed is not explicitly specified. If the program executes too quickly, it will result in a seed-like situation, when the random number generated is the same value. The solution is as follows: The random class is a class that generates pseudo-random numbers, with two constructors, one directly new random (), and the other a new random (INT32), which generates a random number based on the system time that triggered the tick. The latter can set their own trigger seed, usually with uncheck ((INT) DateTime.Now.Ticks) as the parameter seed, so if the computer is running fast, if the trigger Randm function interval is short, it is possible to produce the same random number, Because pseudo-random numbers, in the internal generation mechanism of random still have a certain regularity, is not the true meaning of completely random. Random fast continuous generation of the same random number solution:1, and the method of delay. You can use a For loop approach, or you can use Thread.Sleep (100);2, the seed generation method of increasing random number non-repetition probability: staticintgetrandomseed () {byte[] bytes =New byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider rng=NewSystem.Security.Cryptography.RNGCryptoServiceProvider (); rng. GetBytes (bytes);returnBitconverter.toint32 (Bytes, 0 );} Random Random=NewRandom (Getrandomseed ()); Reference: http://blog.sina.com.cn/s/blog_67f16f3d0100xsdd.html
random increase of seed stochastic rate

Conversion of C # between different binaries

//Decimal Turn binaryConsole.WriteLine (Convert.ToString (69, 2)); //Decimal Turn octalConsole.WriteLine (Convert.ToString (69, 8)); //Decimal Turn hexConsole.WriteLine (Convert.ToString (69, 16)); //Binary goto DecimalConsole.WriteLine (Convert.ToInt32 ("100111101″, 2)); //Octal goto DecimalConsole.WriteLine (Convert.ToInt32 ("76″, 8)); //C # 16 binary conversion 10 binaryConsole.WriteLine (Convert.ToInt32 ("FF", 16)); Binary conversion
View Code

MD5 encryption

 ///<summary>        ///32-bit MD5 encryption        ///</summary>        ///<param name= "str" ></param>        ///<returns></returns>Public static string getmd5_32 (String str) {byte[] arrhashinput; byte[] arrhashoutput; //MD5CryptoServiceProvider objMD5 = new MD5CryptoServiceProvider ();MD5 objMD5 =NewMD5CryptoServiceProvider (); Arrhashinput=c2b (str); Arrhashoutput=Objmd5.computehash (arrhashinput); returnbitconverter.tostring (arrhashoutput); } protected Staticbyte[] C2B (String str) {Char[] Arrchar; Arrchar=Str.            ToCharArray (); byte[] Arrbyte =New byte[Arrchar.length];  for(inti = 0; i < arrchar.length; i++) {Arrbyte[i]=Convert.tobyte (Arrchar[i]); }            returnArrbyte; }
32-bit MD5 encryption
///<summary>        ///16-bit MD5 encryption        ///</summary>        ///<param name= "sinputstring" ></param>        ///<returns></returns>Public static String getmd5_16 (String sinputstring) {byte[] data =Encoding.UTF8.GetBytes (sinputstring); DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); String SKey=GenerateKey (); Des. Key=ASCIIEncoding.ASCII.GetBytes (SKey); Des.iv=ASCIIEncoding.ASCII.GetBytes (SKey); ICryptoTransform desencrypt=DES.            CreateEncryptor (); byte[] result = Desencrypt. TransformFinalBlock (data, 0, data.            Length); returnbitconverter.tostring (Result); }        //Create keyPublic static String GenerateKey () {DESCryptoServiceProvider descrypto=(DESCryptoServiceProvider) descryptoserviceprovider.create (); returnASCIIEncoding.ASCII.GetString (Descrypto.key); }        //Encrypt stringPublic static String encryptstring (String sinputstring, String sKey) {byte[] data =Encoding.UTF8.GetBytes (sinputstring); DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=ASCIIEncoding.ASCII.GetBytes (SKey); Des.iv=ASCIIEncoding.ASCII.GetBytes (SKey); ICryptoTransform desencrypt=DES.            CreateEncryptor (); byte[] result = Desencrypt. TransformFinalBlock (data, 0, data.            Length); returnbitconverter.tostring (Result); }        //decrypting a stringPublic static String decryptstring (String sinputstring, String sKey) {string[] sInput= Sinputstring.split ("-".            ToCharArray ()); byte[] data =New byte[Sinput.length];  for(inti = 0; i < sinput.length; i++) {Data[i]=byte.            Parse (Sinput[i], numberstyles.hexnumber); } DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=ASCIIEncoding.ASCII.GetBytes (SKey); Des.iv=ASCIIEncoding.ASCII.GetBytes (SKey); ICryptoTransform desencrypt=DES.            CreateDecryptor (); byte[] result = Desencrypt. TransformFinalBlock (data, 0, data.            Length); returnEncoding.UTF8.GetString (Result); }
16-bit MD5 encryption

C # Common operations reserved

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.