使用DPAPI加密或解密你的数据

对于小量数据的加密,我们可以使用DPAPI,对称密钥,非对称密钥等。
对于大量数据的加密,非对称密钥加密不仅麻烦,而且速度也很慢,同时还要对公钥和密钥进行保密。
使用对称密钥,速度是相当快的,但仍然要处理密钥的问题,当然这种密钥也有时效性,因为它容易被破解。所出一般情况下用于网络上的会话传输,SSL中用的较多。
对于不想保存密钥,而又要加密和解密来说,DPAPI可能显得简单的多,而且也不需要自己写太多的代码,安全性也得到一定的保证,同时不需要保存密钥,一举多得。

使用举例,下面就DPAPI加密和解密数据库连接字符串举例:

1 None.gif string  dbconn  =   " Provider=SQLOLEDB;SERVER={0};uid={1};pwd={2};database={3} " ;
2 None.gifDataProtector dataProtector  =   new  DataProtector(DataProtector.Store.USE_MACHINE_STORE);
3 None.gif byte [] bts  =   Encoding.Default.GetBytes(dbconn);
4 None.gif byte [] bytes  =  dataProtector.Encrypt(bts,  null ); // 加密
5 None.gif string  values  =  Encoding.Default.GetString(dataProtector.Decrypt(bytes,  null )); // 解密
6 None.gif

下面是对DPAPI的封装。

  1 None.gif using  System;
  2 None.gif using  System.Runtime.InteropServices;
  3 None.gif
  4 None.gif namespace  CNetware.Data.DPAPI
  5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  6ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
  7InBlock.gif  /// DataProtector 是使用 DPAPI 来加密和解密数据的托管库。
  8InBlock.gif  /// Web 应用程序经常需要在应用程序配置文件中存储与安全性密切相关的数据,
  9InBlock.gif  /// 如数据库连接字符串和服务帐户凭据。出于安全性考虑,决不要以明文形式存
 10InBlock.gif  /// 储此类信息,而一定要在存储之前进行加密。本类是一个托管类库,它用于封装
 11InBlock.gif  /// 对数据保护 API (DPAPI) 的调用以使用基于计算机或用户的密钥存储来加密和
 12InBlock.gif  /// 解密数据。可随后从其他托管应用程序使用该库,如 ASP.NET Web 应用程序、Web
 13InBlock.gif  /// 服务以及企业服务应用程序。  
 14InBlock.gif  /// DPAPI 是加密 API (Crypto API) 的一部分并且是在 crypt32.dll 中实现的。它
 15InBlock.gif  /// 包含两个方法:CryptProtectData 和 CryptUnprotectData,本类库中方法被引用
 16InBlock.gif  /// 成了私有方法,在类外不可访问。
 17InBlock.gif  /// DPAPI 特别有用,因为它能够消除使用密码的应用程序所带来的密钥管理问题。虽
 18InBlock.gif  /// 然加密能确保数据安全,但您必须采取额外的步骤来确保密钥的安全。DPAPI 使用
 19InBlock.gif  /// 与 DPAPI 函数的调用代码关联的用户帐户的密码,以便派生加密密钥。因此,是
 20InBlock.gif  /// 操作系统(而非应用程序)管理着密钥。
 21InBlock.gif  /// 
 22InBlock.gif  /// DPAPI 能够与计算机存储或用户存储(需要一个已加载的用户配置文件)配合使用。
 23InBlock.gif  /// DPAPI 默认情况下用于用户存储,但您可以通过将 CRYPTPROTECT_LOCAL_MACHINE 
 24InBlock.gif  /// 标志传递给 DPAPI 函数来指定使用计算机存储。
 25InBlock.gif  /// 
 26InBlock.gif  /// 这种用户配置文件方式提供了一个额外的安全层,因为它限制了哪些用户能访问机
 27InBlock.gif  /// 密内容。只有加密该数据的用户才能解密该数据。但是,当通过 ASP.NET Web 应用
 28InBlock.gif  /// 程序使用 DPAPI 时,使用用户配置文件需要您执行额外的开发工作,因为您需要采
 29InBlock.gif  /// 取明确的步骤来加载和卸载用户配置文件(ASP.NET 不会自动加载用户配置文件)。
 30InBlock.gif  /// 
 31InBlock.gif  /// 计算机存储方式更容易开发,因为它不需要管理用户配置文件。但是,除非使用一个
 32InBlock.gif  /// 附加的熵参数,否则并不安全,因为该计算机的任何用户都可以解密数据。(熵是一
 33InBlock.gif  /// 个设计用来使解密机密内容更为困难的随机值)。使用附加的熵参数出现的问题在于
 34InBlock.gif  /// 它必须由应用程序安全地存储起来,这带来了另一个密钥管理问题。
 35InBlock.gif  /// 
 36InBlock.gif  /// 注意:如果您将 DPAPI 和计算机存储一起使用,那么加密字符串仅适用于给定的计
 37InBlock.gif  /// 算机,因此您必须在每台计算机上生成加密数据。不要在场或群集中将加密数据从一
 38InBlock.gif  /// 台计算机复制到另一台计算机。如果将 DPAPI 和用户存储一起使用,则可以用一个漫
 39InBlock.gif  /// 游的用户配置文件在任何一台计算机上解密数据。
 40ExpandedSubBlockEnd.gif  /// </summary>

 41InBlock.gif  public class DataProtector
 42ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 43ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 44InBlock.gif    /// 用于存储二进制数据流,该结构体有两个成员
 45ExpandedSubBlockEnd.gif    /// </summary>

 46InBlock.gif    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
 47InBlock.gif      internal struct DATA_BLOB
 48ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 49ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//// <summary>
 50InBlock.gif      /// 用于记录数据长度
 51ExpandedSubBlockEnd.gif      /// </summary>

 52InBlock.gif      public int cbData;
 53ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//// <summary>
 54InBlock.gif      /// 用于存储二进制数据
 55ExpandedSubBlockEnd.gif      /// </summary>

 56InBlock.gif      public IntPtr pbData;
 57ExpandedSubBlockEnd.gif    }

 58ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 59InBlock.gif    /// 
 60ExpandedSubBlockEnd.gif    /// </summary>

 61InBlock.gif    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
 62InBlock.gif      internal struct CRYPTPROTECT_PROMPTSTRUCT
 63ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 64InBlock.gif      public int cbSize;
 65InBlock.gif      public int dwPromptFlags;
 66InBlock.gif      public IntPtr hwndApp;
 67InBlock.gif      public String szPrompt;
 68ExpandedSubBlockEnd.gif    }

 69InBlock.gif    private static  IntPtr NullPtr = (IntPtr)0;
 70InBlock.gif
 71InBlock.gif    private const int CRYPTPROTECT_UI_FORBIDDEN = 0x1;
 72InBlock.gif
 73InBlock.gif    private const int CRYPTPROTECT_LOCAL_MACHINE = 0x4;
 74InBlock.gif
 75ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 76InBlock.gif    /// 
 77InBlock.gif    /// </summary>
 78InBlock.gif    /// <param name="pDataIn"></param>
 79InBlock.gif    /// <param name="szDataDescr"></param>
 80InBlock.gif    /// <param name="pOptionalEntropy"></param>
 81InBlock.gif    /// <param name="pvReserved"></param>
 82InBlock.gif    /// <param name="pPromptStruct"></param>
 83InBlock.gif    /// <param name="dwFlags"></param>
 84InBlock.gif    /// <param name="pDataOut"></param>
 85ExpandedSubBlockEnd.gif    /// <returns></returns>

 86InBlock.gif    [DllImport("Crypt32.dll", SetLastError=true,
 87InBlock.gif       CharSet=System.Runtime.InteropServices.CharSet.Auto)]
 88InBlock.gif    private static extern bool CryptProtectData(
 89InBlock.gif      ref DATA_BLOB pDataIn,
 90InBlock.gif      String szDataDescr,
 91InBlock.gif      ref DATA_BLOB pOptionalEntropy,
 92InBlock.gif      IntPtr pvReserved,
 93InBlock.gif      ref CRYPTPROTECT_PROMPTSTRUCT pPromptStruct,
 94InBlock.gif      int dwFlags,
 95InBlock.gif      ref DATA_BLOB pDataOut);
 96InBlock.gif
 97ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 98InBlock.gif    /// 
 99InBlock.gif    /// </summary>
100InBlock.gif    /// <param name="pDataIn"></param>
101InBlock.gif    /// <param name="szDataDescr"></param>
102InBlock.gif    /// <param name="pOptionalEntropy"></param>
103InBlock.gif    /// <param name="pvReserved"></param>
104InBlock.gif    /// <param name="pPromptStruct"></param>
105InBlock.gif    /// <param name="dwFlags"></param>
106InBlock.gif    /// <param name="pDataOut"></param>
107ExpandedSubBlockEnd.gif    /// <returns></returns>

108InBlock.gif    [DllImport("Crypt32.dll", SetLastError=true,
109InBlock.gif       CharSet=System.Runtime.InteropServices.CharSet.Auto)]
110InBlock.gif    private static extern bool CryptUnprotectData(
111InBlock.gif      ref DATA_BLOB pDataIn,
112InBlock.gif      String szDataDescr,
113InBlock.gif      ref DATA_BLOB pOptionalEntropy,
114InBlock.gif      IntPtr pvReserved,
115InBlock.gif      ref CRYPTPROTECT_PROMPTSTRUCT pPromptStruct,
116InBlock.gif      int dwFlags,
117InBlock.gif      ref DATA_BLOB pDataOut);
118InBlock.gif
119InBlock.gif    [DllImport("kernel32.dll",
120InBlock.gif       CharSet=System.Runtime.InteropServices.CharSet.Auto)]
121InBlock.gif    private unsafe static extern int FormatMessage(int dwFlags,
122InBlock.gif      ref IntPtr lpSource,
123InBlock.gif      int dwMessageId,
124InBlock.gif      int dwLanguageId,
125InBlock.gif      ref String lpBuffer, 
126InBlock.gif      int nSize,
127InBlock.gif      IntPtr *Arguments);
128InBlock.gif
129ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
130InBlock.gif    /// 用于指定创建DataProtector的类型
131ExpandedSubBlockEnd.gif    /// </summary>

132InBlock.gif    public enum Store 
133ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
134ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//// <summary>
135InBlock.gif      /// 机器存储
136ExpandedSubBlockEnd.gif      /// </summary>

137InBlock.gif      USE_MACHINE_STORE = 1
138InBlock.gif
139ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//// <summary>
140InBlock.gif      /// 用户自己存储
141ExpandedSubBlockEnd.gif      /// </summary>

142InBlock.gif      USE_USER_STORE
143ExpandedSubBlockEnd.gif    }
;
144InBlock.gif
145ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
146InBlock.gif    /// 
147ExpandedSubBlockEnd.gif    /// </summary>

148InBlock.gif    private Store m_store;
149InBlock.gif
150ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
151InBlock.gif    /// 创建数据保护对象,该对象可能指定数据和处理方法。
152InBlock.gif    /// </summary>
153ExpandedSubBlockEnd.gif    /// <param name="store">存储类型</param>

154InBlock.gif    public DataProtector(Store store)
155ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
156InBlock.gif      m_store = store;
157ExpandedSubBlockEnd.gif    }

158InBlock.gif
159ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
160InBlock.gif    /// 对传入的文本信息进行加密处理,并以字节流方式返回
161InBlock.gif    /// </summary>
162InBlock.gif    /// <param name="plainText">被字节化的文本</param>
163InBlock.gif    /// <param name="optionalEntropy">随机熵,可以为空,如果创建的是用户管理的DataProtector,该选择无效</param>
164ExpandedSubBlockEnd.gif    /// <returns>返回被加过密的内容</returns>

165InBlock.gif    public byte[] Encrypt(byte[] plainText, byte[] optionalEntropy)
166ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
167InBlock.gif      bool retVal = false;
168InBlock.gif      DATA_BLOB plainTextBlob = new DATA_BLOB();
169InBlock.gif      DATA_BLOB cipherTextBlob = new DATA_BLOB();
170InBlock.gif      DATA_BLOB entropyBlob = new DATA_BLOB();
171InBlock.gif      CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
172InBlock.gif      InitPromptstruct(ref prompt);
173InBlock.gif      int dwFlags;
174InBlock.gif      try
175ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
176InBlock.gif        try
177ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
178InBlock.gif          int bytesSize = plainText.Length;
179InBlock.gif          plainTextBlob.pbData = Marshal.AllocHGlobal(bytesSize);
180InBlock.gif          if(IntPtr.Zero == plainTextBlob.pbData)
181ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
182InBlock.gif            throw new Exception(Strings.ALLOC_TEXT_BUFFER_EXCEPTION);
183ExpandedSubBlockEnd.gif          }

184InBlock.gif          plainTextBlob.cbData = bytesSize;
185InBlock.gif          Marshal.Copy(plainText, 0, plainTextBlob.pbData, bytesSize);
186ExpandedSubBlockEnd.gif        }

187InBlock.gif        catch(Exception ex)
188ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
189InBlock.gif          throw new Exception(Strings.MERGING_EXCEPTION + ex.Message);
190ExpandedSubBlockEnd.gif        }

191InBlock.gif        if(Store.USE_MACHINE_STORE == m_store)
192ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{//使用计算机存储,应该提供熵。
193InBlock.gif          dwFlags = CRYPTPROTECT_LOCAL_MACHINE|CRYPTPROTECT_UI_FORBIDDEN;
194InBlock.gif          //查看熵是否为空
195InBlock.gif          if(null == optionalEntropy)
196ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{//分配对象
197InBlock.gif            optionalEntropy = new byte[0];
198ExpandedSubBlockEnd.gif          }

199InBlock.gif          try
200ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
201InBlock.gif            int bytesSize = optionalEntropy.Length;
202InBlock.gif            entropyBlob.pbData = Marshal.AllocHGlobal(optionalEntropy
203InBlock.gif              .Length);;
204InBlock.gif            if(IntPtr.Zero == entropyBlob.pbData)
205ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
206InBlock.gif              throw new Exception(Strings.CANNOT_ALLOC_BUFFER_FOR_ENTROPY);
207ExpandedSubBlockEnd.gif            }

208InBlock.gif            Marshal.Copy(optionalEntropy, 0, entropyBlob.pbData, bytesSize);
209InBlock.gif            entropyBlob.cbData = bytesSize;
210ExpandedSubBlockEnd.gif          }

211InBlock.gif          catch(Exception ex)
212ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
213InBlock.gif            
214InBlock.gif            throw new Exception(Strings.ENTROPY_MERGING_EXCEPTION +
215InBlock.gif              ex.Message);
216ExpandedSubBlockEnd.gif          }

217ExpandedSubBlockEnd.gif        }

218InBlock.gif        else
219ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{//使用用户存储
220InBlock.gif          dwFlags = CRYPTPROTECT_UI_FORBIDDEN;
221ExpandedSubBlockEnd.gif        }

222InBlock.gif        retVal = CryptProtectData(ref plainTextBlob, ""ref entropyBlob,
223InBlock.gif          IntPtr.Zero, ref prompt, dwFlags,
224InBlock.gif          ref cipherTextBlob);
225InBlock.gif        if(false == retVal)
226ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
227InBlock.gif          throw new Exception(Strings.ENCRYPT_FAILED +
228InBlock.gif            GetErrorMessage(Marshal.GetLastWin32Error()));
229ExpandedSubBlockEnd.gif        }

230ExpandedSubBlockEnd.gif      }

231InBlock.gif      catch(Exception ex)
232ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
233InBlock.gif        throw new Exception(Strings.ENCRYPT_EXCEPTION + ex.Message);
234ExpandedSubBlockEnd.gif      }

235InBlock.gif      byte[] cipherText = new byte[cipherTextBlob.cbData];
236InBlock.gif      Marshal.Copy(cipherTextBlob.pbData, cipherText, 0, cipherTextBlob.cbData);
237InBlock.gif      return cipherText;
238ExpandedSubBlockEnd.gif    }

239InBlock.gif
240ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
241InBlock.gif    /// 对已经加过密的数据进行解密
242InBlock.gif    /// </summary>
243InBlock.gif    /// <param name="cipherText">密文</param>
244InBlock.gif    /// <param name="optionalEntropy">随机熵</param>
245ExpandedSubBlockEnd.gif    /// <returns>返回解密后的明文</returns>

246InBlock.gif    public byte[] Decrypt(byte[] cipherText, byte[] optionalEntropy)
247ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
248InBlock.gif      bool retVal = false;
249InBlock.gif      DATA_BLOB plainTextBlob = new DATA_BLOB();
250InBlock.gif      DATA_BLOB cipherBlob = new DATA_BLOB();
251InBlock.gif      CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
252InBlock.gif      InitPromptstruct(ref prompt);
253InBlock.gif      try
254ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
255InBlock.gif        try
256ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
257InBlock.gif          int cipherTextSize = cipherText.Length;
258InBlock.gif          cipherBlob.pbData = Marshal.AllocHGlobal(cipherTextSize);
259InBlock.gif          if(IntPtr.Zero == cipherBlob.pbData)
260ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
261InBlock.gif            throw new Exception(String.Format(Strings.ALLOC_BUFFER_FAILED,"cipherBlob"));
262ExpandedSubBlockEnd.gif          }

263InBlock.gif          cipherBlob.cbData = cipherTextSize;
264InBlock.gif          Marshal.Copy(cipherText, 0, cipherBlob.pbData, cipherBlob.cbData);
265ExpandedSubBlockEnd.gif        }

266InBlock.gif        catch(Exception ex)
267ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
268InBlock.gif          throw new Exception(Strings.MERGING_EXCEPTION + ex.Message);
269ExpandedSubBlockEnd.gif        }

270InBlock.gif        DATA_BLOB entropyBlob = new DATA_BLOB();
271InBlock.gif        int dwFlags;
272InBlock.gif        if(Store.USE_MACHINE_STORE == m_store)
273ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{//使用计算机存储,应该提供熵。
274InBlock.gif          dwFlags = CRYPTPROTECT_LOCAL_MACHINE|CRYPTPROTECT_UI_FORBIDDEN;
275InBlock.gif          //查看熵是否为空
276InBlock.gif          if(null == optionalEntropy)
277ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{//分配对象
278InBlock.gif            optionalEntropy = new byte[0];
279ExpandedSubBlockEnd.gif          }

280InBlock.gif          try
281ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
282InBlock.gif            int bytesSize = optionalEntropy.Length;
283InBlock.gif            entropyBlob.pbData = Marshal.AllocHGlobal(bytesSize);
284InBlock.gif            if(IntPtr.Zero == entropyBlob.pbData)
285ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
286InBlock.gif              throw new Exception(String.Format(Strings.ALLOC_BUFFER_FAILED,""));
287ExpandedSubBlockEnd.gif            }

288InBlock.gif            entropyBlob.cbData = bytesSize;
289InBlock.gif            Marshal.Copy(optionalEntropy, 0, entropyBlob.pbData, bytesSize);
290ExpandedSubBlockEnd.gif          }

291InBlock.gif          catch(Exception ex)
292ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{
293InBlock.gif            throw new Exception("Entropy(熵)"+Strings.MERGING_EXCEPTION +ex.Message);
294ExpandedSubBlockEnd.gif          }

295ExpandedSubBlockEnd.gif        }

296InBlock.gif        else
297ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{//使用用户存储
298InBlock.gif          dwFlags = CRYPTPROTECT_UI_FORBIDDEN;
299ExpandedSubBlockEnd.gif        }

300InBlock.gif        retVal = CryptUnprotectData(ref cipherBlob, nullref entropyBlob,
301InBlock.gif          IntPtr.Zero, ref prompt, dwFlags,
302InBlock.gif          ref plainTextBlob);
303InBlock.gif        if(false == retVal)
304ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
305InBlock.gif          throw new Exception(Strings.DECRYPT_FAILED +
306InBlock.gif            GetErrorMessage(Marshal.GetLastWin32Error()));
307ExpandedSubBlockEnd.gif        }

308InBlock.gif        //释放 blob 和熵。
309InBlock.gif        if(IntPtr.Zero != cipherBlob.pbData)
310ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
311InBlock.gif          Marshal.FreeHGlobal(cipherBlob.pbData);
312ExpandedSubBlockEnd.gif        }

313InBlock.gif        if(IntPtr.Zero != entropyBlob.pbData)
314ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
315InBlock.gif          Marshal.FreeHGlobal(entropyBlob.pbData);
316ExpandedSubBlockEnd.gif        }

317ExpandedSubBlockEnd.gif      }

318InBlock.gif      catch(Exception ex)
319ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
320InBlock.gif        throw new Exception(Strings.DECRYPT_EXCEPTION + ex.Message);
321ExpandedSubBlockEnd.gif      }

322InBlock.gif      byte[] plainText = new byte[plainTextBlob.cbData];
323InBlock.gif      Marshal.Copy(plainTextBlob.pbData, plainText, 0, plainTextBlob.cbData);
324InBlock.gif      return plainText;
325ExpandedSubBlockEnd.gif    }

326InBlock.gif
327ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
328InBlock.gif    /// 
329InBlock.gif    /// </summary>
330ExpandedSubBlockEnd.gif    /// <param name="ps"></param>

331InBlock.gif    private void InitPromptstruct(ref CRYPTPROTECT_PROMPTSTRUCT ps)
332ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
333InBlock.gif      ps.cbSize = Marshal.SizeOf(typeof(CRYPTPROTECT_PROMPTSTRUCT));
334InBlock.gif      ps.dwPromptFlags = 0;
335InBlock.gif      ps.hwndApp = NullPtr;
336InBlock.gif      ps.szPrompt = null;
337ExpandedSubBlockEnd.gif    }

338InBlock.gif
339ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
340InBlock.gif    /// 
341InBlock.gif    /// </summary>
342InBlock.gif    /// <param name="errorCode"></param>
343ExpandedSubBlockEnd.gif    /// <returns></returns>

344InBlock.gif    private unsafe static String GetErrorMessage(int errorCode)
345ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
346InBlock.gif      int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
347InBlock.gif      int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
348InBlock.gif      int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
349InBlock.gif      int messageSize = 255;
350InBlock.gif      String lpMsgBuf = "";
351InBlock.gif      int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
352InBlock.gif        FORMAT_MESSAGE_FROM_SYSTEM |
353InBlock.gif        FORMAT_MESSAGE_IGNORE_INSERTS;
354InBlock.gif      IntPtr ptrlpSource = new IntPtr();
355InBlock.gif      IntPtr prtArguments = new IntPtr();
356InBlock.gif      int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0,
357InBlock.gif        ref lpMsgBuf, messageSize, &
358InBlock.gif        prtArguments);
359InBlock.gif      if(0 == retVal)
360ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
361InBlock.gif        throw new Exception(Strings.SET_ERROR_MSG_FAILED + errorCode + "");
362ExpandedSubBlockEnd.gif      }

363InBlock.gif      return lpMsgBuf;
364ExpandedSubBlockEnd.gif    }

365ExpandedSubBlockEnd.gif  }

366InBlock.gif  internal class Strings
367ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
368InBlock.gif    private Strings()
369ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
370ExpandedSubBlockEnd.gif    }

371InBlock.gif    internal static string ENTROPY_MERGING_EXCEPTION    ="熵整理数据时发生异常。";
372InBlock.gif    internal static string MERGING_EXCEPTION        ="整理数据时发生异常。";
373InBlock.gif    internal static string CANNOT_ALLOC_BUFFER_FOR_ENTROPY  ="无法分配熵数据缓冲区。";
374InBlock.gif    internal static string ALLOC_TEXT_BUFFER_EXCEPTION    ="无法分配纯文本缓冲区。";
375InBlock.gif    internal static string ALLOC_BUFFER_FAILED        ="为{0}分配缓存区失败。";
376InBlock.gif    internal static string ENCRYPT_FAILED         ="加密失败。";
377InBlock.gif    internal static string DECRYPT_FAILED         ="解密失败。";
378InBlock.gif    internal static string ENCRYPT_EXCEPTION        ="加密时发生异常。";
379InBlock.gif    internal static string DECRYPT_EXCEPTION        ="解密时发生异常。";
380InBlock.gif    internal static string SET_ERROR_MSG_FAILED       ="无法设置错误代码消息的格式。";
381InBlock.gif    internal static string NOT_RIGHT_CONFIGPATH       ="不正确的配置文件路径。";
382InBlock.gif    internal static string CONFIG_PATH_NOT_SETTED     ="配置文件的全路径未设置。";
383InBlock.gif    internal static string NOT_FOUND_CONNECTION_STRING    ="没有发现配置文件中有此文件。";
384InBlock.gif    internal static string CONN_STRING_NAMED_EXCEPTION    ="配置文件中定义的数据库连接字符串的key的名字应当以ConnectionString打头。";
385ExpandedSubBlockEnd.gif  }

386ExpandedBlockEnd.gif}

387 None.gif
388 None.gif

转载于:https://www.cnblogs.com/begincsdn/archive/2005/07/13/191987.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值