C # instance code for generating the registration code

Source: Internet
Author: User

Copy codeThe Code is as follows: string t = DateTime. Now. Ticks. ToString ();

T = cipher ey. DESEncrypt (t, cipher ey. Cipher eystr );
String [] strid = new string [t. Length]; //
For (int I = 0; I <t. Length; I ++) // assign the character to the array
{
Strid [I] = t. Substring (I, 1 );
}
String s = "";
Random rdid = new Random ();
For (int I = 0; I <9; I ++) // randomly extracts characters from the array to form a new character generation machine 3
{
S + = strid [rdid. Next (0, strid. Length)];
}

Copy codeThe Code is as follows: class policey
{
Public const string shorteystr = "BLUE2013 ";

# Region DES encryption
/// <Summary>
/// DES encryption
/// </Summary>
/// <Param name = "pToEncrypt"> string to be encrypted </param>
/// <Returns> encrypted string </returns>
Public static string DESEncrypt (string pToEncrypt, string javaseystr)
{
Try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
Byte [] inputByteArray = Encoding. Default. GetBytes (pToEncrypt );
Des. Key = ASCIIEncoding. ASCII. GetBytes (DesKeyStr );
Des. IV = ASCIIEncoding. ASCII. GetBytes (DesKeyStr );
MemoryStream MS = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, des. CreateEncryptor (), CryptoStreamMode. Write );
Cs. Write (inputByteArray, 0, inputByteArray. Length );
Cs. FlushFinalBlock ();
StringBuilder ret = new StringBuilder ();
Foreach (byte B in ms. ToArray ())
{
Ret. AppendFormat ("{0: X2}", B );
}
Ret. ToString ();
Return ret. ToString ();
}
Catch
{

Return "";
}

}
# Endregion

# Region DES decryption
/// <Summary>
/// DES decryption
/// </Summary>
/// <Param name = "pToDecrypt"> encrypted string </param>
/// <Returns> decrypted string </returns>
Public static string DESDecrypt (string pToDecrypt, string encryption eystr)
{
Try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();

Byte [] inputByteArray = new byte [pToDecrypt. Length/2];
For (int x = 0; x <pToDecrypt. Length/2; x ++)
{
Int I = (Convert. ToInt32 (pToDecrypt. Substring (x * 2, 2), 16 ));
InputByteArray [x] = (byte) I;
}

Des. Key = ASCIIEncoding. ASCII. GetBytes (DesKeyStr );
Des. IV = ASCIIEncoding. ASCII. GetBytes (DesKeyStr );
MemoryStream MS = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, des. CreateDecryptor (), CryptoStreamMode. Write );
Cs. Write (inputByteArray, 0, inputByteArray. Length );
Cs. FlushFinalBlock ();

StringBuilder ret = new StringBuilder ();

Return System. Text. Encoding. Default. GetString (ms. ToArray ());
}
Catch
{

Return "";
}
}
# Endregion

}

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.