本文整理汇总了C#中RSAParameters类的典型用法代码示例。如果您正苦于以下问题:C# RSAParameters类的具体用法?C# RSAParameters怎么用?C# RSAParameters使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RSAParameters类属于命名空间,在下文中一共展示了RSAParameters类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteCore
/// <summary>
/// Writes a key to the specified stream.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="parameters">The RSA parameters of the key.</param>
protected override void WriteCore(Stream stream, RSAParameters parameters)
{
Requires.NotNull(stream, "stream");
var rootElement = new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Constructed,
Asn.BerTag.Sequence,
new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Constructed,
Asn.BerTag.Sequence,
new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Primitive,
Asn.BerTag.ObjectIdentifier,
Pkcs1KeyFormatter.RsaEncryptionObjectIdentifier),
new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Primitive,
Asn.BerTag.Null,
new byte[0])),
new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Primitive,
Asn.BerTag.BitString,
PrependLeadingZero(KeyFormatter.Pkcs1PrependZeros.Write(parameters, includePrivateKey: false), alwaysPrependZero: true)));
stream.WriteAsn1Element(rootElement);
}
示例2: TestData
static TestData()
{
RSAParameters rp = new RSAParameters();
rp.D = ("2806880f41dfba6ea9cb91f141c07e09cc0def786030162e1947c50d427d21dc5c0779ded52c50e570665884ba0ba32977c6"
+ "3019da0d255de458c9f421f0a17cd70bc21ea1e97152d3ded5ef1f17927bf2c03f83a72534033baacc670443d4e9c80e2d87"
+ "e206a3c3094ee5b20c3a1edf99c275f8f63cd4de7cdea326050cb151").HexToByteArray();
rp.DP = ("0aa6fc0436a24aa03c7a4d0b4cb84b75b9475eb0410ffaaa2a2c6d4dd8d4c3a5ac815bdeb93245babef613f983e4770d63d0"
+ "d931e33f0509019a1e431e6b5911").HexToByteArray();
rp.DQ = ("b7944d4d4846708c33adb0ad964623ad0e55d7c5bbd6475d25b12fbb39ab8c75794fdc977d67f54833ba59acbec8f3d91ddb"
+ "f29d0e780d52f8c656cad787fad5").HexToByteArray();
rp.Exponent = ("010001").HexToByteArray();
rp.InverseQ = ("8fdd8821b7fcc6e907436bc33d7311f9344ee18a3af36429c550f34f83c4c93fd0429f63bdc502db9cc03d3d857a6354e98b"
+ "db7c76b3ab54c32cdae75c539f2c").HexToByteArray();
rp.Modulus = ("c7b5012552672f812a015bf3356abdfe4964cfe2ae35b8aba819120c58ffa2f1fc0f512e76fd22e6d32646ceea78829a9cbb"
+ "2dbe5c66d14390e1bcef05afbababfe1f5ca07983b1f688a01b2beef8886b05df9e9420e65a1c0dc605ccfa2e27d84b39433"
+ "ffcd07441ef5be8ab80497bc553fce022c7620922d1d624b6e3babe1").HexToByteArray();
rp.P = ("c7eb601fdd49b22eda5b9a5ccb2fcfc35a660bb3bd2872857c864432e32916c2231e3b3da8afddc3efa38d04f9b1a08a08ab"
+ "08b4603ff28345ba32d24de3cfa5").HexToByteArray();
rp.Q = ("ffba608710355472b48b41e57eadd19a3f1a5d2fc1baa3d6210520c95694f11a065a16354827abdb06a59c3616f5ff2c5ca3"
+ "be835f1278e9a9e9f0373027b68d").HexToByteArray();
TestRsaKeyPair = rp;
}
示例3: WriteCore
/// <summary>
/// Writes the core.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="value">The value.</param>
protected override void WriteCore(Stream stream, RSAParameters value)
{
Requires.NotNull(stream, "stream");
var sequence = new MemoryStream();
if (KeyFormatter.HasPrivateKey(value))
{
// Only include the version element if this is a private key.
sequence.WriteAsn1Element(new Asn.DataElement(Asn.BerClass.Universal, Asn.BerPC.Primitive, Asn.BerTag.Integer, new byte[1]));
}
sequence.WriteAsn1Element(new Asn.DataElement(Asn.BerClass.Universal, Asn.BerPC.Primitive, Asn.BerTag.Integer, this.prependLeadingZeroOnCertainElements ? PrependLeadingZero(value.Modulus) : value.Modulus));
sequence.WriteAsn1Element(new Asn.DataElement(Asn.BerClass.Universal, Asn.BerPC.Primitive, Asn.BerTag.Integer, value.Exponent));
if (KeyFormatter.HasPrivateKey(value))
{
sequence.WriteAsn1Element(new Asn.DataElement(Asn.BerClass.Universal, Asn.BerPC.Primitive, Asn.BerTag.Integer, value.D));
sequence.WriteAsn1Element(new Asn.DataElement(Asn.BerClass.Universal, Asn.BerPC.Primitive, Asn.BerTag.Integer, this.prependLeadingZeroOnCertainElements ? PrependLeadingZero(value.P) : value.P));
sequence.WriteAsn1Element(new Asn.DataElement(Asn.BerClass.Universal, Asn.BerPC.Primitive, Asn.BerTag.Integer, this.prependLeadingZeroOnCertainElements ? PrependLeadingZero(value.Q) : value.Q));
sequence.WriteAsn1Element(new Asn.DataElement(Asn.BerClass.Universal, Asn.BerPC.Primitive, Asn.BerTag.Integer, this.prependLeadingZeroOnCertainElements ? PrependLeadingZero(value.DP) : value.DP));
sequence.WriteAsn1Element(new Asn.DataElement(Asn.BerClass.Universal, Asn.BerPC.Primitive, Asn.BerTag.Integer, value.DQ));
sequence.WriteAsn1Element(new Asn.DataElement(Asn.BerClass.Universal, Asn.BerPC.Primitive, Asn.BerTag.Integer, this.prependLeadingZeroOnCertainElements ? PrependLeadingZero(value.InverseQ) : value.InverseQ));
}
stream.WriteAsn1Element(new Asn.DataElement(Asn.BerClass.Universal, Asn.BerPC.Constructed, Asn.BerTag.Sequence, sequence.ToArray()));
}
示例4: WriteCore
/// <summary>
/// Writes a key to the specified stream.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="parameters">The RSA parameters of the key.</param>
protected override void WriteCore(Stream stream, RSAParameters parameters)
{
var rootElement = new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Constructed,
Asn.BerTag.Sequence,
new Asn.DataElement( // Version 0
Asn.BerClass.Universal,
Asn.BerPC.Primitive,
Asn.BerTag.Integer,
new byte[] { 0x00 }),
new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Constructed,
Asn.BerTag.Sequence,
new Asn.DataElement( // privateKeyAlgorithm
Asn.BerClass.Universal,
Asn.BerPC.Primitive,
Asn.BerTag.ObjectIdentifier,
Pkcs1KeyFormatter.RsaEncryptionObjectIdentifier),
new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Primitive,
Asn.BerTag.Null,
new byte[0])),
new Asn.DataElement( // rsaPrivateKey
Asn.BerClass.Universal,
Asn.BerPC.Primitive,
Asn.BerTag.OctetString,
KeyFormatter.Pkcs1PrependZeros.Write(parameters, HasPrivateKey(parameters))),
new Asn.DataElement(
Asn.BerClass.ContextSpecific,
Asn.BerPC.Constructed,
Asn.BerTag.EndOfContent,
new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Constructed,
Asn.BerTag.Sequence,
new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Primitive,
Asn.BerTag.ObjectIdentifier,
new byte[] { 0x55, 0x1d, 0x0f }),
new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Constructed,
Asn.BerTag.SetAndSetOf,
new Asn.DataElement(
Asn.BerClass.Universal,
Asn.BerPC.Primitive,
Asn.BerTag.BitString,
new byte[] { 0x00, 0x10 })))));
Asn.WriteAsn1Element(stream, rootElement);
}
示例5: VerifyCapiCompatibleParameters
/// <summary>
/// Throws an exception if the specified RSAParameters cannot be
/// serialized in the CAPI format.
/// </summary>
/// <param name="parameters">The RSA parameters.</param>
internal static void VerifyCapiCompatibleParameters(RSAParameters parameters)
{
try
{
KeyFormatter.VerifyFormat(IsCapiCompatible(parameters), "Private key parameters have lengths that are not supported by CAPI.");
}
catch (FormatException ex)
{
throw new NotSupportedException(ex.Message, ex);
}
}
示例6: ExportRsaParameters
internal static unsafe RSAParameters ExportRsaParameters(SafeRsaHandle key, bool includePrivateParameters)
{
Debug.Assert(
key != null && !key.IsInvalid,
"Callers should check the key is invalid and throw an exception with a message");
if (key == null || key.IsInvalid)
{
throw new CryptographicException();
}
RSAParameters rsaParameters;
bool addedRef = false;
try
{
key.DangerousAddRef(ref addedRef);
RSA_ST* rsaStructure = (RSA_ST*)key.DangerousGetHandle();
int modulusSize = RSA_size(key);
// RSACryptoServiceProvider expects P, DP, Q, DQ, and InverseQ to all
// be padded up to half the modulus size.
int halfModulus = modulusSize / 2;
rsaParameters = new RSAParameters
{
Modulus = ExtractBignum(rsaStructure->n, modulusSize),
Exponent = ExtractBignum(rsaStructure->e, 0),
};
if (includePrivateParameters)
{
rsaParameters.D = ExtractBignum(rsaStructure->d, modulusSize);
rsaParameters.P = ExtractBignum(rsaStructure->p, halfModulus);
rsaParameters.DP = ExtractBignum(rsaStructure->dmp1, halfModulus);
rsaParameters.Q = ExtractBignum(rsaStructure->q, halfModulus);
rsaParameters.DQ = ExtractBignum(rsaStructure->dmq1, halfModulus);
rsaParameters.InverseQ = ExtractBignum(rsaStructure->iqmp, halfModulus);
}
}
finally
{
if (addedRef)
{
key.DangerousRelease();
}
}
return rsaParameters;
}
示例7: PublicOnly_WithNoPrivate
public static void PublicOnly_WithNoPrivate()
{
using (var rsa = new RSACryptoServiceProvider())
{
RSAParameters publicParams = new RSAParameters
{
Modulus = TestData.RSA1024Params.Modulus,
Exponent = TestData.RSA1024Params.Exponent,
};
rsa.ImportParameters(publicParams);
Assert.True(rsa.PublicOnly);
}
}
示例8: FromSFSObject
public override bool FromSFSObject(Sfs2X.Entities.Data.ISFSObject data)
{
// We just pull the data straight out of the packet because we don't have a GetByteArray that works with encryption
bool retVal = false;
if (data.ContainsKey("key"))
{
ISFSObject publicKeyData = data.GetSFSObject("key");
var tempParams = new RSAParameters();
tempParams.Modulus = publicKeyData.GetByteArray("mod").Bytes;
tempParams.Exponent = publicKeyData.GetByteArray("exp").Bytes;
parameters = tempParams;
}
return retVal;
}
示例9: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
var code = Request.QueryString["code"];
//send request to github server to get access token
HttpWebRequest req = WebRequest.Create("https://github.com/login/oauth/access_token?client_id=TODO:<your own client id>&client_secret=TODO:<your own client secret>&code=" + code) as HttpWebRequest;
req.Method = "POST";
HttpWebResponse rsps = req.GetResponse() as HttpWebResponse;
var str = new StreamReader(rsps.GetResponseStream()).ReadToEnd();
Match m = Regex.Match(str, "access_token=([^&]+)&token_type=([^&]+)");
//RSA encrypt access token with public key from browser side
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
RSAParameters publicKey = new RSAParameters();
publicKey.Modulus = literal2bytes(Request.Cookies["modulus"].Value);
publicKey.Exponent = literal2bytes(Request.Cookies["exponent"].Value);
rsa.ImportParameters(publicKey);
byte[] result = rsa.Encrypt(Encoding.UTF8.GetBytes(m.Groups[1].ToString()), false);
StringBuilder access_token = new StringBuilder();
for (var i = 0; i < result.Length; i++)
{
access_token.Append(result[i].ToString("x2"));
}
//write encrypted access_token back into cookie
HttpCookie cookie = new HttpCookie("access_token");
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, 0, 0);
cookie.Expires = dt.Add(ts);
cookie.Value = access_token.ToString();
Response.AppendCookie(cookie);
cookie = new HttpCookie("token_type");
dt = DateTime.Now;
ts = new TimeSpan(0, 0, 0, 0);
cookie.Expires = dt.Add(ts);
cookie.Value = m.Groups[2].ToString();
Response.AppendCookie(cookie);
//now jump back, only the browser side could decrypt the access_token from cookie
Response.Redirect("TODO:<your own address>");
}
示例10: RSAEncrypt
public static byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo)
{
try
{
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAKeyInfo);
encryptedData = RSA.Encrypt(DataToEncrypt, false);
}
return encryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
示例11: ExportRsaParameters
internal static RSAParameters ExportRsaParameters(SafeRsaHandle key, bool includePrivateParameters)
{
Debug.Assert(
key != null && !key.IsInvalid,
"Callers should check the key is invalid and throw an exception with a message");
if (key == null || key.IsInvalid)
{
throw new CryptographicException();
}
IntPtr n, e, d, p, dmp1, q, dmq1, iqmp;
if (!GetRsaParameters(key, out n, out e, out d, out p, out dmp1, out q, out dmq1, out iqmp))
{
throw new CryptographicException();
}
int modulusSize = Crypto.RsaSize(key);
// RSACryptoServiceProvider expects P, DP, Q, DQ, and InverseQ to all
// be padded up to half the modulus size.
int halfModulus = modulusSize / 2;
RSAParameters rsaParameters = new RSAParameters
{
Modulus = Crypto.ExtractBignum(n, modulusSize),
Exponent = Crypto.ExtractBignum(e, 0),
};
if (includePrivateParameters)
{
rsaParameters.D = Crypto.ExtractBignum(d, modulusSize);
rsaParameters.P = Crypto.ExtractBignum(p, halfModulus);
rsaParameters.DP = Crypto.ExtractBignum(dmp1, halfModulus);
rsaParameters.Q = Crypto.ExtractBignum(q, halfModulus);
rsaParameters.DQ = Crypto.ExtractBignum(dmq1, halfModulus);
rsaParameters.InverseQ = Crypto.ExtractBignum(iqmp, halfModulus);
}
return rsaParameters;
}
示例12: RSAKeyToASN1
///
/// SEQUENCE (a)
/// +- INTEGER (V) // Version - 0 (v1998)
/// +- SEQUENCE (b)
/// | +- OID (oid) // 1.2.840.113549.1.1.1
/// | +- Nil (c)
/// +- OCTETSTRING(PRVKY) (os) // Private Key Parameter
///
/// However, OCTETSTRING(PRVKY) wraps
/// SEQUENCE(
/// INTEGER(0) // Version - 0 (v1998)
/// INTEGER(N)
/// INTEGER(E)
/// INTEGER(D)
/// INTEGER(P)
/// INTEGER(Q)
/// INTEGER(DP)
/// INTEGER(DQ)
/// INTEGER(InvQ)
/// )
public static byte[] RSAKeyToASN1(RSAParameters PrivateKey) {
ASN1 v = ASN1Convert.FromUnsignedBigInteger(new byte[] {0});
ASN1 b = PKCS7.AlgorithmIdentifier ("1.2.840.113549.1.1.1");
ASN1 os = new ASN1(0x30);
os.Add(ASN1Convert.FromUnsignedBigInteger(new byte[] {0}));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.Modulus));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.Exponent));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.D));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.P));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.Q));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.DP));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.DQ));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.InverseQ));
ASN1 pem = new ASN1(0x30);
pem.Add(v);
pem.Add(b);
// Make this into an OCTET string
pem.Add(new ASN1(0x04, os.GetBytes()));
return pem.GetBytes();
}
示例13: CreateKeyPair
/// <inheritdoc/>
public ICryptographicKey CreateKeyPair(int keySize)
{
Requires.Range(keySize > 0, "keySize");
var keyGen = KeyPairGenerator.GetInstance("RSA");
keyGen.Initialize(keySize);
var key = keyGen.GenerateKeyPair();
var privateKeyParameters = key.Private.JavaCast<IRSAPrivateCrtKey>();
var parameters = new RSAParameters
{
Modulus = privateKeyParameters.Modulus.ToByteArray(),
Exponent = privateKeyParameters.PublicExponent.ToByteArray(),
P = privateKeyParameters.PrimeP.ToByteArray(),
Q = privateKeyParameters.PrimeQ.ToByteArray(),
DP = privateKeyParameters.PrimeExponentP.ToByteArray(),
DQ = privateKeyParameters.PrimeExponentQ.ToByteArray(),
InverseQ = privateKeyParameters.CrtCoefficient.ToByteArray(),
D = privateKeyParameters.PrivateExponent.ToByteArray(),
};
return new RsaCryptographicKey(key.Public, key.Private, parameters, this.algorithm);
}
示例14: IsCapiCompatible
/// <summary>
/// Determines whether the specified RSA parameters
/// can be represented in the CAPI format.
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <returns><c>true</c> if CAPI is compatible with these parameters; <c>false</c> otherwise.</returns>
internal static bool IsCapiCompatible(RSAParameters parameters)
{
// Only private keys have this restriction.
if (!KeyFormatter.HasPrivateKey(parameters))
{
return true;
}
int halfModulusLength = (parameters.Modulus.Length + 1) / 2;
// These are the same assertions that Windows crypto lib itself
// follows when it returns 'bad data'.
// CAPI's file format does not include lengths for parameters.
// Instead it makes some assumptions about their relative lengths
// which make it fundamentally incompatible with some private keys
// generated by iOS.
return
halfModulusLength == parameters.P.Length &&
halfModulusLength == parameters.Q.Length &&
halfModulusLength == parameters.DP.Length &&
halfModulusLength == parameters.DQ.Length &&
halfModulusLength == parameters.InverseQ.Length &&
parameters.Modulus.Length == parameters.D.Length;
}
示例15: ValidateParameters
internal static void ValidateParameters(ref RSAParameters rsaParams)
{
Assert.NotNull(rsaParams.Modulus);
Assert.NotNull(rsaParams.Exponent);
// Key compatibility: RSA as an algorithm is achievable using just N (Modulus),
// E (public Exponent) and D (private exponent). Having all of the breakdowns
// of D make the algorithm faster, and shipped versions of RSACryptoServiceProvider
// have thrown if D is provided and the rest of the private key values are not.
// So, here we're going to assert that none of them were null for private keys.
if (rsaParams.D == null)
{
Assert.Null(rsaParams.P);
Assert.Null(rsaParams.DP);
Assert.Null(rsaParams.Q);
Assert.Null(rsaParams.DQ);
Assert.Null(rsaParams.InverseQ);
}
else
{
Assert.NotNull(rsaParams.P);
Assert.NotNull(rsaParams.DP);
Assert.NotNull(rsaParams.Q);
Assert.NotNull(rsaParams.DQ);
Assert.NotNull(rsaParams.InverseQ);
}
}