KeyFile.txt
资源名称:KeyFile.rar [点击查看]
上传用户:zjqiusheng
上传日期:2013-06-12
资源大小:2k
文件大小:4k
源码类别:
CA认证
开发平台:
C#
- 用C#处理数字证书
- 使用System.Security.Cryptography.X509Certificate名称空间
- System.Security.Cryptography.X509Certificate名称空间特别重要,因为它提供了开发人员处理数字签名的那些类.
- 以下是读取一个X.509证书的代码:
- private void btnTest_Click(object sender,System.EventArgs e)
- {
- String CertPath; //Cerificate path
- X509Certificate MyCert; //The certificate
- StringBuilder CertData; //Certificate information to display
- //Create the certificate path string
- Certpath=Application.ExecutablePath;
- CertPath=CertPath.Substring(0,CertPath.LastIndexOf(@"")+1)+"1.CER";
- //Load the certificate
- MyCert=X509Certificate.CreateFromCertFile(CertPath);
- //Get the certificate information
- CertData=new StringBuilder();
- CertData.Append("rnPublic Key String: ");
- CertData.Append(MyCert.GetPublicKeyString());
- //Display the information on screen
- MessageBox.show(CertData.ToString(),"SampleCertificate Data",MessageBoxButtons.OK,MessageBoxIcon.Information);
- }
- 3.5使用不对称加密法
- 3.5.1创建一个密钥对
- public frmMain()
- {
- String KeyPath; //The location of the key
- CspParameters Params; //Cryptographic parameters
- FileStream KeyFile; //Key disk storage
- Char[ ] KeyData; //Key data as a Char array
- Byte[ ] KeyConv; //Converted key data
- StringBuilder KeyString; //Loop counter
- //Required for Windows Forms Designer support
- InitializeComponent();
- //Create the key string
- KeyPath=Application.ExecutablePath;
- KeyPath=KeyPath.Substring(0,KeyPath.LastIndexOf(@””)+1)+”SpecialKey”;
- //Define the cryptographic parameters
- Params=new CspParameters();
- Params.KeyContainName=”TemporarySpecialKey”;
- Parsms.KeyNumber=1;
- Parsms.ProviderName=”Microsoft RSA SChannel Cryptographic Provider”;
- Params.ProviderType=12;
- Params.Flags=CspProviderFlags.UseMachineKeyStore;
- //Detect the presence of a key pair file
- if (!File.Exists(KeyPath))
- {
- //Generate a key pair
- RSACrypto=new RSACryptoServiceProvider(2048,Params);
- //Convert the key data for storage
- KeyData=RSACrypto.ToXmlString(True).ToCharArray();
- KeyConv=new Byte[KeyData.Length];
- For (Counter=0;Counter<KeyData.Length;Counter++)
- KeyConv[Counter]=Convert.ToByte(KeyData[Counter]);
- //Save the key to file
- KeyFile=File.Open(KeyPath,FileMode.CreateNew);
- KeyFile.Write(KeyConv, 0,RSACrypto.ToXmlString(true).Length);
- KeyFile.Close();
- }
- else
- {
- //Open the key file for reading
- KeyFile=File.Open(KeyPath,FileMode.Open);
- KeyConv=new Byte[KeyFile.Length];
- KeyFile.Read(KeyConv,0,(Int32)KeyFile.Length);
- KeyFile.Close();
- //Convert the key file
- KeyString=new StringBuilder(KeyConv.Length);
- For (Counter=0;Counter<KeyConv.Length;Counter++)
- KeyString.Append(Convert.ToChar(KeyConv[Counter]));
- //Create the key
- RSACrypto= new RSACryptoServiceProvider(2048,Params);
- RSACrypto.FromXmlString(KeyString.ToString());]
- }
- }
- 使用不对称加密加密与解密数据
- private RSACryptServiceProvider RSACrypto; //the key pair
- private void btnEncrypt_Click(object sender,system.EventArgs e)
- {
- FileStream FIn; //Input file
- FileStream Fout; //Output file
- Byte[ ] InData; //Input buffer
- Byte[ ] OutData; //Output buffer
- Int Counter=0; //Total convert counter
- Int ReaderByte=0; //Currently read counter
- //Open the input and output files
- Fin=new FileStream(txtInput.Text,FileMode.Open,FileAccess.Read);
- Fout=new FileStream(txtEncrypt.Text,FileMode.OpenOrCreate,FileAccess.Write);
- //Initialize the buffers
- InData=new Byte[100];
- OutData=new Byte[256];
- //Encrypt the file
- while(Counter<Fin.Length)
- {
- //Determine if we’re encrypting a partial packet
- if ((Fin.Length-Counter)<100)
- {
- //if so ,create a small encryption value
- InData=new Byte[Fin.Length-Counter];
- ReadByte=Fin.Read(InData,0,(Int32)(Fin.Length-Counter));
- }
- else
- //otherwise,create a full encryption value
- ReadByte=FIn.Read(InData,0,100);
- //output the encrypted data
- OutData=RSACrypt.Encrypt(InData.false);
- FOut.Write(OutData,0,OutData.Length);
- Counter=Counter+ReadByte;
- }
- //close the open stream and file
- FIn.Close();
- Fout.Close();
- }
- 以上就是应用Visual C# .NET开发工具解决读取X.509证书的公钥,并且利用公钥加密文件,实现对文件的访问控制.以上程序在WINDOWS XP ,MICROSOFT VISUAL C# .NET系统上调试通过.