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
}