我想读通过USB令牌SafeNet的签名(阿拉丁的eToken亲72 K(JAVA)),并连接到PDF格式。 我不知道如何做到这一点。 在以前,他们给导出选项.pfx
文件。 现在,他们正在给出口选项.cer
文件。 当我用Google搜索我得到这个代码。 当我运行此代码它提示令牌的密码后输入密码,我可以能够验证签名,但我不知道如何将签名附加到PDF格式。 请指导我是否是方向正确与否。 我使用C#语言
private void btnGenpdfdigitalSignature_Click(object sender, EventArgs e)
{
try
{
// Cert myCert = null;
// Sign text
byte[] signature = Sign("Test", "Name of the signature person");
// Verify signature. Testcert.cer corresponds to "cn=my cert subject"
if (Verify("Test", signature,"jai.cer"))
{
}
else
{
Console.WriteLine("ERROR: Signature not valid!");
}
}
catch (Exception ex)
{
Console.WriteLine("EXCEPTION: " + ex.Message);
}
// Console.ReadKey();
}
static byte[] Sign(string text, string certSubject)
{
// Access Personal (MY) certificate store of current user
X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
my.Open(OpenFlags.ReadOnly);
// Find the certificate we'll use to sign
RSACryptoServiceProvider csp = null;
foreach (X509Certificate2 cert in my.Certificates)
{
if (cert.Subject.Contains(certSubject))
{
// We found it.
// Get its associated CSP and private key
csp = (RSACryptoServiceProvider)cert.PrivateKey;
}
}
if (csp == null)
{
throw new Exception("No valid cert was found");
}
// Hash the data
SHA1Managed sha1 = new SHA1Managed();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(text);
byte[] hash = sha1.ComputeHash(data);
// Sign the hash
return csp.SignHash(hash, CryptoConfig.MapNameToOID("Test"));
}
public bool Verify(string text, byte[] signature, string certPath)
{
// Load the certificate we'll use to verify the signature from a file
cert = new X509Certificate2(certPath);
// Note:
// If we want to use the client cert in an ASP.NET app, we may use something like this instead:
// X509Certificate2 cert = new X509Certificate2(Request.ClientCertificate.Certificate);
// Get its associated CSP and public key
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PublicKey.Key;
// Hash the data
SHA1Managed sha1 = new SHA1Managed();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(text);
byte[] hash = sha1.ComputeHash(data);
// Verify the signature with the hash
return csp.VerifyHash(hash, CryptoConfig.MapNameToOID("Test"), signature);
}