本文整理汇总了C#中System.Security.Cryptography.RSAPKCS1SignatureFormatter.CreateSignature方法的典型用法代码示例。如果您正苦于以下问题:C# RSAPKCS1SignatureFormatter.CreateSignature方法的具体用法?C# RSAPKCS1SignatureFormatter.CreateSignature怎么用?C# RSAPKCS1SignatureFormatter.CreateSignature使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RSAPKCS1SignatureFormatter
的用法示例。
在下文中一共展示了RSAPKCS1SignatureFormatter.CreateSignature方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Signature
public static void Signature(Stream input, Stream output, string privateKey)
{
using (var sha = new SHA256CryptoServiceProvider())
using (var rsa = new RSACryptoServiceProvider())
{
// Compute hash
var buffer = ReadAllBytes(input);
var hash = sha.ComputeHash(buffer);
// RSA Initialize
rsa.FromXmlString(privateKey);
// format
var formatter = new RSAPKCS1SignatureFormatter(rsa);
formatter.SetHashAlgorithm("SHA256");
var signature = formatter.CreateSignature(hash);
// Krile Signature Package
var magic = MagicStr + ":" + signature.Length + ":";
var magicbytes = Encoding.UTF8.GetBytes(magic);
if (magicbytes.Length > 64)
throw new Exception("Magic bits too long.");
output.Write(magicbytes, 0, magicbytes.Length);
var padding = new byte[64 - magicbytes.Length];
output.Write(padding, 0, padding.Length);
output.Write(signature, 0, signature.Length);
output.Write(buffer, 0, buffer.Length);
}
}
示例2: SignData
public byte[] SignData(byte[] hashOfDataToSign)
{
var rsaFormatter = new RSAPKCS1SignatureFormatter(mRsa);
rsaFormatter.SetHashAlgorithm("SHA256");
return rsaFormatter.CreateSignature(hashOfDataToSign);
}
示例3: SignN3Rsa
public string SignN3Rsa(string data)
{
var cspParams = new CspParameters {KeyContainerName = "XML_DSIG_RSA_KEY"};
var key = new RSACryptoServiceProvider(cspParams);
var cspBlob = key.ExportCspBlob(false);
var base64Blob = Convert.ToBase64String(cspBlob);
var rsaFormatter = new RSAPKCS1SignatureFormatter(key);
rsaFormatter.SetHashAlgorithm("MD5");
var hash = Md5Helper.GetMd5Hash(data);
var base64Hash = Convert.ToBase64String(hash);
var sign = rsaFormatter.CreateSignature(hash);
var base64Sign = Convert.ToBase64String(sign);
var signData = new SignData
{
data = data,
public_key = base64Blob,
hash = base64Hash,
sign = base64Sign
};
return new SerializationHelper<SignData>().Serialize(signData);
}
示例4: Sign
private byte[] Sign(SHA1CryptoServiceProvider hash)
{
var formatter = new RSAPKCS1SignatureFormatter(_certificate.PrivateKey).
Tap(it => it.SetHashAlgorithm("MD5"));
return formatter.CreateSignature(hash);
}
示例5: Sign
public static byte[] Sign(byte[] privateKey, Stream stream)
{
#if Windows
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(Encoding.ASCII.GetString(privateKey));
var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm("SHA256");
using (var Sha256 = SHA256.Create())
{
return rsaFormatter.CreateSignature(Sha256.ComputeHash(stream));
}
}
#endif
#if Unix
lock (_lockObject)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(Encoding.ASCII.GetString(privateKey));
var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm("SHA256");
using (var Sha256 = SHA256.Create())
{
return rsaFormatter.CreateSignature(Sha256.ComputeHash(stream));
}
}
}
#endif
}
示例6: Sign
public DigitalSignatureCreationResult Sign(DigitalSignatureCreationArguments arguments)
{
var res = new DigitalSignatureCreationResult();
try
{
var rsaProviderReceiver = new RSACryptoServiceProvider();
rsaProviderReceiver.FromXmlString(arguments.PublicKeyForEncryption.ToString());
var encryptionResult = rsaProviderReceiver.Encrypt(Encoding.UTF8.GetBytes(arguments.Message), false);
var hashed = _hashingService.Hash(Convert.ToBase64String(encryptionResult));
var rsaProviderSender = new RSACryptoServiceProvider();
rsaProviderSender.FromXmlString(arguments.FullKeyForSignature.ToString());
var signatureFormatter = new RSAPKCS1SignatureFormatter(rsaProviderSender);
signatureFormatter.SetHashAlgorithm(_hashingService.HashAlgorithmCode());
var signature = signatureFormatter.CreateSignature(hashed.HashedBytes);
res.Signature = signature;
res.CipherText = Convert.ToBase64String(encryptionResult);
res.Success = true;
}
catch (Exception ex)
{
res.ExceptionMessage = ex.Message;
}
return res;
}
示例7: CreateSignature
public byte[] CreateSignature(byte[] hash)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
RSAFormatter.SetHashAlgorithm("MD5");
m_public = RSA.ExportParameters(false);
return RSAFormatter.CreateSignature(hash);
}
示例8: sign
public byte[] sign()
{
SSC.RSA rsa = SSC.RSA.Create();
rsa.ImportParameters(RSAparams);
SSC.RSAPKCS1SignatureFormatter signer = new SSC.RSAPKCS1SignatureFormatter(rsa);
signer.SetHashAlgorithm("SHA1");
sc.Close();
return signer.CreateSignature(md.Hash);
}
示例9: sign
public byte[] sign()
{
m_cs.Close();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(m_RSAKeyInfo);
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
RSAFormatter.SetHashAlgorithm("SHA1");
return RSAFormatter.CreateSignature(m_sha1);
}
示例10: CreateSignature
public static string CreateSignature(string textToSign, string XMLprivateKey)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(XMLprivateKey);
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
RSAFormatter.SetHashAlgorithm("SHA1");
SHA1Managed SHhash = new SHA1Managed();
byte[] SignedHashValue = RSAFormatter.CreateSignature(SHhash.ComputeHash(new UnicodeEncoding().GetBytes(textToSign)));
string signature = System.Convert.ToBase64String(SignedHashValue);
return signature;
}
示例11: GenerateLicense
public static void GenerateLicense(int months)
{
// The license key, it must be 20 bytes long to be compatible with RSA
string licenseKey = DateTime.Now.Date.AddMonths(months).ToShortDateString();
// If a current license exists, add the new license length onto the old one.
if (File.Exists(Environment.CurrentDirectory + @"\licenseinfo.xml"))
{
XmlDocument licenseFile = new XmlDocument();
licenseFile.Load(Environment.CurrentDirectory + @"\licenseinfo.xml");
string savedLicenseKey = licenseFile.DocumentElement.SelectSingleNode(@"/LicenseInfo/KEY").InnerText;
string licenseExpiry = savedLicenseKey.Substring(0, savedLicenseKey.LastIndexOf(@"/") + 5);
if (Convert.ToDateTime(licenseExpiry) > DateTime.Today)
{
licenseKey = (Convert.ToDateTime(licenseExpiry).AddMonths(months)).ToShortDateString();
}
}
string machineName = Environment.MachineName;
licenseKey += machineName;
// Adds trailing 0's if the date+machinename is less than 20 charachters
while (licenseKey.Length < 20)
{
licenseKey += "0";
}
// Cuts the key down to 20 charachters if the date+machinename are too large.
licenseKey = licenseKey.Substring(0, 20);
// Byte arrays to store the license key
byte[] byteLicenseKey = Encoding.ASCII.GetBytes(licenseKey);
// The RSA handling and public key creation
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAPKCS1SignatureFormatter rsaFormatter = new RSAPKCS1SignatureFormatter(RSA);
rsaFormatter.SetHashAlgorithm("SHA1");
string publicKey = RSA.ToXmlString(false);
// The digital signature for the license
byte[] digitalSignature = rsaFormatter.CreateSignature(byteLicenseKey);
//An XML file that holds the public key, license key, and the Digital Signature
XmlDocument licenseDocument = new XmlDocument();
XmlElement parentNode = licenseDocument.CreateElement("LicenseInfo");
licenseDocument.AppendChild(parentNode);
XmlElement xmlDocLicenseKey = licenseDocument.CreateElement("KEY");
xmlDocLicenseKey.InnerText = Encoding.Default.GetString(byteLicenseKey);
parentNode.AppendChild(xmlDocLicenseKey);
XmlElement xmlDocLicenseSignature = licenseDocument.CreateElement("SignedKey");
xmlDocLicenseSignature.InnerText = Convert.ToBase64String(digitalSignature);
parentNode.AppendChild(xmlDocLicenseSignature);
XmlDocumentFragment publicKeyNode = licenseDocument.CreateDocumentFragment();
publicKeyNode.InnerXml = publicKey;
parentNode.AppendChild(publicKeyNode);
licenseDocument.Save(Environment.CurrentDirectory + @"\licenseinfo.xml");
}
示例12: Sign
public byte[] Sign(byte[] securedInput, object key)
{
using (var sha = HashAlgorithm)
{
var privateKey = Ensure.Type<AsymmetricAlgorithm>(key, "RsaUsingSha alg expects key to be of AsymmetricAlgorithm type.");
var pkcs1 = new RSAPKCS1SignatureFormatter(privateKey);
pkcs1.SetHashAlgorithm(hashMethod);
return pkcs1.CreateSignature(sha.ComputeHash(securedInput));
}
}
示例13: CalculateSignature
/// <summary>
/// Calculate the signature of <paramref name="data"/>
/// </summary>
/// <param name="data">The data to sign</param>
/// <param name="consumerSecret">The consumer secret</param>
/// <param name="tokenSecret">The token secret</param>
/// <returns>The signature</returns>
public string CalculateSignature(byte[] data, string consumerSecret, string tokenSecret)
{
var signatureFormatter = new RSAPKCS1SignatureFormatter(_privateKey);
signatureFormatter.SetHashAlgorithm("SHA1");
using (var hasher = HashAlgorithm.Create("SHA1"))
{
Debug.Assert(hasher != null, "hasher != null");
var hash = hasher.ComputeHash(data);
var signature = signatureFormatter.CreateSignature(hash);
return Convert.ToBase64String(signature);
}
}
示例14: GenerateSignature
private string GenerateSignature(OAuthContext authContext, SigningContext signingContext)
{
if (signingContext.Algorithm == null) throw Error.AlgorithmPropertyNotSetOnSigningContext();
SHA1CryptoServiceProvider sha1 = GenerateHash(signingContext);
var formatter = new RSAPKCS1SignatureFormatter(signingContext.Algorithm);
formatter.SetHashAlgorithm("MD5");
byte[] signature = formatter.CreateSignature(sha1);
return Convert.ToBase64String(signature);
}
示例15: CreateDigitalSignature_SHA1
/// <summary>
/// デジタル署名を作成する
/// </summary>
/// <param name="message">署名を付けるメッセージ</param>
/// <param name="privateKey">署名に使用する秘密鍵</param>
/// <returns>作成された署名</returns>
public static byte[] CreateDigitalSignature_SHA1(byte[] message, string privateKey)
{
byte[] hashData = SHA1.Create().ComputeHash(message);
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKey);
RSAPKCS1SignatureFormatter rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm("SHA1");
return rsaFormatter.CreateSignature(hashData);
}
}