KeyFile.txt
上传用户:zjqiusheng
上传日期:2013-06-12
资源大小:2k
文件大小:4k
源码类别:

CA认证

开发平台:

C#

  1. 用C#处理数字证书
  2. 使用System.Security.Cryptography.X509Certificate名称空间
  3. System.Security.Cryptography.X509Certificate名称空间特别重要,因为它提供了开发人员处理数字签名的那些类.
  4. 以下是读取一个X.509证书的代码:
  5. private void btnTest_Click(object sender,System.EventArgs e)
  6. {
  7. String CertPath;                      //Cerificate path
  8. X509Certificate MyCert;               //The certificate 
  9. StringBuilder CertData;               //Certificate information to display
  10. //Create the certificate path string 
  11. Certpath=Application.ExecutablePath;
  12. CertPath=CertPath.Substring(0,CertPath.LastIndexOf(@"")+1)+"1.CER";
  13. //Load the certificate
  14. MyCert=X509Certificate.CreateFromCertFile(CertPath);
  15. //Get the certificate information
  16. CertData=new StringBuilder();
  17. CertData.Append("rnPublic Key String: ");
  18. CertData.Append(MyCert.GetPublicKeyString());
  19. //Display the information on screen 
  20. MessageBox.show(CertData.ToString(),"SampleCertificate Data",MessageBoxButtons.OK,MessageBoxIcon.Information);
  21. }
  22. 3.5使用不对称加密法
  23. 3.5.1创建一个密钥对
  24. public frmMain()
  25. {
  26. String                 KeyPath;            //The location of the key
  27. CspParameters        Params;             //Cryptographic parameters
  28. FileStream            KeyFile;             //Key disk storage
  29. Char[ ]                KeyData;            //Key data as a Char array
  30. Byte[ ]                KeyConv;            //Converted key data
  31. StringBuilder          KeyString;           //Loop counter
  32. //Required for Windows Forms Designer support
  33. InitializeComponent();
  34. //Create the key string 
  35. KeyPath=Application.ExecutablePath;
  36. KeyPath=KeyPath.Substring(0,KeyPath.LastIndexOf(@””)+1)+”SpecialKey”;
  37. //Define the cryptographic parameters
  38. Params=new CspParameters();
  39. Params.KeyContainName=”TemporarySpecialKey”;
  40. Parsms.KeyNumber=1;
  41. Parsms.ProviderName=”Microsoft RSA SChannel Cryptographic Provider”;
  42. Params.ProviderType=12;
  43. Params.Flags=CspProviderFlags.UseMachineKeyStore;
  44. //Detect the presence of a key pair file
  45. if (!File.Exists(KeyPath))
  46. {
  47. //Generate a key pair 
  48. RSACrypto=new RSACryptoServiceProvider(2048,Params);
  49. //Convert the key data for storage
  50. KeyData=RSACrypto.ToXmlString(True).ToCharArray();
  51. KeyConv=new Byte[KeyData.Length];
  52. For (Counter=0;Counter<KeyData.Length;Counter++)
  53. KeyConv[Counter]=Convert.ToByte(KeyData[Counter]);
  54. //Save the key to file 
  55. KeyFile=File.Open(KeyPath,FileMode.CreateNew);
  56. KeyFile.Write(KeyConv, 0,RSACrypto.ToXmlString(true).Length);
  57. KeyFile.Close();
  58. }
  59. else 
  60. {
  61. //Open the key file for reading
  62. KeyFile=File.Open(KeyPath,FileMode.Open);
  63. KeyConv=new Byte[KeyFile.Length];
  64. KeyFile.Read(KeyConv,0,(Int32)KeyFile.Length);
  65. KeyFile.Close();
  66. //Convert the key file 
  67. KeyString=new StringBuilder(KeyConv.Length);
  68. For (Counter=0;Counter<KeyConv.Length;Counter++)
  69.   KeyString.Append(Convert.ToChar(KeyConv[Counter]));
  70. //Create the key 
  71. RSACrypto= new RSACryptoServiceProvider(2048,Params);
  72. RSACrypto.FromXmlString(KeyString.ToString());]
  73. }
  74. }
  75. 使用不对称加密加密与解密数据
  76. private RSACryptServiceProvider RSACrypto;         //the key pair
  77. private void btnEncrypt_Click(object sender,system.EventArgs e)
  78. {
  79. FileStream       FIn;           //Input file
  80. FileStream       Fout;          //Output file
  81. Byte[ ]           InData;        //Input buffer
  82. Byte[ ]           OutData;       //Output buffer
  83. Int              Counter=0;     //Total convert counter
  84. Int              ReaderByte=0;  //Currently read counter
  85. //Open the input and output files
  86. Fin=new FileStream(txtInput.Text,FileMode.Open,FileAccess.Read);
  87. Fout=new FileStream(txtEncrypt.Text,FileMode.OpenOrCreate,FileAccess.Write); 
  88. //Initialize the buffers
  89. InData=new Byte[100];
  90. OutData=new Byte[256];
  91. //Encrypt the file 
  92. while(Counter<Fin.Length)
  93. {
  94. //Determine if we’re encrypting a partial packet
  95. if ((Fin.Length-Counter)<100)
  96. {
  97. //if so ,create a small encryption value
  98. InData=new Byte[Fin.Length-Counter];
  99. ReadByte=Fin.Read(InData,0,(Int32)(Fin.Length-Counter));
  100. }
  101. else 
  102. //otherwise,create a full encryption value
  103. ReadByte=FIn.Read(InData,0,100);
  104. //output the encrypted data
  105. OutData=RSACrypt.Encrypt(InData.false);
  106. FOut.Write(OutData,0,OutData.Length);
  107. Counter=Counter+ReadByte;
  108. }
  109. //close the open stream and file
  110. FIn.Close();
  111. Fout.Close();
  112. }
  113. 以上就是应用Visual C# .NET开发工具解决读取X.509证书的公钥,并且利用公钥加密文件,实现对文件的访问控制.以上程序在WINDOWS XP ,MICROSOFT VISUAL C# .NET系统上调试通过.