[C #] MD5 & des

Source: Internet
Author: User

Using system;
Using system. IO;
Using system. text;
Using system. Security. cryptography;
Using system. Web;
Namespace test. com
{
/// <Summary>
/// Desencryptor abstract description.
/// </Summary>
Public class desencryptor
{
# Region private member
/// <Summary>
/// Input string
/// </Summary>
Private string inputstring = NULL;
/// <Summary>
/// Output string
/// </Summary>
Private string outstring = NULL;
/// <Summary>
/// Input file path
/// </Summary>
Private string inputfilepath = NULL;
/// <Summary>
/// Output file path
/// </Summary>

Private string outfilepath = NULL;
/// <Summary>
/// Encryption key
/// </Summary>
Private string encryptkey = NULL;
/// <Summary>
/// Decrypt the key
/// </Summary>
Private string decryptkey = NULL;
/// <Summary>
/// Prompt message
/// </Summary>
Private string notemessage = NULL;
# Endregion
# Region Public attributes
/// <Summary>
/// Input string
/// </Summary>
Public String inputstring
{
Get {return inputstring ;}
Set {inputstring = value ;}
}
/// <Summary>
/// Output string
/// </Summary>
Public String outstring
{
Get {return outstring ;}
Set {outstring = value ;}
}
/// <Summary>
/// Input file path
/// </Summary>
Public String inputfilepath
{
Get {return inputfilepath ;}
Set {inputfilepath = value ;}
}
/// <Summary>
/// Output file path
/// </Summary>
Public String outfilepath
{
Get {return outfilepath ;}
Set {outfilepath = value ;}
}
/// <Summary>
/// Encryption key
/// </Summary>
Public String encryptkey
{
Get {return encryptkey ;}
Set {encryptkey = value ;}
}
/// <Summary>
/// Decrypt the key
/// </Summary>
Public String decryptkey
{
Get {return decryptkey ;}
Set {decryptkey = value ;}
}
/// <Summary>
/// Error message
/// </Summary>
Public String notemessage
{
Get {return notemessage ;}
Set {notemessage = value ;}
}
# Endregion
# Region Constructor
Public desencryptor ()
{
//
// Todo: add the constructor logic here
//
}
# Endregion
# Region DES encrypted string
/// <Summary>
/// Encrypted string
/// Note: The key must be 8 bits.
/// </Summary>
/// <Param name = "strtext"> string </param>
/// <Param name = "encryptkey"> key </param>
Public void desencrypt ()
{
Byte [] bykey = NULL;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Try
{
Bykey = system. Text. encoding. utf8.getbytes (this. encryptkey. substring (0, 8 ));
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = encoding. utf8.getbytes (this. inputstring );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createencryptor (bykey, IV), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
This. outstring = convert. tobase64string (Ms. toarray ());
}
Catch (system. Exception error)
{
This. notemessage = Error. message;
}
}
# Endregion
# Region des decryption string
/// <Summary>
/// Decrypt the string
/// </Summary>
/// <Param name = "This. inputstring"> encrypted string </param>
/// <Param name = "decryptkey"> key </param> professional 3 s website 3s8.cn
Public void desdecrypt ()
{
Byte [] bykey = NULL;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Byte [] inputbytearray = new byte [This. inputstring. Length];
Try
{
Bykey = system. Text. encoding. utf8.getbytes (decryptkey. substring (0, 8 ));
Descryptoserviceprovider des = new descryptoserviceprovider ();
Inputbytearray = convert. frombase64string (this. inputstring );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (bykey, IV), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
System. Text. Encoding encoding = new system. Text. utf8encoding ();
This. outstring = encoding. getstring (Ms. toarray ());
}
Catch (system. Exception error)
{
This. notemessage = Error. message;
}
}
# Endregion
# Region DES encrypted file
/// <Summary>
/// DES encrypted file
/// </Summary>
/// <Param name = "This. inputfilepath"> source file path </param>
/// <Param name = "This. outfilepath"> output file path </param>
/// <Param name = "encryptkey"> key </param>
Public void filedesencrypt ()
{
Byte [] bykey = NULL;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Try
{
Bykey = system. Text. encoding. utf8.getbytes (this. encryptkey. substring (0, 8 ));
Filestream fin = new filestream (this. inputfilepath, filemode. Open, fileaccess. Read );
Filestream fout = new filestream (this. outfilepath, 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 (bykey, IV), cryptostreammode. Write );

// read from the input file, then encrypt and write to the output file.
while (rdlen {< br> Len = fin. reads (bin, 0,100);
encstream. write (bin, 0, Len);
rdlen = rdlen + Len;
}

Encstream. Close ();
Fout. Close ();
Fin. Close ();

}
Catch (system. Exception error)
{
This. notemessage = Error. Message. tostring ();

}
}
# Endregion
# Region des file decryption
/// <Summary>
/// Decrypt the file
/// </Summary>
/// <Param name = "This. inputfilepath"> encrypted file path </param>
/// <Param name = "This. outfilepath"> output file path </param>
/// <Param name = "decryptkey"> key </param>
Public void filedesdecrypt ()
{
Byte [] bykey = NULL;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Try
{
Bykey = system. Text. encoding. utf8.getbytes (decryptkey. substring (0, 8 ));
Filestream fin = new filestream (this. inputfilepath, filemode. Open, fileaccess. Read );
Filestream fout = new filestream (this. outfilepath, 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 (bykey, IV), cryptostreammode. Write );

// read from the input file, then encrypt and write to the output file.
while (rdlen {< br> Len = fin. reads (bin, 0,100);
encstream. write (bin, 0, Len);
rdlen = rdlen + Len;
}

Encstream. Close ();
Fout. Close ();
Fin. Close ();
}
Catch (system. Exception error)
{
This. notemessage = Error. Message. tostring ();
}
}
# Endregion
# Region MD5
/// <Summary>
/// MD5 Encrypt
/// </Summary>
/// <Param name = "strtext"> text </param>
/// <Returns> MD5 encrypt string </returns>
Public void md5encrypt ()
{
MD5 MD5 = new md5cryptoserviceprovider ();
Byte [] result = md5.computehash (system. Text. encoding. Default. getbytes (this. inputstring ));
This. outstring = system. Text. encoding. Default. getstring (result );
}
# Endregion

}
}

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.