本文整理汇总了C#中System.Security.Cryptography.RSAParameters类的典型用法代码示例。如果您正苦于以下问题:C# RSAParameters类的具体用法?C# RSAParameters怎么用?C# RSAParameters使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RSAParameters类属于System.Security.Cryptography命名空间,在下文中一共展示了RSAParameters类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DecryptFromBase64
/// <summary>
/// 从base64字符串解密
/// </summary>
/// <param name="privateKey"></param>
/// <param name="content"></param>
/// <param name="size"></param>
/// <returns></returns>
public static string DecryptFromBase64(RSAParameters privateKey, string content, int size = 1024)
{
var rsa = new RSACryptoServiceProvider(size);
rsa.ImportParameters(privateKey);
var cipherbytes = rsa.Decrypt(content.Base64ToBytes(), false);
return cipherbytes.FromUtf8Bytes();
}
示例2: Init
private void Init()
{
string KeyFile = TAppSettingsManager.GetValue("Server.ChannelEncryption.PrivateKeyfile");
// read the encryption key from the specified file
FileInfo fi = new FileInfo(KeyFile);
if (!fi.Exists)
{
throw new RemotingException(
String.Format("Specified keyfile {0} does not exist", KeyFile));
}
XmlDocument doc = new XmlDocument();
doc.Load(KeyFile);
FPrivateKey = new RSAParameters();
try
{
FPrivateKey.D = Convert.FromBase64String(TXMLParser.GetChild(doc.FirstChild, "D").InnerText);
FPrivateKey.P = Convert.FromBase64String(TXMLParser.GetChild(doc.FirstChild, "P").InnerText);
FPrivateKey.Q = Convert.FromBase64String(TXMLParser.GetChild(doc.FirstChild, "Q").InnerText);
FPrivateKey.DP = Convert.FromBase64String(TXMLParser.GetChild(doc.FirstChild, "DP").InnerText);
FPrivateKey.DQ = Convert.FromBase64String(TXMLParser.GetChild(doc.FirstChild, "DQ").InnerText);
FPrivateKey.InverseQ = Convert.FromBase64String(TXMLParser.GetChild(doc.FirstChild, "InverseQ").InnerText);
FPrivateKey.Modulus = Convert.FromBase64String(TXMLParser.GetChild(doc.FirstChild, "Modulus").InnerText);
FPrivateKey.Exponent = Convert.FromBase64String(TXMLParser.GetChild(doc.FirstChild, "Exponent").InnerText);
}
catch (Exception)
{
throw new RemotingException(
String.Format("Problems reading the keyfile {0}. Cannot find all attributes of the key.", KeyFile));
}
}
示例3: EnrollCandidate
/// <summary>
/// Enroll a candidate a to Factom. A new chain is created and the candidate data is packed and split into entries for
/// that chain.
/// This operation is irreversable.
/// </summary>
/// <param name="c">The candidate to enroll</param>
/// <param name="password">The password provided by the candidate</param>
/// <param name="privKey">The private key to pack the data with</param>
/// <returns>The chain ID of the enrolled candidate</returns>
public static byte[] EnrollCandidate(Candidate c, string password, RSAParameters privKey)
{
var packed = Pack(c, password, privKey);
Chain.ChainType factomChain = null;
foreach (
var segment in
DataSegment.Segmentize(packed,
firstSegmentLength: DataSegment.DefaultMaxSegmentLength - ExtIDsLength)) {
var dataToUpload = segment.Pack();
var factomEntry = Entry.NewEntry(dataToUpload, null, null);
if (segment.CurrentSegment == 0) {
//New chain
factomEntry.ExtIDs = GenerateExtIDs(packed);
factomChain = Chain.NewChain(factomEntry);
Chain.CommitChain(factomChain, FactomWallet); // Wallet Name
Thread.Sleep(10100);
Chain.RevealChain(factomChain);
}
else {
//new entry
Debug.Assert(factomChain != null, "factomChain != null");
factomEntry.ChainId = factomChain.ChainId;
Entry.CommitEntry(factomEntry, FactomWallet);
Thread.Sleep(10100);
Entry.RevealEntry(factomEntry);
}
}
return factomChain.ChainId;
}
示例4: WriteRSAParamsToFile
public void WriteRSAParamsToFile(string file)
{
using (var sw = new StreamWriter(new FileStream(file, FileMode.Append, FileAccess.Write)))
{
var sb = new StringBuilder();
sb.AppendLine("class RsaStore");
sb.AppendLine("{");
// Write all private & public rsa parameters.
WritePublicByteArray(ref sb, "D", RsaParams.D);
WritePublicByteArray(ref sb, "DP", RsaParams.DP);
WritePublicByteArray(ref sb, "DQ", RsaParams.DQ);
WritePublicByteArray(ref sb, "Exponent", RsaParams.Exponent);
WritePublicByteArray(ref sb, "InverseQ", RsaParams.InverseQ);
WritePublicByteArray(ref sb, "Modulus", RsaParams.Modulus);
WritePublicByteArray(ref sb, "P", RsaParams.P);
WritePublicByteArray(ref sb, "Q", RsaParams.Q);
sb.AppendLine("}");
sw.WriteLine(sb.ToString());
}
// Reset all values
RsaParams = new RSAParameters();
}
示例5: ToPEM
public static string ToPEM(RSAParameters Parameters)
{
StringBuilder sb = new StringBuilder();
MemoryStream ms = new MemoryStream(),
all = new MemoryStream();
bool hasPrivate = Parameters.D != null;
string type = hasPrivate ? "PRIVATE" : "PUBLIC";
byte[] noise = new byte[] { 0x00 };
sb.AppendFormat("-----BEGIN RSA {0} KEY-----\r\n", type);
writeBytes(ms, 0x02, noise);
writeBytes(ms, 0x02, Parameters.Modulus);
writeBytes(ms, 0x02, Parameters.Exponent);
if(hasPrivate) {
writeBytes(ms, 0x02, Parameters.D);
writeBytes(ms, 0x02, Parameters.P);
writeBytes(ms, 0x02, Parameters.Q);
writeBytes(ms, 0x02, Parameters.DP);
writeBytes(ms, 0x02, Parameters.DQ);
writeBytes(ms, 0x02, Parameters.InverseQ);
}
writeBytes(all, 0x30, ms.ToArray());
sb.AppendLine(Convert.ToBase64String(all.ToArray(), Base64FormattingOptions.InsertLineBreaks));
sb.AppendFormat("-----END RSA {0} KEY-----", type);
return sb.ToString();
}
示例6: 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;
}
}
示例7: ParseAccounts
private Account[] ParseAccounts(List<ParserHelper.Chunk> chunks, byte[] encryptionKey)
{
var accounts = new List<Account>(chunks.Count(i => i.Id == "ACCT"));
SharedFolder folder = null;
var rsaKey = new RSAParameters();
foreach (var i in chunks)
{
switch (i.Id)
{
case "ACCT":
var account = ParserHelper.Parse_ACCT(i,
folder == null ? encryptionKey : folder.EncryptionKey,
folder);
if (account != null)
accounts.Add(account);
break;
case "PRIK":
rsaKey = ParserHelper.Parse_PRIK(i, encryptionKey);
break;
case "SHAR":
folder = ParserHelper.Parse_SHAR(i, encryptionKey, rsaKey);
break;
}
}
return accounts.ToArray();
}
示例8: FromXmlString
public override void FromXmlString (string xmlString)
{
if (xmlString == null)
throw new ArgumentNullException ("xmlString");
RSAParameters rsaParams = new RSAParameters ();
try {
rsaParams.P = GetNamedParam (xmlString, "P");
rsaParams.Q = GetNamedParam (xmlString, "Q");
rsaParams.D = GetNamedParam (xmlString, "D");
rsaParams.DP = GetNamedParam (xmlString, "DP");
rsaParams.DQ = GetNamedParam (xmlString, "DQ");
rsaParams.InverseQ = GetNamedParam (xmlString, "InverseQ");
rsaParams.Exponent = GetNamedParam (xmlString, "Exponent");
rsaParams.Modulus = GetNamedParam (xmlString, "Modulus");
ImportParameters (rsaParams);
}
catch (Exception e) {
ZeroizePrivateKey (rsaParams);
throw new CryptographicException (
Locale.GetText ("Couldn't decode XML"), e);
}
finally {
ZeroizePrivateKey (rsaParams);
}
}
示例9: RSAEncrypt
static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
try
{
byte[] encryptedData;
// Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
// Import the RSA Key information. This only needs
// toinclude the public key information.
RSA.ImportParameters(RSAKeyInfo);
// Encrypt the passed byte array and specify OAEP padding.
// OAEP padding is only available on Microsoft Windows XP or
// later.
encryptedData = RSA.SignHash(DataToEncrypt, CryptoConfig.MapNameToOID("SHA256"));
}
return encryptedData;
}
// Catch and display a CryptographicException
// to the console.
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
示例10: GetProviderFromDerEncodedRsaPrivateKey
public void GetProviderFromDerEncodedRsaPrivateKey()
{
string keyValid;
string keyDER = null;
RSAParameters parameters = new RSAParameters();
parameters.D = Convert.FromBase64String(@"pRgiRK2tfvFdYcGbiqyJ+rgi/HTAPEnR/dtr87I5ctDwOzBG0qOaB3oiUW7qEU0G0iy4hNc1zaHsjhSZYgKZEHP+Xgs7RJZYOTPI9sqbymrDJDLur7h2pMvsqLhcJjEn6qz+hnLMT046D9uSMg9Tpr0Z6FUiOoAwnUZcSK50gj0=");
parameters.DP = Convert.FromBase64String(@"w49jS+lsTPP5l8QLmMWeyKQ1PzWpRWsV0DJPHRZFHdjNtQkW1zMn5yJsGJ0a9yqXROv6n3BY18iuqY0S/c2PYw==");
parameters.DQ = Convert.FromBase64String(@"tzis4VqnqbIsZ/CkcBE6Nz3/Rk9nnU5Bw6JyZMs2DVY3JJtOVdmqzZmQ4KquonW5IH6ti3W3844ao+sHm3o3xQ==");
parameters.Exponent = Convert.FromBase64String(@"AQAB");
parameters.InverseQ = Convert.FromBase64String(@"/ihX2MBotgMyCIhbyR8l/7G877/nF5BFIC8RGUJqh0SFovYRHVEleLTK7Pi7eA7+OokKEwshZlfwPuE7xiIKyw==");
parameters.Modulus = Convert.FromBase64String(@"9ws/iSH6l00F/3HUhoJQyY2Y1iorw0roP9BcZRxEmtEfRzPmLnWwrQpusWJUfK71LQu/OLPD9qtnfQdVIGBMns7gfFJBGq+Dsef7CVRb0HIZv3kqUAh8AI46KIx3xRKsdVY4mh7QZcSdAyHHUi0839yNVbObhXDUNETgT5CzKFU=");
parameters.P = Convert.FromBase64String(@"/u0mEbvml8X8DrbKBiB0QGX9+G2ALRN+SwasDi7jW65SeBf49ENPxH8iC5XXB/yxQpBV2RojferhdE4Nh1+btw==");
parameters.Q = Convert.FromBase64String(@"+BWZ2QG6x2gL5qOqfZd6wtP/eQRLVz9OC2IUfw0ZojHuWXt45ybw/F+o+bQmyQcTFYES6hFYTUtWjMgn5IG0Uw==");
using (RSACryptoServiceProvider providerValid = new RSACryptoServiceProvider())
{
providerValid.ImportParameters(parameters);
keyValid = providerValid.ToXmlString(true);
}
byte[] fileBytes = File.ReadAllBytes(@"..\..\..\..\Resources\Tests\private.der");
using (RSACryptoServiceProvider providerDER = RSACryptoHelper.GetProviderFromDerEncodedRsaPrivateKey(fileBytes))
{
if (providerDER != null)
keyDER = providerDER.ToXmlString(true);
}
Assert.AreEqual(keyValid, keyDER);
}
示例11: RegisterMessage
/// <summary>
/// konstruktor obiektu RegisterMessage
/// </summary>
/// <param name="name">nazwa uzytkownika</param>
/// <param name="password">haslo</param>
/// <param name="publicKey">klucz publiczny RSA</param>
/// <param name="registered">czy zarejestrowany</param>
public RegisterMessage(string name, string password, RSAParameters publicKey, bool registered)
{
username = name;
this.password = password;
this.publicKey = publicKey;
this.isRegistered = registered;
}
示例12: Encrypt2Base64
/// <summary>
/// 加密成base64字符串
/// </summary>
/// <param name="publicKey"></param>
/// <param name="content"></param>
/// <param name="size"></param>
/// <returns></returns>
public static string Encrypt2Base64(RSAParameters publicKey, string content, int size = 1024)
{
var rsa = new RSACryptoServiceProvider(size);
rsa.ImportParameters(publicKey);
var cipherbytes = rsa.Encrypt(content.ToUtf8Bytes(), false);
return cipherbytes.Bytes2Base64();
}
示例13: EncryptData
public string EncryptData(string data)
{
try
{
//initialze the byte arrays to the public key information.
byte[] PublicKey = _publicKey.ToArray();
byte[] Exponent = _exponent.ToArray();
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Create a new instance of RSAParameters.
RSAParameters RSAKeyInfo = new RSAParameters();
//Set RSAKeyInfo to the public key values.
RSAKeyInfo.Modulus = PublicKey;
RSAKeyInfo.Exponent = Exponent;
//Import key parameters into RSA.
RSA.ImportParameters(RSAKeyInfo);
var dataBytes = ASCIIEncoding.ASCII.GetBytes(data);
var encryptedBytes = RSA.Encrypt(dataBytes, false);
var encryptedValue = BitConverter.ToString(encryptedBytes).Replace("-", "").ToLower();
return encryptedValue;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
return null;
}
示例14: Decrypt
/// <summary>
/// 解密数据
/// </summary>
/// <param name="base64code">传入加密数据</param>
/// <returns>返回解密数据</returns>
static public string Decrypt(string base64code)
{
var a = new FileInfo("E:/100115_SignKey.pub").OpenRead();
var b = new BufferedStream(a);
//string c =
try
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString("");
RSAParameters rsaParameters = new RSAParameters();
rsaParameters.Exponent = Convert.FromBase64String("AQAB");
rsaParameters.Modulus =
Convert.FromBase64String(
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyq3xJ3jtuWSWk4nCCgysplqV3DyFGaF7iP7PO2vEUsgEq+vqKr+frlwji2n7A1TbpV7KhEGJIT9LW/9WCdBhlu6gnBdErtAA4Or43ol2K1BnY6VBcLWccloMd3YFHG8gOohCVIDbw863Wg0FNS27SM25U+XQfrNFaqBIa093WgAbwRIK06uzC01sW+soutvk+yAYBtbH7I7/1/dFixHKS2KN/7y3pvmXYBIRuBvn35IqwY3Gk0duEfbEr9F6wm2VKhS1zQG760FrHfhbXR+IN5nSTQBHBkw4QukLLvUqueKYfVdp2/2RCnY/At0bbOcA2tAPohDAfUDRdOZsFiTIMQID");
byte[] encryptedData;
byte[] decryptedData;
encryptedData = Convert.FromBase64String(base64code);
decryptedData = RSADeCrtypto(encryptedData, rsaParameters, true);
return ByteConverter.GetString(decryptedData);
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}
示例15: HashAndSignString
/// <summary>
/// Hash the data and generate signature
/// </summary>
/// <param name="dataToSign"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string HashAndSignString(string dataToSign, RSAParameters key)
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] signatureBytes = HashAndSignBytes(ByteConverter.GetBytes(dataToSign), key);
return ByteConverter.GetString(signatureBytes);
}