本文整理汇总了C#中System.Security.Cryptography.RSAPKCS1SignatureFormatter类的典型用法代码示例。如果您正苦于以下问题:C# RSAPKCS1SignatureFormatter类的具体用法?C# RSAPKCS1SignatureFormatter怎么用?C# RSAPKCS1SignatureFormatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RSAPKCS1SignatureFormatter类属于System.Security.Cryptography命名空间,在下文中一共展示了RSAPKCS1SignatureFormatter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RSAEncrypt
static public byte[] RSAEncrypt(int bits, byte[] dataToEncrypt, RSAParameters rsaKeyInfo, bool doOAEPPadding)
{
try
{
byte[] encryptedData;
//Create a new instance of RSACryptoServiceProvider.
using (var rsa = new RSACryptoServiceProvider(bits))
{
//Import the RSA Key information. This only needs
//toinclude the public key information.
rsa.ImportParameters(rsaKeyInfo);
var rsaExportParameters = rsa.ExportParameters(true);
var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm("SHA256");
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
encryptedData = rsa.Encrypt(dataToEncrypt, doOAEPPadding);
}
return encryptedData;
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
示例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
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;
}
示例5: Sign
private byte[] Sign(SHA1CryptoServiceProvider hash)
{
var formatter = new RSAPKCS1SignatureFormatter(_certificate.PrivateKey).
Tap(it => it.SetHashAlgorithm("MD5"));
return formatter.CreateSignature(hash);
}
示例6: CreateFormatter
public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
var provider = (RSACryptoServiceProvider)key;
// The provider is probably using the default ProviderType. That's
// a problem, because it doesn't support SHA256. Let's do some
// black magic and create a new provider of a type that supports
// SHA256 without the user ever knowing we fix this. This is what
// is done in X509AsymmetricKey.GetSignatureFormatter if
// http://www.w3.org/2001/04/xmldsig-more#rsa-sha256 isn't
// a known algorithm, so users kind of expect this to be handled
// for them magically.
var cspParams = new CspParameters();
cspParams.ProviderType = 24; //PROV_RSA_AES
cspParams.KeyContainerName = provider.CspKeyContainerInfo.KeyContainerName;
cspParams.KeyNumber = (int)provider.CspKeyContainerInfo.KeyNumber;
SetMachineKeyFlag(provider, cspParams);
cspParams.Flags |= CspProviderFlags.UseExistingKey;
provider = new RSACryptoServiceProvider(cspParams);
var f = new RSAPKCS1SignatureFormatter(provider);
f.SetHashAlgorithm(typeof(SHA256Managed).FullName);
return f;
}
示例7: 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);
}
}
示例8: 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
}
示例9: 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);
}
示例10: CreateFormatter
public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
var f = new RSAPKCS1SignatureFormatter(key);
f.SetHashAlgorithm(SHA_512);
return f;
}
示例11: CreateFormatter
public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
{
if (key == null)
throw new ArgumentNullException("key");
RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
formatter.SetHashAlgorithm("SHA256");
return formatter;
}
示例12: 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);
}
示例13: 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);
}
示例14: 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;
}
示例15: 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");
}