Asp. The application of net encryption technology

Source: Internet
Author: User
Tags bool decrypt hash md5 md5 encryption
asp.net| encryption

Encrypt class Code


/**//**//**//**********************created by chen**************************

* If you feel that my article is good, to quote please respect the work of the authors of the fruits of labor, explain

* Source and original creator, Thank you!!! Email:aishen944-sohu.com

*******************************************************************/

Using System;
Using System.Text;
Using System.Security;
Using System.Security.Cryptography;
Using System.IO;
Namespace Encryptclasses
... {
/**//**//**////<summary>
Des encryption is defined here, in order to facilitate future management and maintenance
Please do not arbitrarily change the password, or change the password, please be sure to
Keep the previous password in mind, or it will be an unexpected loss.
</summary>
public class Desencrypt
... {
' Member Fields ' ' Member Fields ' #region ' member Fields '
private string iv= "12345678";
private string key= "12345678";
Private Encoding encoding=new unicodeencoding ();
Private Des des;
#endregion
/**//**//**////<summary>
Constructors
</summary>
Public Desencrypt ()
... {
Des=new DESCryptoServiceProvider ();
}
"Propertys" "Propertys" #region "Propertys"
/**//**//**////<summary>
Set Encryption key
</summary>
public string Encryptkey
... {
Get ... {return this.key;}
Set
... {
This.key=value;
}
}
/**//**//**////<summary>
Encoding mode to encrypt characters
</summary>
Public Encoding Encodingmode
... {
Get ... {return this.encoding;}
Set ... {This.encoding=value;}
}
#endregion
"Methods" "Methods" #region "Methods"
/**//**//**////<summary>
Encrypts the string and returns the result of the encryption
</summary>
<param name= "str" ></param>
<returns></returns>
public string encryptstring (String str)
... {
Byte[] Ivb=encoding.ascii.getbytes (THIS.IV);
Byte[] Keyb=encoding.ascii.getbytes (this. Encryptkey);/Get Encryption Key
Byte[] Toencrypt=this. Encodingmode.getbytes (str);//Get the content to be encrypted
Byte[] encrypted;
ICryptoTransform encryptor=des. CreateEncryptor (KEYB,IVB);
MemoryStream msencrypt=new MemoryStream ();
CryptoStream csencrypt=new CryptoStream (msencrypt,encryptor,cryptostreammode.write);
Csencrypt.write (toencrypt,0,toencrypt.length);
Csencrypt.flushfinalblock ();
Encrypted=msencrypt.toarray ();
Csencrypt.close ();
Msencrypt.close ();
return this. Encodingmode.getstring (encrypted);
}
/**//**//**////<summary>
Encrypts the specified file, or False if it returns true successfully
</summary>
<param name= "FilePath" > File path to encrypt </param>
<param name= "Outpath" > Encrypted file Output path </param>
public void EncryptFile (String filepath,string outpath)
... {
BOOL Isexist=file.exists (FilePath);
if (isexist)//if present
... {
Byte[] Ivb=encoding.ascii.getbytes (THIS.IV);
Byte[] Keyb=encoding.ascii.getbytes (this. Encryptkey);
Get the byte stream to encrypt the file
FileStream fin=new FileStream (filepath,filemode.open,fileaccess.read);
StreamReader reader=new StreamReader (fin,this. Encodingmode);
String Datastr=reader. ReadToEnd ();
Byte[] Toencrypt=this. Encodingmode.getbytes (DATASTR);
Fin. Close ();

FileStream fout=new FileStream (outpath,filemode.create,fileaccess.write);
ICryptoTransform encryptor=des. CreateEncryptor (KEYB,IVB);
CryptoStream csencrypt=new CryptoStream (fout,encryptor,cryptostreammode.write);
Try
... {
Encrypted file byte stream
Csencrypt.write (toencrypt,0,toencrypt.length);
Csencrypt.flushfinalblock ();
}
catch (Exception err)
... {
throw new ApplicationException (err. message);
}
Finally
... {
Try
... {
Fout. Close ();
Csencrypt.close ();
}
Catch
... {
;
}
}
}
Else
... {
throw new FileNotFoundException ("No specified file found");
}
}
/**//**//**////<summary>
Overloaded version of the file encryption function, if you do not specify an output path,
Then the original file will be overwritten by the encrypted file
</summary>
<param name= "FilePath" ></param>
public void EncryptFile (string filePath)
... {
This. EncryptFile (Filepath,filepath);
}
/**//**//**////<summary>
Decrypt the given string
</summary>
<param name= "str" > Characters to Decrypt </param>
<returns></returns>
public string decryptstring (String str)
... {
Byte[] Ivb=encoding.ascii.getbytes (THIS.IV);
Byte[] Keyb=encoding.ascii.getbytes (this. Encryptkey);
Byte[] Todecrypt=this. Encodingmode.getbytes (str);
Byte[] Decrypted=new byte[todecrypt.length];
ICryptoTransform decryptor=des. CreateDecryptor (KEYB,IVB);
MemoryStream msdecrypt=new MemoryStream (todecrypt);
CryptoStream csdecrypt=new CryptoStream (msdecrypt,decryptor,cryptostreammode.read);
Try
... {
Csdecrypt.read (decrypted,0,decrypted.length);
}
catch (Exception err)
... {
throw new ApplicationException (err. message);
}
Finally
... {
Try
... {
Msdecrypt.close ();
Csdecrypt.close ();
}
Catch... {;}
}
return this. Encodingmode.getstring (decrypted);
}
/**//**//**////<summary>
Decrypt the specified file
</summary>
<param name= "FilePath" > File path to decrypt </param>
<param name= "Outpath" > decrypted file Output path </param>
public void DecryptFile (String filepath,string outpath)
... {
BOOL Isexist=file.exists (FilePath);
if (isexist)//if present
... {
Byte[] Ivb=encoding.ascii.getbytes (THIS.IV);
Byte[] Keyb=encoding.ascii.getbytes (this. Encryptkey);
FileInfo file=new FileInfo (FilePath);
Byte[] Decrypted=new byte[file. Length];
Get the byte stream to decrypt the file
FileStream fin=new FileStream (filepath,filemode.open,fileaccess.read);
Decrypting files
Try
... {
ICryptoTransform decryptor=des. CreateDecryptor (KEYB,IVB);
CryptoStream csdecrypt=new CryptoStream (fin,decryptor,cryptostreammode.read);
Csdecrypt.read (decrypted,0,decrypted.length);
}
catch (Exception err)
... {
throw new ApplicationException (err. message);
}
Finally
... {
Try
... {
Fin. Close ();
}
Catch... {;}
}
FileStream fout=new FileStream (outpath,filemode.create,fileaccess.write);
Fout. Write (decrypted,0,decrypted.length);
Fout. Close ();
}
Else
... {
throw new FileNotFoundException ("The specified decryption file is not found");
}
}
/**//**//**////<summary>
To decrypt the overloaded version of the file, if it does not give the output path of the decrypted file,
The decrypted file will overwrite the previous file
</summary>
<param name= "FilePath" ></param>
public void DecryptFile (string filePath)
... {
This. DecryptFile (Filepath,filepath);
}
#endregion
}
/**//**//**////<summary>
MD5 Encryption class, note that MD5 encrypted information can not be converted back to the original data
, do not use this encryption technique in user-sensitive information, such as the user's password,
Please try to use symmetric encryption
</summary>
public class Md5encrypt
... {
Private MD5 MD5;
Public Md5encrypt ()
... {
Md5=new MD5CryptoServiceProvider ();
}
/**//**//**////<summary>
Get hash value from string
</summary>
<param name= "str" > string to compute hash value </param>
<returns></returns>
public string getmd5fromstring (String str)
... {
Byte[] Tocompute=encoding.unicode.getbytes (str);
Byte[] Hashed=md5.computehash (tocompute,0,tocompute.length);
Return Encoding.ASCII.GetString (hashed);
}
/**//**//**////<summary>
To compute a hash value based on a file
</summary>
<param name= "FilePath" > File path to compute hash value </param>
<returns></returns>
public string Getmd5fromfile (string filePath)
... {
BOOL Isexist=file.exists (FilePath);
if (isexist)//if file exists
... {
FileStream stream=new FileStream (filepath,filemode.open,fileaccess.read);
StreamReader reader=new StreamReader (Stream,encoding.unicode);
String Str=reader. ReadToEnd ();
Byte[] Tohash=encoding.unicode.getbytes (str);
Byte[] Hashed=md5.computehash (tohash,0,tohash.length);
Stream. Close ();
Return Encoding.ASCII.GetString (hashed);
}
else//file does not exist
... {
throw new FileNotFoundException ("specified file not found");
}
}
}
/**//**//**////<summary>
Hash class for digital signatures
</summary>
public class Mactripledesencrypt
... {
Private MACTripleDES MacT;
private string __key= "ksn168ch";
Private byte[] __data=null;
Public Mactripledesencrypt ()
... {
Mact=new MACTripleDES ();
}
/**//**//**////<summary>
Gets or sets the key used for digital signatures
</summary>
public string Key
... {
Get ... {return this.__key;}
Set
... {
int Keylength=value. Length;
Int[] Keyallowlengths=new int[] ... {8,16,24};
BOOL Isright=false;
foreach (int i in keyallowlengths)
... {
if (Keylength==keyallowlengths[i])
... {
Isright=true;
Break
}
}
if (!isright)
throw new ApplicationException ("The key length used for digital signatures must be one of the 8,16,24 values");
Else
This.__key=value;
}
}
/**//**//**////<summary>
Gets or sets the user data for digital signatures
</summary>
Public byte[] Data
... {
Get ... {return this.__data;}
Set ... {This.__data=value;}
}
/**//**//**////<summary>
Get the hash value after the signature
</summary>
<returns></returns>
public string Gethashvalue ()
... {
if (this. Data==null)
throw new Notsetspecialpropertyexception ("No users are set to digitally sign" +
"Data (Property:data)");
Byte[] Key=encoding.ascii.getbytes (this. Key);
This.mact.key=key;
Byte[] Hash_b=this.mact.computehash, This.mact.ComputeHash (this. Data));
Return Encoding.ASCII.GetString (Hash_b);
}
}
}




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.