The release number of this article has been CHS307010
This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.
For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.
For Microsoft Visual Basic .NET versions of this article, see
301070.
This task content
Summary
Require encryption and decryption to confirm that it can use a full code list reference
SUMMARY This article demonstrates how to encrypt the text file using the encrypted class provided by the Microsoft .NET framework, which is unreadable, and then decrypt this information to restore it to the original format.
Back to top
The following table summarizes the recommended hardware, software, network architecture, and service pack required:
Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Microsoft Visual Studio .NET
Back to top
Encryption and decrypting the Microsoft .NET framework
System.Security.cryptographic namespace provides a variety of encryption and decryption assistance tools.
The CryptostReam class is one of the many classes offered, and the class is used to encrypt or decrypt the content when outputting the content into the file.
To encrypt file, follow these steps:
Open Visual Studio .NET. In Microsoft C # .NET, create a new console application. Visual C # .NET creates a static class and an empty main () process. Using the USING statement for System, System.Security, System.Security.cryptography, System.Text, and System.io names, this is not necessary to define declarations in these namespaces in later code. These statements must be placed before all other declarations. Using system;
Using system.io;
Using system.security;
Using system.security.cryptography;
Using system.text; add a constant to your class, indicating a key for encryption / decryption. // must be 64 bits, 8 bytes.
Private const string ssecretkey = "password"; Create a method called EncryptFile in your class, which has three parameters: SINPUTFILE, SOUTPUTFILE, and SKEY (for encryption and decryption files). Static void Encryptfile (String SinputFileName,
String SutputFileName,
String Skey) Create input and output FileStream objects for processing target file read and write during the EncryptFile process. FILESTREAM FSInput = New FileStream (SINPUTFILENAME,
Filemode.Open,
FileAccess.read;
FileStream fsencrypted = new filestream (SoutputFileName,
Filemode.create,
FileAccess.write; declare an instance of a DescryptoServiceProvider class, which represents the actual file encryption / decryption technology. At this point, if you plan to use RSA or another encryption technology, you can create a different provider. DescryptoServiceProvider des = New DescryptoServiceProvider (); must provide a key to the encryption provider in the form of byte arrays. The System.Text Namespace provides a ready-made GetBytes () function (part of its encoding function), which is a string and returns a byte array. The key length used by various encryption techniques is different; the data encryption standard (DES) uses a 64-bit key, equivalent to 8 bytes or 8 characters. If you do not provide a key, the provider will randomly generate a key, which can successfully encode the file to ensure that decryption cannot be decrypted. Note that you also need to provide initialization vectors (IV); this value is also part of the encryption process, similar to the key, if you do not provide this value, the provider will randomly generate an initialization vector. Since this value must be the same for encryption and decryption, the random generated value cannot be used. DES.key = asciiencoding.ascii.getbytes (SKEY); DES.IV = Asciiencoding.ascii.GetBytes (SKEY); Create an instance of a CryptostReam class, the method is to get an encryption object (CREATEENCRYPTOR) using the encryption provider and will Existing Output FileStream objects as part of the constructor. Icryptotransform desencrypt = des.createEncryptor ();
Cryptostream Cryptostream = New Cryptostream (fsencrypted,
Desencrypt,
CryptostReammode.write); Finally, read the input file and write to the output file via the CryptostReam object, encrypt it in this process. Byte [] ByteArrayInput = New Byte [fsinput.length - 1];
Fsinput.read (ByteArrayInput, 0, ByteaRrayinput.length);
Cryptostream.write (ByteaRrayInput, 0, ByteaRrayinput.length); Create a method called DecryptFile. The decryption process is very similar to the encryption process, but there are two main differences between the DecryptFile and the EncryptFile process. First, when you create a CRYPTOSTREAM object, use createDecryptor instead of CREATEENCRYPTOR to specify how the object is used. Second, when writing the decrypted text to the target file, the CryptostReam object is the source and is not a target stream. Static void Decryptfile (String SinputFileName,
String SutputFileName,
String skey)
{
DescryptoServiceProvider des = New DescryptoServiceProvider ();
// a 64 bit key and iv is Required for this provider.
// Set Secret Key for des Algorithm.
Des.key = asciiencoding.ascii.getbytes (SKEY); // set initialization vector.
DES.IV = asciiencoding.ascii.getbytes (SKEY);
// Create a File Stream to read the encrypted file back.
FILESTREAM FSREAD = New FileStream (SINPUTFILENAME,
Filemode.Open,
FileAccess.read;
// CREATE A DES DECRYPTOR from the des instance.
Icryptotransform deSDecrypt = des.createdecryptor ();
// Create Crypto Stream Set To Read and Do A
// Des Decryption Transform on Incoming Bytes.
Cryptostream CryptostreamDecr = New CryptostReam (fsread,
Desdecrypt,
CryptostReammode.read;
// Print Out the contents of the decrypted file.
Streamwriter fsdecrypted = new streamwriter (SoutputFileName)
FSDecrypted.write (New StreamReader (CryptostreamDecr) .readToend ());
fsdecrypted.flush ();
fsdecrypted.close ();
} Add code line to the main () process to call EncryptFile and DecryptFile. Static void main (string [] args)
{
Encryptfile ("c: //temp/test.txt",
"c: //temp//encrypted.txt",
sSecretKey);
Decryptfile ("c: //temp//encrypted.txt",
"c: //temp//decrypted.txt",
sSecretKey);
} Save and run the application to ensure that the path used by the file name points to an existing (not particularly important) file.
Back to top
Confirm that it can be tested this code with a text file (.txt) to confirm that it does have correct encryption and decryption of the file. Make sure the file is decrypted to a new file (eg in this article)
The main () process is shown in the process instead of the original file. Check the decrypted file and compare with the source file.
Back to top
Complete code list
Using system;
Using system.io;
Using system.security;
Using system.security.cryptography;
Using system.text;
Namespace CsencryPTDecrypt
{
///
/// summary description for class1.
/// summary>
Class class1
{
// must be 64 bits, 8 bytes.
Private const string ssecretKey = "password";
Static void main (string [] args)
{
Encryptfile ("c: //temp/test.txt",
"c: //temp//encrypted.txt", ssecretkey);
Decryptfile ("c: //temp//encrypted.txt",
"c: //temp//decrypted.txt",
sSecretKey);
}
Static void Encryptfile (String SinputFileName,
String SutputFileName,
String skey)
{
FILESTREAM FSInput = New FileStream (SINPUTFILENAME,
Filemode.Open,
FileAccess.read;
FileStream fsencrypted = new filestream (SoutputFileName,
Filemode.create,
FileAccess.write;
DescryptoServiceProvider des = New DescryptoServiceProvider ();
DES.key = asciiencoding.ascii.getbytes (SKEY);
DES.IV = asciiencoding.ascii.getbytes (SKEY);
Icryptotransform desencrypt = des.createEncryptor ();
Cryptostream Cryptostream = New Cryptostream (fsencrypted,
Desencrypt,
CryptostReammode.write;
Byte [] byterrayinput = new byte [fsinput.length];
Fsinput.read (ByteArrayInput, 0, ByteaRrayinput.length);
Cryptostream.write (ByteArrayInput, 0, ByteaRrayinput.length);
Cryptostream.close ();
fsinput.close ();
fsencrypted.close ();
}
Static void Decryptfile (String SinputFileName,
String SutputFileName,
String skey)
{
DescryptoServiceProvider des = New DescryptoServiceProvider ();
// a 64 bit key and iv is Required for this provider.
// Set Secret Key for des Algorithm.
DES.key = asciiencoding.ascii.getbytes (SKEY);
// set initialization vector.
DES.IV = asciiencoding.ascii.getbytes (SKEY);
// Create a File Stream to read the encrypted file back.
FILESTREAM FSREAD = New FileStream (SINPUTFILENAME,
Filemode.Open,
FileAccess.read;
// CREATE A DES DECRYPTOR from the des instance.
Icryptotransform deSDecrypt = des.createdecryptor ();
// Create Crypto Stream Set To Read and Do A
// Des Decryption Transform on Incoming Bytes.
Cryptostream CryptostreamDecr = New CryptostReam (fsread, Desdecrypt,
CryptostReammode.read;
// Print Out the contents of the decrypted file.
Streamwriter fsdecrypted = new streamwriter (SoutputFileName);
FSDecrypted.write (New StreamReader (CryptostreamDecr) .readToend ());
fsdecrypted.flush ();
fsdecrypted.close ();
}
}
}
Back to top
Refer to more information about using the .NET encryption, and relevant encrypted general information, please visit the following Microsoft Developer Network (MSDN) Web site:
Reference System.Security.Cryptography http://msdn.microsoft.com/library/dotnet/cpref/frlrfsystemsecuritycryptography.htm MSDN Online .NET Developer Center http://msdn.microsoft.com/net
Back to top
The information in this article applies to:
Microsoft Visual C # .NET Beta 2
Recent Updated: 2001-11-5 (1.0) Keyword Kbhowto KbhowTomaster KB307010 KBAUDDEVELOPER