Web string encryption and decryption Methods

Source: Internet
Author: User

Using System;
Using System. Text;
Using System. Security;
Using System. Web. Security;

Namespace StringOperation
{
/// <Summary>
/// Summary of StringOperation.
/// </Summary>
Public class StringOperation
{
Public StringOperation ()
{
//
// TODO: add the constructor logic here
//
}
/// <Summary>
/// Convert special characters
/// </Summary>
/// <Param name = "str"> characters to be converted </param>
/// <Returns> </returns>
Public static string Encode (string str)
{
Str = str. Replace ("&", "& amp ;");
Str = str. Replace ("'","''");
Str = str. Replace ("\" "," & quot ;");
Str = str. Replace ("", "& nbsp ;");
Str = str. Replace ("<", "& lt ;");
Str = str. Replace (">", "& gt ;");
Str = str. Replace ("\ n", "<br> ");
Return str;
}

/// <Summary>
/// Decode
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> </returns>
Public static string Decode (string str)
{
Str = str. Replace ("<br>", "\ n ");
Str = str. Replace ("& gt;", "> ");
Str = str. Replace ("& lt;", "<");
Str = str. Replace ("& nbsp ;","");
Str = str. Replace ("& quot ;","\"");
Return str;
}

/// <Summary>
/// Encrypting string
/// </Summary>
/// <Returns> </returns>
Public static string Encrypt (string Password)
{
String str = "";
FormsAuthenticationTicket ticket = new System. Web. Security. FormsAuthenticationTicket (Password, true, 2 );
Str = FormsAuthentication. Encrypt (ticket). ToString ();
Return str;
}

/// <Summary>
/// Encrypting string
/// </Summary>
/// <Param name = "Password"> encrypting string </param>
/// <Param name = "Format"> format, 0 is SHA1, 1 is MD5 </param>
/// <Returns> </returns>
Public static string Encrypt (string Password, int Format)
{
String str = "";
Switch (Format)
{
Case 0:
Str = FormsAuthentication. HashPasswordForStoringInConfigFile (Password, "SHA1 ");
Break;
Case 1:
Str = FormsAuthentication. HashPasswordForStoringInConfigFile (Password, "MD5 ");
Break;
}
Return str;
}

/// <Summary>
/// Decrypt string
/// </Summary>
/// <Param name = "Passowrd"> encrypted string </param>
/// <Returns> </returns>
Public static string Decrypt (string Passowrd)
{
String str = "";
Str = FormsAuthentication. Decrypt (Passowrd). Name. ToString ();
Return str;
}

/// <Summary>
/// Encrypt Cookie
/// </Summary>
/// <Param name = "str"> Target cookie string </param>
/// <Returns> </returns>
Public static string EncryptCookie (string strCookie, int type)
{
String str = En (strCookie, type );
StringBuilder sb = new StringBuilder ();
Foreach (char a in str)
{
Sb. Append (Convert. ToString (a, 16). PadLeft (4, '0 '));
}
Return sb. ToString ();
}

/// <Summary>
/// Decrypt Cookie
/// </Summary>
/// <Param name = "str"> Target cookie string </param>
/// <Returns> </returns>
Public static string DecryptCookie (string strCookie, int type)
{
StringBuilder sb = new StringBuilder ();
String [] strarr = new String [255];
Int I, j, count = strCookie. Length/4;
String strTmp;

For (I = 0; I <count; I ++)
{
For (j = 0; j <4; j ++)
{
Sb. Append (strCookie. Substring (I * 4 + j, 1 ));
}
Strarr [I] = sb. ToString ();
Sb. Remove (0, sb. Length );
}

For (I = 0; I <count; I ++)
{
StrTmp = uint. Parse (strarr [I], System. Globalization. NumberStyles. AllowHexSpecifier). ToString ("D ");
Char ch = (char) uint. Parse (strTmp );
Sb. Append (ch );
}

Return De (sb. ToString (), type );
}

Private static string En (string strCookie, int type)
{
String str;
If (type % 2 = 0)
{
Str = Transform1 (strCookie );
}
Else
{
Str = Transform3 (strCookie );
}

Str = Transform2 (strCookie );
Return str;
}

Private static string De (string strCookie, int type)
{
String str;
If (type % 2 = 0)
{
Str = DeTransform1 (strCookie );
}
Else
{
Str = DeTransform3 (strCookie );
}

Str = Transform2 (strCookie );
Return str;
}

/// <Summary>
/// Methods 1
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> decrypted string </returns>
Public static string DeTransform1 (string str)
{
Int I = 0;
StringBuilder sb = new StringBuilder ();

Foreach (char a in str)
{
Switch (I % 6)
{
Case 0:
Sb. Append (char) (A-1 ));
Break;
Case 1:
Sb. Append (char) (A-5 ));
Break;
Case 2:
Sb. Append (char) (A-7 ));
Break;
Case 3:
Sb. Append (char) (A-2 ));
Break;
Case 4:
Sb. Append (char) (A-4 ));
Break;
Case 5:
Sb. Append (char) (a-9 ));
Break;
}
I ++;
}

Return sb. ToString ();
}

/// <Summary>
/// Method1
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> encrypted string </returns>
Public static string Transform1 (string str)
{
Int I = 0;
StringBuilder sb = new StringBuilder ();

Foreach (char a in str)
{
Switch (I % 6)
{
Case 0:
Sb. Append (char) (a + 1 ));
Break;
Case 1:
Sb. Append (char) (a + 5 ));
Break;
Case 2:
Sb. Append (char) (a + 7 ));
Break;
Case 3:
Sb. Append (char) (a + 2 ));
Break;
Case 4:
Sb. Append (char) (a + 4 ));
Break;
Case 5:
Sb. Append (char) (a + 9 ));
Break;
}
I ++;
}

Return sb. ToString ();
}

/// <Summary>
/// Method1
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> encrypted string </returns>
Public static string Transform2 (string str)
{
Uint j = 0;
StringBuilder sb = new StringBuilder ();

Str = Reverse (str );
Foreach (char a in str)
{
J =;
If (j> 255)
{
J = (uint) (a> 8) + (a & 0x0ff) <8 ));
}
Else
{
J = (uint) (a> 4) + (a & 0x0f) <4 ));
}
Sb. Append (char) j );
}

Return sb. ToString ();
}

/// <Summary>
/// Methods 1
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> decrypted string </returns>
Public static string DeTransform3 (string str)
{
Int I = 0;
StringBuilder sb = new StringBuilder ();

Foreach (char a in str)
{
Switch (I % 6)
{
Case 0:
Sb. Append (char) (A-3 ));
Break;
Case 1:
Sb. Append (char) (A-6 ));
Break;
Case 2:
Sb. Append (char) (A-8 ));
Break;
Case 3:
Sb. Append (char) (A-7 ));
Break;
Case 4:
Sb. Append (char) (A-5 ));
Break;
Case 5:
Sb. Append (char) (A-2 ));
Break;
}
I ++;
}

Return sb. ToString ();
}

/// <Summary>
/// Method1
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> encrypted string </returns>
Public static string Transform3 (string str)
{
Int I = 0;
StringBuilder sb = new StringBuilder ();

Foreach (char a in str)
{
Switch (I % 6)
{
Case 0:
Sb. Append (char) (a + 3 ));
Break;
Case 1:
Sb. Append (char) (a + 6 ));
Break;
Case 2:
Sb. Append (char) (a + 8 ));
Break;
Case 3:
Sb. Append (char) (a + 7 ));
Break;
Case 4:
Sb. Append (char) (a + 5 ));
Break;
Case 5:
Sb. Append (char) (a + 2 ));
Break;
}
I ++;
}

Return sb. ToString ();
}

/// <Summary>
/// Reverse display the specified character
/// </Summary>
/// <Param name = "str"> characters to be reversed </param>
/// <Returns> reverse character </returns>
Public static string Reverse (string str)
{
Int I;
StringBuilder sb = new StringBuilder ();

For (I = str. Length-1; I> = 0; I --)
{
Sb. Append (str [I]);
}

Return sb. ToString ();
}
}
}

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.