// Encrypted string. Note that the strencrkey length is 8 bits. (to increase or decrease the key length, adjust the IV length)
Public String desencrypt (string strtext, string strencrkey)
// Decrypt the string. Note that the strencrkey length is 8 bits (to increase or decrease the key length, adjust the IV length)
Public String desdecrypt (string strtext, string sdecrkey)
// Encrypt the data file. Note that the strencrkey length is 8 bits (if you want to increase or decrease the key length, adjust the IV length)
Public void desencrypt (string m_infilepath, string m_outfilepath, string strencrkey)
// Decrypt the data file. Note that the strencrkey length is 8 bits (if you want to increase or decrease the key length, adjust the IV length)
Public void desdecrypt (string m_infilepath, string m_outfilepath, string sdecrkey)
// MD5 Encryption
Public String md5encrypt (string strtext)
*/
/*************************************** **************************************** ***********
* Cryptography class is for Cryptography
Author: Jim
E_mail: tjq_tang@hotmail.com
* Thanks:
* URI:
*
*
**************************************** **************************************** *********/
Using system;
Using system. Security. cryptography;
Using system. text;
Using system. IO;
Using system. Windows. forms;
Namespace netbee. classes. Security
{
/// <Summary>
/// Cryptography
/// </Summary>
Public class Cryptography
{
Public Cryptography ()
{
}
/// <Summary>
/// Encrypt the string
/// Attention: key must be 8 bits
/// </Summary>
/// <Param name = "strtext"> string </param>
/// <Param name = "strencrkey"> key </param>
/// <Returns> </returns>
Public String desencrypt (string strtext, string strencrkey)
{
Byte [] bykey = NULL;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Try
{
Bykey = system. Text. encoding. utf8.getbytes (strencrkey. substring (0, 8 ));
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = encoding. utf8.getbytes (strtext );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createencryptor (bykey, IV), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Return convert. tobase64string (Ms. toarray ());
}
Catch (system. Exception error)
{
MessageBox. Show (error. Message );
Return "error:" + error. Message + "/R ";
}
}
/// <Summary>
/// Decrypt string
/// Attention: key must be 8 bits
/// </Summary>
/// <Param name = "strtext"> decrypt string </param>
/// <Param name = "sdecrkey"> key </param>
/// <Returns> output string </returns>
Public String desdecrypt (string strtext, string sdecrkey)
{
Byte [] bykey = NULL;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Byte [] inputbytearray = new byte [strtext. Length];
Try
{
Bykey = system. Text. encoding. utf8.getbytes (sdecrkey. substring (0, 8 ));
Descryptoserviceprovider des = new descryptoserviceprovider ();
Inputbytearray = convert. frombase64string (strtext );
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 ();
Return encoding. getstring (Ms. toarray ());
}
Catch (system. Exception error)
{
MessageBox. Show (error. Message );
Return "error:" + error. Message + "/R ";
}
}
/// <Summary>
/// Encrypt files
/// Attention: key must be 8 bits
/// </Summary>
/// <Param name = "m_infilepath"> encrypt file path </param>
/// <Param name = "m_outfilepath"> output file </param>
/// <Param name = "strencrkey"> key </param>
Public void desencrypt (string m_infilepath, string m_outfilepath, string strencrkey)
{
Byte [] bykey = NULL;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Try
{
Bykey = system. Text. encoding. utf8.getbytes (strencrkey. substring (0, 8 ));
Filestream fin = new filestream (m_infilepath, filemode. Open, fileaccess. Read );
Filestream fout = new filestream (m_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 <totlen)
{
Len = fin. Read (bin, 0,100 );
Encstream. Write (bin, 0, Len );
Rdlen = rdlen + Len;
}
Encstream. Close ();
Fout. Close ();
Fin. Close ();
}
Catch (system. Exception error)
{
MessageBox. Show (error. Message. tostring ());
}
}
/// <Summary>
/// Decrypt files
/// Attention: key must be 8 bits
/// </Summary>
/// <Param name = "m_infilepath"> decrypt filepath </param>
/// <Param name = "m_outfilepath"> output filepath </param>
/// <Param name = "sdecrkey"> key </param>
Public void desdecrypt (string m_infilepath, string m_outfilepath, string sdecrkey)
{
Byte [] bykey = NULL;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Try
{
Bykey = system. Text. encoding. utf8.getbytes (sdecrkey. substring (0, 8 ));
Filestream fin = new filestream (m_infilepath, filemode. Open, fileaccess. Read );
Filestream fout = new filestream (m_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 <totlen)
{
Len = fin. Read (bin, 0,100 );
Encstream. Write (bin, 0, Len );
Rdlen = rdlen + Len;
}
Encstream. Close ();
Fout. Close ();
Fin. Close ();
}
Catch (system. Exception error)
{
MessageBox. Show ("error:" + error. Message );
}
}
/// <Summary>
/// MD5 Encrypt
/// </Summary>
/// <Param name = "strtext"> text </param>
/// <Returns> MD5 encrypt string </returns>
Public String md5encrypt (string strtext)
{
MD5 MD5 = new md5cryptoserviceprovider ();
Byte [] result = md5.computehash (system. Text. encoding. Default. getbytes (strtext ));
Return System. Text. encoding. Default. getstring (result );
}
}
}