C # reversible and irreversible MD5 encryption,

Source: Internet
Author: User

C # reversible and irreversible MD5 encryption,

1. method 1 (irreversible encryption) srxljl

Public string EncryptPassword (string PasswordString, string PasswordFormat)
{
String encryptPassword = null;
If (PasswordFormat = "SHA1 "){
EncryptPassword = FormsAuthortication. HashPasswordForStoringInConfigFile (PasswordString, "SHA1 ");
}
Elseif (PasswordFormat = "MD5 ")
{

EncryptPassword = FormsAuthortication. HashPasswordForStoringInConfigFile (PasswordString, "MD5 ");
}
Return encryptPassword;
}

 

2. method 2 (reversible encryption) srxljl

 

Public interface IBindesh
{
String encode (string str );
String decode (string str );
}

Public class EncryptionDecryption: IBindesh
{
Public string encode (string str)
{
String htext = "";

For (int I = 0; I <str. Length; I ++)
{
Htext = htext + (char) (str [I] + 10-1*2 );
}
Return htext;
}

Public string decode (string str)
{
String dtext = "";

For (int I = 0; I <str. Length; I ++)
{
Dtext = dtext + (char) (str [I]-10 + 1*2 );
}
Return dtext;
}

 

3. Method 3 (reversible encryption) srxljl

 


Const string KEY_64 = "VavicApp"; // note that it is 8 characters long and 64-bit

Const string IV_64 = "VavicApp ";
Public string Encode (string data)
{
Byte [] byKey = System. Text. ASCIIEncoding. ASCII. GetBytes (KEY_64 );
Byte [] byIV = System. Text. ASCIIEncoding. ASCII. GetBytes (IV_64 );

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider ();
Int I = cryptoProvider. KeySize;
MemoryStream MS = new MemoryStream ();
CryptoStream cst = new CryptoStream (MS, cryptoProvider. CreateEncryptor (byKey, byIV), CryptoStreamMode. Write );

StreamWriter sw = new StreamWriter (cst );
Sw. Write (data );
Sw. Flush ();
Cst. FlushFinalBlock ();
Sw. Flush ();
Return Convert. ToBase64String (ms. GetBuffer (), 0, (int) ms. Length );

}

Public string Decode (string data)
{
Byte [] byKey = System. Text. ASCIIEncoding. ASCII. GetBytes (KEY_64 );
Byte [] byIV = System. Text. ASCIIEncoding. ASCII. GetBytes (IV_64 );

Byte [] byEnc;
Try
{
ByEnc = Convert. FromBase64String (data );
}
Catch
{
Return null;
}

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider ();
MemoryStream MS = new MemoryStream (byEnc );
CryptoStream cst = new CryptoStream (MS, cryptoProvider. CreateDecryptor (byKey, byIV), CryptoStreamMode. Read );
StreamReader sr = new StreamReader (cst );
Return sr. ReadToEnd ();
}

 

4. md5 (32-bit encryption) srxljl

Public string GetMD5 (string s, string _ input_charset)
{

/// <Summary>
/// ASP-compatible MD5 Encryption Algorithm
/// </Summary>

MD5 md5 = new MD5CryptoServiceProvider ();
Byte [] t = md5.ComputeHash (Encoding. GetEncoding (_ input_charset). GetBytes (s ));
StringBuilder sb = new StringBuilder (32 );
For (int I = 0; I <t. Length; I ++)
{
Sb. Append (t [I]. ToString ("x"). PadLeft (2, '0 '));
}
Return sb. ToString ();
}

(16-bit encryption) srxljl

 

Public static string GetMd5Str (string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider ();
String t2 = BitConverter. ToString (md5.ComputeHash (UTF8Encoding. Default. GetBytes (ConvertString), 4, 8 );
T2 = t2.Replace ("-","");
Return t2;
}

 

5. Add and unbind the text file srxljl

 

// Encrypt the file
Private static void EncryptData (String inName, String outName, byte [] cipher ey, byte [] desIV)
{
// Create the file streams to handle the input and output files.
FileStream fin = new FileStream (inName, FileMode. Open, FileAccess. Read );
FileStream fout = new FileStream (outName, FileMode. OpenOrCreate, FileAccess. Write );
Fout. SetLength (0 );

// Create variables to help with read and write.
Byte [] bin = new byte [100]; // This is intermediate storage for the encryption.
Long rdlen = 0; // This is the total number of bytes written.
Long totlen = fin. Length; // This is the total length of the input file.
Int len; // This is the number of bytes to be written at a time.

DES des = new DESCryptoServiceProvider ();
CryptoStream encStream = new CryptoStream (fout, des. CreateEncryptor (Cipher ey, desIV), CryptoStreamMode. Write );

// Read from the input file, then encrypt and write to the output file.
While (rdlen <totlen)
{
Len = fin. Read (bin, 0,100 );
EncStream. Write (bin, 0, len );
Rdlen = rdlen + len;
}

EncStream. Close ();
Fout. Close ();
Fin. Close ();
}

// Decrypt the file
Private static void DecryptData (String inName, String outName, byte [] cipher ey, byte [] desIV)
{
// Create the file streams to handle the input and output files.
FileStream fin = new FileStream (inName, FileMode. Open, FileAccess. Read );
FileStream fout = new FileStream (outName, FileMode. OpenOrCreate, FileAccess. Write );
Fout. SetLength (0 );

// Create variables to help with read and write.
Byte [] bin = new byte [100]; // This is intermediate storage for the encryption.
Long rdlen = 0; // This is the total number of bytes written.
Long totlen = fin. Length; // This is the total length of the input file.
Int len; // This is the number of bytes to be written at a time.

DES des = new DESCryptoServiceProvider ();
CryptoStream encStream = new CryptoStream (fout, des. CreateDecryptor (Cipher ey, desIV), CryptoStreamMode. Write );

// Read from the input file, then encrypt and write to the output file.
While (rdlen <totlen)
{
Len = fin. Read (bin, 0,100 );
EncStream. Write (bin, 0, len );
Rdlen = rdlen + len;
}

EncStream. Close ();
Fout. Close ();
Fin. Close ();
}


In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

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.