本文整理汇总了C#中System.Security.Cryptography.RSAPKCS1SignatureFormatter.SetKey方法的典型用法代码示例。如果您正苦于以下问题:C# RSAPKCS1SignatureFormatter.SetKey方法的具体用法?C# RSAPKCS1SignatureFormatter.SetKey怎么用?C# RSAPKCS1SignatureFormatter.SetKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RSAPKCS1SignatureFormatter
的用法示例。
在下文中一共展示了RSAPKCS1SignatureFormatter.SetKey方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: sign
private byte[] sign(byte[] message)
{
if (certificate.PrivateKey == null)
{
throw new Exception("a private key is required when generating RSA-SHA1 signatures.");
}
using (HashAlgorithm hasher = HashAlgorithm.Create("SHA1"))
{
RSAPKCS1SignatureFormatter signatureFormatter = new RSAPKCS1SignatureFormatter();
signatureFormatter.SetKey(certificate.PrivateKey);
signatureFormatter.SetHashAlgorithm("SHA1");
byte[] hash = hasher.ComputeHash(message);
return signatureFormatter.CreateSignature(hash);
}
}
示例2: CreateSignatureHash
private byte[] CreateSignatureHash (string hashName)
{
fmt = new RSAPKCS1SignatureFormatter ();
// we need the private key
RSA rsa = RSA.Create ("Mono.Security.Cryptography.RSAManaged"); // only available with Mono::
if (rsa == null) {
rsa = RSA.Create ();
}
rsa.ImportParameters (AllTests.GetRsaKey (true));
fmt.SetKey (rsa);
HashAlgorithm hash = HashAlgorithm.Create (hashName);
byte[] data = new byte [(hash.HashSize >> 3)];
hash.ComputeHash (data);
return fmt.CreateSignature (hash);
}
示例3: SetKeyRSA
public void SetKeyRSA ()
{
fmt = new RSAPKCS1SignatureFormatter ();
fmt.SetKey (rsa);
}
示例4: CreateSignatureRSABadHash
public void CreateSignatureRSABadHash ()
{
fmt = new RSAPKCS1SignatureFormatter ();
// we need the private key
fmt.SetKey (rsa);
// null (bad ;-)
byte[] hash = null;
byte[] signature = fmt.CreateSignature (hash);
}
示例5: CreateSignatureHashMD5
public void CreateSignatureHashMD5 ()
{
fmt = new RSAPKCS1SignatureFormatter ();
byte[] data = new byte [16];
// we need the private key
fmt.SetKey (rsa);
// good MD5
HashAlgorithm hash = MD5.Create ();
hash.ComputeHash (data);
byte[] signature = fmt.CreateSignature (hash);
Assert.IsNotNull (signature);
}
示例6: CreateSignatureRSAMD5
public void CreateSignatureRSAMD5 ()
{
fmt = new RSAPKCS1SignatureFormatter ();
// we need the private key
fmt.SetKey (rsa);
// good MD5
fmt.SetHashAlgorithm ("MD5");
byte[] hash = new byte [16];
byte[] signature = fmt.CreateSignature (hash);
Assert.IsNotNull (fmt);
}
示例7: CreateSignatureRSASHA1BadLength
public void CreateSignatureRSASHA1BadLength ()
{
fmt = new RSAPKCS1SignatureFormatter ();
// we need the private key
fmt.SetKey (rsa);
// wrong length SHA1
fmt.SetHashAlgorithm ("SHA1");
byte[] hash = new byte [19];
byte[] signature = fmt.CreateSignature (hash);
}
示例8: CreateSignatureNullHash
public void CreateSignatureNullHash ()
{
fmt = new RSAPKCS1SignatureFormatter ();
fmt.SetKey (rsa);
byte[] hash = null;
byte[] signature = fmt.CreateSignature (hash);
}
示例9: SetKeyNull
public void SetKeyNull ()
{
fmt = new RSAPKCS1SignatureFormatter ();
fmt.SetKey (null);
}
示例10: Sign
/// <summary>
/// Signiert einen Text mit dem angegebenen privaten Schlüssel.
/// </summary>
/// <param name="textToSign">Der zusignierende Text.</param>
/// <param name="privateKey">Der private Schlüssel.</param>
/// <returns></returns>
public static string Sign(string textToSign, string privateKey) {
//Initialisieren der Provider
var rsaCryptoServiceProvider = new RSACryptoServiceProvider();
var rsaFormatter = new RSAPKCS1SignatureFormatter(rsaCryptoServiceProvider);
System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create();
var encoding = new ASCIIEncoding();
var sha1 = new SHA1Managed();
//Zuweisen des Hashalgorithmus und des privaten Schlüssels
rsaFormatter.SetHashAlgorithm("SHA1");
rsa.FromXmlString(privateKey);
rsaFormatter.SetKey(rsa);
//String nach Byte[] Konvertieren und die Signatur erstellen
byte[] valueToHash = encoding.GetBytes(textToSign);
byte[] signedValue = rsaFormatter.CreateSignature(sha1.ComputeHash(valueToHash));
return Convert.ToBase64String(signedValue);
}