C # reversible and irreversible MD5 Encryption

Source: Internet
Author: User
Tags md5 encryption sha1

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 = 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 = 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 ();
}

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.