Des (Data encryption Standard) encryption and decryption finishing

Source: Internet
Author: User
Tags md5 variables tostring
standard| Encryption | Decrypting this class is my online reference to a few documents summed up, test can be used directly, behind a section of MD5, should be independent into a class, I am lazy, so the test is written to a file, feeling is still full of practical, if there are any confidential documents, Use this thing to deal with, in the future to see when the reverse processing, but you do not forget the password on the right, if you are as lazy as me, you directly copy down the following code to use it directly.
Using System;
Using System.IO;
Using System.Text;
Using System.Security.Cryptography;
Using System.Web;

Namespace test.com
{
<summary>
Summary description of the desencryptor.
</summary>
public class Desencryptor
{
#region Private Members
<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>
Decryption key
</summary>
private string Decryptkey=null;
<summary>
Hint Information
</summary>
private string Notemessage=null;
#endregion
#region Public Properties
<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>
Decryption 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 Constructors
Public Desencryptor ()
{
//
TODO: Add constructor logic here
//
}
#endregion
#region des encrypted string
<summary>
Encrypt 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>
Decrypting a string
</summary>
<param name= "this.inputstring" > Added a secret string </param>
<param name= "Decryptkey" > Key </param>
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 files
<summary>
Des encrypted files
</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 being written at a time.
Des 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)
{
This.notemessage=error. Message.tostring ();

}
}
#endregion
#region des decryption file
<summary>
Decrypting files
</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 being written at a time.
Des 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)
{
This.notemessage=error. Message.tostring ();
}
}
#endregion
#region MD5
<summary>
MD5 Encrypt
</summary>
<param name= "StrText" >text</param>
&LT;RETURNS&GT;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

}
}



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.