MSDN原文http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.descryptoserviceprovider(VS.85).aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;


namespace ConsoleApplication1
{
    
class Program
    
{
        
// Main method.
        public static void Main()
        
{
            
// Create a new DES key.
            DESCryptoServiceProvider key = new DESCryptoServiceProvider();

            
// Encrypt a string to a byte array.
            
//byte[] buffer = Encrypt("This is some plaintext!", key);

            
byte[] buffer = Encrypt("test123ABCD!@#!", key);
            
// Decrypt the byte array back to a string.
            string ss 
            
string plaintext = Decrypt(buffer, key);

            
// Display the plaintext value to the console.
            Console.WriteLine(plaintext);
        }


        
// Encrypt the string.
        public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
        
{
            
// Create a memory stream.
            MemoryStream ms = new MemoryStream();

            
// Create a CryptoStream using the memory stream and the 
            
// CSP DES key.  
            CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

            
// Create a StreamWriter to write a string
            
// to the stream.
            StreamWriter sw = new StreamWriter(encStream);

            
// Write the plaintext to the stream.
            sw.WriteLine(PlainText);

            
// Close the StreamWriter and CryptoStream.
            sw.Close();
            encStream.Close();

            
// Get an array of bytes that represents
            
// the memory stream.
            byte[] buffer = ms.ToArray();

            
// Close the memory stream.
            ms.Close();

            
// Return the encrypted byte array.
            return buffer;
        }


        
// Decrypt the byte array.
        public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
        
{
            
// Create a memory stream to the passed buffer.
            MemoryStream ms = new MemoryStream(CypherText);

            
// Create a CryptoStream using the memory stream and the 
            
// CSP DES key. 
            CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);

            
// Create a StreamReader for reading the stream.
            StreamReader sr = new StreamReader(encStream);

            
// Read the stream as a string.
            string val = sr.ReadLine();

            
// Close the streams.
            sr.Close();
            encStream.Close();
            ms.Close();

            
return val;
        }


    }

}



顺便把C++的也备份下,明天下了VC++6,温习下MFC。
 1// This sample demonstrates using a key based on the cryptographic service provider (CSP) version
 2// of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then 
 3// to decrypt the byte array back to a string.
 4using namespace System;
 5using namespace System::IO;
 6using namespace System::Text;
 7using namespace System::Security::Cryptography;
 8
 9// Encrypt the string.
10array<Byte>^ Encrypt( String^ PlainText, SymmetricAlgorithm^ key )
11{
12   
13   // Create a memory stream.
14   MemoryStream^ ms = gcnew MemoryStream;
15   
16   // Create a CryptoStream using the memory stream and the 
17   // CSP DES key.  
18   CryptoStream^ encStream = gcnew CryptoStream( ms,key->CreateEncryptor(),CryptoStreamMode::Write );
19   
20   // Create a StreamWriter to write a string
21   // to the stream.
22   StreamWriter^ sw = gcnew StreamWriter( encStream );
23   
24   // Write the plaintext to the stream.
25   sw->WriteLine( PlainText );
26   
27   // Close the StreamWriter and CryptoStream.
28   sw->Close();
29   encStream->Close();
30   
31   // Get an array of bytes that represents
32   // the memory stream.
33   array<Byte>^buffer = ms->ToArray();
34   
35   // Close the memory stream.
36   ms->Close();
37   
38   // Return the encrypted byte array.
39   return buffer;
40}

41
42
43// Decrypt the byte array.
44String^ Decrypt( array<Byte>^CypherText, SymmetricAlgorithm^ key )
45{
46   
47   // Create a memory stream to the passed buffer.
48   MemoryStream^ ms = gcnew MemoryStream( CypherText );
49   
50   // Create a CryptoStream using the memory stream and the 
51   // CSP DES key. 
52   CryptoStream^ encStream = gcnew CryptoStream( ms,key->CreateDecryptor(),CryptoStreamMode::Read );
53   
54   // Create a StreamReader for reading the stream.
55   StreamReader^ sr = gcnew StreamReader( encStream );
56   
57   // Read the stream as a string.
58   String^ val = sr->ReadLine();
59   
60   // Close the streams.
61   sr->Close();
62   encStream->Close();
63   ms->Close();
64   return val;
65}

66
67int main()
68{
69   
70   // Create a new DES key.
71   DESCryptoServiceProvider^ key = gcnew DESCryptoServiceProvider;
72   
73   // Encrypt a string to a byte array.
74   array<Byte>^buffer = Encrypt( "This is some plaintext!", key );
75   
76   // Decrypt the byte array back to a string.
77   String^ plaintext = Decrypt( buffer, key );
78   
79   // Display the plaintext value to the console.
80   Console::WriteLine( plaintext );
81}

82
83
 posted on 2008-07-17 22:46  天 天  阅读(1263)  评论(0编辑  收藏  举报