本文整理汇总了C#中System.Security.Cryptography.RSACryptoServiceProvider.SignData方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.SignData方法的具体用法?C# RSACryptoServiceProvider.SignData怎么用?C# RSACryptoServiceProvider.SignData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.SignData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
RSACryptoServiceProvider signer = new RSACryptoServiceProvider();
FileStream file = new FileStream(args[0], FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(file);
byte[] data = reader.ReadBytes((int)file.Length);
byte[] signature = signer.SignData(data, new SHA256CryptoServiceProvider());
string publicKey = signer.ToXmlString(false);
Console.WriteLine("Signature: " + Convert.ToBase64String(signature));
reader.Close();
file.Close();
RSACryptoServiceProvider verifier = new RSACryptoServiceProvider();
verifier.FromXmlString(publicKey);
FileStream file2 = new FileStream(args[0], FileMode.Open, FileAccess.Read);
BinaryReader reader2 = new BinaryReader(file2);
byte[] data2 = reader2.ReadBytes((int)file2.Length);
if (verifier.VerifyData(data2, new SHA256CryptoServiceProvider(), signature))
Console.WriteLine("Signature verified");
else
Console.WriteLine("Signature NOT verified");
reader2.Close();
file2.Close();
}
示例2: SignData
public static string SignData(byte[] message, RSAParameters privateKey)
{
//// The array to store the signed message in bytes
byte[] signedBytes;
using (var rsa = new RSACryptoServiceProvider())
{
byte[] originalData = message;
try
{
//// Import the private key used for signing the message
rsa.ImportParameters(privateKey);
//// Sign the data, using SHA512 as the hashing algorithm
signedBytes = rsa.SignData(originalData, CryptoConfig.MapNameToOID("SHA512"));
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
finally
{
//// Set the keycontainer to be cleared when rsa is garbage collected.
rsa.PersistKeyInCsp = false;
}
}
//// Convert the a base64 string before returning
return Convert.ToBase64String(signedBytes);
}
示例3: SignAuthCookieV1
public string SignAuthCookieV1(string cookie)
{
byte[] privateKey = this.GetPrivateKey(KeyType.AuthCookieV1);
//// The array to store the signed message in bytes
byte[] signedBytes;
using (var rsa = new RSACryptoServiceProvider())
{
// Write the message to a byte array using UTF8 as the encoding.
byte[] originalData = System.Text.Encoding.UTF8.GetBytes(cookie);
try
{
// Import the private key used for signing the message
rsa.ImportParameters(SecureSigningService.FromBinaryToRSAParameters(privateKey));
signedBytes = rsa.SignData(originalData, CryptoConfig.MapNameToOID("SHA512"));
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
finally
{
//// Set the keycontainer to be cleared when rsa is garbage collected.
rsa.PersistKeyInCsp = false;
}
}
// Convert the a base64 string before returning
return Convert.ToBase64String(signedBytes);
}
示例4: SignData
public static string SignData(string originalMessage, RSAParameters Parameters)
{
string success = "";
var encoder = new UTF8Encoding();
byte[] bytesToSign = encoder.GetBytes(originalMessage);
RSAParameters parameters = Parameters;
using (var rsa = new RSACryptoServiceProvider())
{
try
{
rsa.ImportParameters(parameters);
SHA256Managed Hash = new SHA256Managed();
byte[] hashedData = Hash.ComputeHash(bytesToSign);
success = Convert.ToBase64String(rsa.SignData(hashedData, CryptoConfig.MapNameToOID("SHA256")));
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
return success;
}
示例5: Main
static void Main(string[] args)
{
// To idendify the Smart Card CryptoGraphic Providers on your
// computer, use the Microsoft Registry Editor (Regedit.exe).
// The available Smart Card CryptoGraphic Providers are listed
// in HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.
// Create a new CspParameters object that identifies a
// Smart Card CryptoGraphic Provider.
// The 1st parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider Types.
// The 2nd parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.
CspParameters csp = new CspParameters(1);
csp.KeyContainerName = "MyKeyContainer ya!!";
csp.Flags = CspProviderFlags.UseDefaultKeyContainer;
// Initialize an RSACryptoServiceProvider object using
// the CspParameters object.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
Console.WriteLine("KeyName:{0}", rsa.ToXmlString(true));
// Create some data to sign.
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
Console.WriteLine("Data : " + BitConverter.ToString(data, 0, data.Length));
// Sign the data using the Smart Card CryptoGraphic Provider.
byte[] signData = rsa.SignData(data, "SHA1");
Console.WriteLine("Signature:" + BitConverter.ToString(signData));
bool verify = rsa.VerifyData(data, "SHA1", signData);//原始資料和sign過的資料作SHA1比對
Console.WriteLine("驗證資料:" + verify);
Console.ReadKey();
}
示例6: Main
static void Main(string[] args)
{
// Create message and signature on your end
string message = "Here is the license message";
var secret = "wangchunlei";
var converter = new ASCIIEncoding();
byte[] plainText = converter.GetBytes(secret);
var rsaWrite = new RSACryptoServiceProvider();
var privateParams = rsaWrite.ExportParameters(true);
// Generate the public key / these can be sent to the user.
var publicParams = rsaWrite.ExportParameters(false);
byte[] signature =
rsaWrite.SignData(plainText, new SHA1CryptoServiceProvider());
// Verify from the user's side. Note that only the public parameters
// are needed.
var rsaRead = new RSACryptoServiceProvider();
rsaRead.ImportParameters(publicParams);
if (rsaRead.VerifyData(plainText,
new SHA1CryptoServiceProvider(),
signature))
{
Console.WriteLine("Verified!");
}
else
{
Console.WriteLine("NOT verified!");
}
}
示例7: MakeDigitalSignature
/// <summary>
/// Metoda za digitalno potpisivanje dokumenta
/// </summary>
/// <param name="file"></param>
public void MakeDigitalSignature(string file)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
StreamReader streamReader = new StreamReader("privatni_kljuc.txt");
string publicOnlyKeyXml = streamReader.ReadToEnd();
rsa.FromXmlString(publicOnlyKeyXml);
streamReader.Close();
streamReader.Dispose();
FileStream dat = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryReader binReader = new BinaryReader(dat);
byte[] data = binReader.ReadBytes((int)dat.Length);
byte[] sign = rsa.SignData(data, "SHA1");
binReader.Close();
binReader.Dispose();
dat.Close();
dat.Dispose();
string datName = file + ".dp";
TextWriter tw = new StreamWriter(datName);
tw.WriteLine(Convert.ToBase64String(sign));
tw.Close();
tw.Dispose();
}
示例8: Calculate
public string Calculate(string input, RSACryptoServiceProvider provider)
{
byte[] podatki = Encoding.ASCII.GetBytes(input);
byte[] signature = provider.SignData(podatki, CryptoConfig.MapNameToOID("SHA256"));
return this.CalculateMD5Hash(signature);
}
示例9: Main
static void Main(string[] args)
{
// Create digital signature algortihm object
// This will generate private/public key pair
RSACryptoServiceProvider signer = new RSACryptoServiceProvider();
// array to hold signature - will be shared
byte[] signature = null;
// string to hold public key - will be shared
string publicKey = null;
using(FileStream file = new FileStream(@"info.txt", FileMode.Open,
FileAccess.Read))
{
// read file to be used to create signature into a byte array
BinaryReader reader = new BinaryReader(file);
byte[] data = reader.ReadBytes((int)file.Length);
// create signature by signing data - generates a digital signature by first
// generating the hash the data and then generate a signature based on the
// hash and the private key
// file, signature and public key are then shared with the recipient
signature = signer.SignData(data,new SHA1CryptoServiceProvider());
// export public key
publicKey = signer.ToXmlString(false);
reader.Close();
file.Close();
}
// Create digital signature algortihm object
// which will use the public key exported by the signer
RSACryptoServiceProvider verifier = new RSACryptoServiceProvider();
verifier.FromXmlString(publicKey);
using (FileStream file2 = new FileStream(@"info.txt", FileMode.Open,
FileAccess.Read))
{
// read file to be used to verify the signature into a byte array
BinaryReader reader2 = new BinaryReader(file2);
byte[] data2 = reader2.ReadBytes((int)file2.Length);
// verify the signature based on the contents of the file
// verification will only succeed if the signature was generated from this
// file using the correct private key, thus confirming the identity of the
// signer
if (verifier.VerifyData(data2, new SHA1CryptoServiceProvider(), signature))
{
Console.WriteLine("Verified");
}
else
{
Console.WriteLine("NOT verified");
}
reader2.Close();
file2.Close();
}
}
示例10: AsymmetricSign
public static byte[] AsymmetricSign(byte[] Data, string xmlPrivateKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
byte[] signData = rsa.SignData(Data, new SHA1CryptoServiceProvider());
return signData;
}
示例11: ComputeSignature
private static byte[] ComputeSignature(string outputFileName, RSACryptoServiceProvider cryptoServiceProvider)
{
byte[] signature;
using( var packageFileStream = new FileStream(outputFileName, FileMode.Open))
{
signature = cryptoServiceProvider.SignData(packageFileStream, CreateHashAlgorithm());
}
return signature;
}
示例12: RsaTest_ExportImportTest
public MFTestResults RsaTest_ExportImportTest()
{
bool testResult = true;
try
{
using (Session session = new Session("", MechanismType.RSA_PKCS))
using (CryptoKey privateKey = CryptoKey.LoadKey(session, m_importKeyPrivate))
{
string dataToSign = "This is a simple message to be encrypted";
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(dataToSign);
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(privateKey))
{
RSAParameters kp1 = rsa.ExportParameters(true);
byte[] sig = rsa.SignData(data);
rsa.ImportParameters(kp1);
RSAParameters kp2 = rsa.ExportParameters(true);
testResult &= CompareByteArray(kp1.D, kp2.D);
testResult &= CompareByteArray(kp1.DP, kp2.DP);
testResult &= CompareByteArray(kp1.DQ, kp2.DQ);
testResult &= CompareByteArray(kp1.Exponent, kp2.Exponent);
testResult &= CompareByteArray(kp1.InverseQ, kp2.InverseQ);
testResult &= CompareByteArray(kp1.Modulus, kp2.Modulus);
testResult &= CompareByteArray(kp1.P, kp2.P);
testResult &= CompareByteArray(kp1.Q, kp2.Q);
testResult &= CompareByteArray(m_importKeyPrivate[2].Value, kp1.Modulus);
testResult &= CompareByteArray(m_importKeyPrivate[3].Value, kp1.Exponent);
testResult &= CompareByteArray(m_importKeyPrivate[4].Value, kp1.D);
testResult &= CompareByteArray(m_importKeyPrivate[5].Value, kp1.P);
testResult &= CompareByteArray(m_importKeyPrivate[6].Value, kp1.Q);
testResult &= CompareByteArray(m_importKeyPrivate[7].Value, kp1.DP);
testResult &= CompareByteArray(m_importKeyPrivate[8].Value, kp1.DQ);
testResult &= CompareByteArray(m_importKeyPrivate[9].Value, kp1.InverseQ);
testResult &= rsa.VerifyData(data, sig);
}
}
}
catch (Exception ex)
{
Log.Exception("Unexpected Exception", ex);
testResult = false;
}
return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
}
示例13: SignData
/// <summary>
/// Create a crytographic signature for a block of data.
/// </summary>
/// <param name="data">The data to be signed.</param>
/// <param name="rsa">The Crypto Service Provider to be used.</param>
/// <returns>The signature string for the data block.</returns>
public static string SignData(this byte[] data, RSACryptoServiceProvider rsa)
{
if(data == null) {
throw new ArgumentNullException("data");
}
if(rsa == null) {
throw new ArgumentNullException("rsa");
}
// sign data and prepend signature type
return "rsa-sha1:" + Convert.ToBase64String(rsa.SignData(data, "SHA1"));
}
示例14: CreateSignature
public static string CreateSignature(string text, string my_private_key)
{
RSACryptoServiceProvider rsacp = new RSACryptoServiceProvider(2048);
rsacp.FromXmlString(my_private_key);
ASCIIEncoding ByteConverter = new ASCIIEncoding();
byte[] sign_this = ByteConverter.GetBytes(text);
byte[] signature = rsacp.SignData(sign_this, new SHA1CryptoServiceProvider());
string base64_string = Convert.ToBase64String(signature);
return base64_string;
}
示例15: SignLicense
private static string SignLicense(string privateKeyFile, string licenseFile)
{
string keyXml = GetKeyXml(privateKeyFile);
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(keyXml);
using (FileStream stream = File.OpenRead(licenseFile))
{
string signature = EncodeToBase64(provider.SignData(stream, new SHA1CryptoServiceProvider()));
return signature;
}
}