?? keyfile.txt
字號(hào):
用C#處理數(shù)字證書
使用System.Security.Cryptography.X509Certificate名稱空間
System.Security.Cryptography.X509Certificate名稱空間特別重要,因?yàn)樗峁┝碎_發(fā)人員處理數(shù)字簽名的那些類.
以下是讀取一個(gè)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("\r\nPublic Key String: ");
CertData.Append(MyCert.GetPublicKeyString());
//Display the information on screen
MessageBox.show(CertData.ToString(),"SampleCertificate Data",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
3.5使用不對(duì)稱加密法
3.5.1創(chuàng)建一個(gè)密鑰對(duì)
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());]
}
}
使用不對(duì)稱加密加密與解密數(shù)據(jù)
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();
}
以上就是應(yīng)用Visual C# .NET開發(fā)工具解決讀取X.509證書的公鑰,并且利用公鑰加密文件,實(shí)現(xiàn)對(duì)文件的訪問控制.以上程序在WINDOWS XP ,MICROSOFT VISUAL C# .NET系統(tǒng)上調(diào)試通過.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -