The following are the encryption and decryption methods:
Same plain text, different passwords are generated each time,
PS: it is best not to use the encrypted ciphertext in the address bar of a webpage to pass the value, because the encrypted ciphertext may contain special characters such? #. The REQUEST may be truncated when another page is uploaded. As a result, the decrypted plaintext is different from that before encryption.
/// <Summary>
/// Encryption and decryption method
/// </Summary>
/// <Param name = "sSource"> operation string </param>
/// <Param name = "iFlag"> Operation Type 1: encryption rather than 1: decryption </param>
/// <Returns> encrypted or decrypted string </returns>
Public string pWord (string sSource, int iFlag)
{
If (sSource = null)
Return null;
If (sSource. Equals (""))
Return "";
Int li_len, I, li_asc, li_rand, li_head;
String ls_ I, ls_code = "";
If (iFlag = 1)
{
Li_len = sSource. Length;
Random rdm1 = new Random (~ Unchecked (int) DateTime. Now. Ticks ));
Li_head = (int) (rdm1.NextDouble () * 10 );
If (li_head = 0)
Li_head = 1;
For (I = 0; I <li_len; I ++)
{
Random rdm2 = new Random (~ Unchecked (int) DateTime. Now. Ticks ));
Int rand2 = (int) (rdm2.NextDouble () * 94 );
If (rand2 = 0)
Rand2 = 1;
Li_rand = rand2 + 32;
Li_asc = Convert. ToInt32 (sSource. ToCharArray (I, 1) [0]);
Ls_ I = (char) (li_asc-I). ToString ();
If (li_asc + I + li_head> 126) // The model where the ASCI Code exceeds 126. For example, Chinese occupies two bytes.
{
If (li_rand % 2 = 1)
Li_rand = li_rand + 1;
Ls_ I = (char) (li_rand). ToString () + (char) (li_asc-I-li_head). ToString ();
}
Else // encrypted is a letter
{
If (li_rand % 2 = 0)
Li_rand = li_rand + 1;
Ls_ I = (char) (li_rand). ToString () + (char) (li_asc + I + li_head). ToString ();
}
Ls_code = ls_code + ls_ I;
}
Random rdm3 = new Random (~ Unchecked (int) DateTime. Now. Ticks ));
Int rand1 = (int) (rdm3.NextDouble () * 9 );
If (rand1 = 0)
Rand1 = 1;
Ls_code = (char) (rand1*10 + li_head + 40). ToString () + ls_code;
}
Else
{
Int li_ret;
Li_len = sSource. Length;
Ls_code = "";
Li_ret = Convert. ToInt32 (sSource. ToCharArray (0, 1) [0]) % 10;
For (I = 2; I <li_len; I = I + 2)
{
Li_asc = Convert. ToInt32 (sSource. ToCharArray (I, 1) [0]);
If (Convert. ToInt32 (sSource. ToCharArray (I-1,1) [0]) % 2 = 0)
{
Ls_ I = (char) (li_asc + (I-1)/2 + li_ret). ToString ();
}
Else
{
Ls_ I = (char) (li_asc-(I-1)/2-li_ret). ToString ();
}
Ls_code = ls_code + ls_ I;
}
}
Return ls_code;
}