本文整理汇总了C#中System.Security.Cryptography.RSA.ExportParameters方法的典型用法代码示例。如果您正苦于以下问题:C# RSA.ExportParameters方法的具体用法?C# RSA.ExportParameters怎么用?C# RSA.ExportParameters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RSA
的用法示例。
在下文中一共展示了RSA.ExportParameters方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RsaKeyIdentifierClause
public RsaKeyIdentifierClause(RSA rsa)
: base(clauseType)
{
if (rsa == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rsa");
this.rsa = rsa;
this.rsaParameters = rsa.ExportParameters(false);
}
示例2: Matches
public bool Matches(RSA rsa)
{
if (rsa == null)
{
return false;
}
RSAParameters parameters = rsa.ExportParameters(false);
return (System.IdentityModel.SecurityUtils.MatchesBuffer(this.rsaParameters.Modulus, parameters.Modulus) && System.IdentityModel.SecurityUtils.MatchesBuffer(this.rsaParameters.Exponent, parameters.Exponent));
}
示例3: Matches
public bool Matches(RSA rsa)
{
if (rsa == null)
return false;
RSAParameters rsaParameters = rsa.ExportParameters(false);
return SecurityUtils.MatchesBuffer(this.rsaParameters.Modulus, rsaParameters.Modulus) &&
SecurityUtils.MatchesBuffer(this.rsaParameters.Exponent, rsaParameters.Exponent);
}
示例4: VerifyRsaPKCS1
public static bool VerifyRsaPKCS1(RSA key, byte[] signature, byte[] hash, bool allowNoPadding)
{
var parameters = key.ExportParameters(false);
var e = Utils.BigIntegerFromBigEndian(parameters.Exponent, 0, parameters.Exponent.Length);
var mod = Utils.BigIntegerFromBigEndian(parameters.Modulus, 0, parameters.Modulus.Length);
var m = Utils.BigIntegerFromBigEndian(signature, 0, signature.Length);
var decryptedArr = Utils.BigEndianFromBigInteger(BigInteger.ModPow(m, e, mod));
/*
PKCS padding used in TLS 1.0/TLS 1.1:
00 01 [k-3-hashlen 0xff bytes] 00 (hash)
OR, for only TLS 1.0, there may be no padding (or equivalently, 00 00 [k-3-hashlen 00 bytes] 00 (hash))
where k is the keylen
*/
if (allowNoPadding && decryptedArr.Length <= hash.Length)
{
int zeros = hash.Length - decryptedArr.Length;
for (var i = 0; i < zeros; i++)
{
if (hash[i] != 0)
return false;
}
return Utils.ArraysEqual(decryptedArr, 0, hash, zeros, hash.Length - zeros);
}
if (decryptedArr.Length != parameters.Modulus.Length - 1)
return false;
if (decryptedArr[0] != 1)
return false;
for (var i = 1; i < decryptedArr.Length - hash.Length - 1; i++)
{
if (decryptedArr[i] != 0xff)
return false;
}
if (decryptedArr[decryptedArr.Length - hash.Length - 1] != 0)
return false;
return Utils.ArraysEqual(decryptedArr, decryptedArr.Length - hash.Length, hash, 0, hash.Length);
}
示例5: CreateJWK
private static JsonWebKey CreateJWK(RSA rsa)
{
if (rsa == null)
throw new ArgumentNullException("rsa");
RSAParameters rsaParameters = rsa.ExportParameters(true);
var webKey = new JsonWebKey()
{
Kty = JsonWebKeyType.Rsa,
E = rsaParameters.Exponent,
N = rsaParameters.Modulus,
D = rsaParameters.D,
DP = rsaParameters.DP,
DQ = rsaParameters.DQ,
QI = rsaParameters.InverseQ,
P = rsaParameters.P,
Q = rsaParameters.Q
};
return webKey;
}
示例6: SignRsaPKCS1
public static byte[] SignRsaPKCS1(RSA key, byte[] hash)
{
// NOTE: The X509Certificate2 must be initialized with the X509KeyStorageFlags.Exportable flag
var parameters = key.ExportParameters(true);
var dp = Utils.BigIntegerFromBigEndian(parameters.DP, 0, parameters.DP.Length);
var dq = Utils.BigIntegerFromBigEndian(parameters.DQ, 0, parameters.DQ.Length);
var qinv = Utils.BigIntegerFromBigEndian(parameters.InverseQ, 0, parameters.InverseQ.Length);
var p = Utils.BigIntegerFromBigEndian(parameters.P, 0, parameters.P.Length);
var q = Utils.BigIntegerFromBigEndian(parameters.Q, 0, parameters.Q.Length);
var data = new byte[parameters.D.Length - 1];
data[0] = 1;
for (var i = 1; i < data.Length - hash.Length - 1; i++)
{
data[i] = 0xff;
}
data[data.Length - hash.Length - 1] = 0;
Buffer.BlockCopy(hash, 0, data, data.Length - hash.Length, hash.Length);
var m = Utils.BigIntegerFromBigEndian(data, 0, data.Length);
var m1 = BigInteger.ModPow(m, dp, p);
var m2 = BigInteger.ModPow(m, dq, q);
var h = qinv * (m1 - m2) % p;
if (h.Sign == -1)
h += p;
var signature = Utils.BigEndianFromBigInteger(m2 + h * q);
Utils.ClearArray(parameters.D);
Utils.ClearArray(parameters.DP);
Utils.ClearArray(parameters.DQ);
Utils.ClearArray(parameters.InverseQ);
Utils.ClearArray(parameters.P);
Utils.ClearArray(parameters.Q);
return signature;
}
示例7: GetRsaPublicKey
public static RsaKeyParameters GetRsaPublicKey(
RSA rsa)
{
return GetRsaPublicKey(rsa.ExportParameters(false));
}
示例8: GetRsaKeyPair
public static AsymmetricCipherKeyPair GetRsaKeyPair(
RSA rsa)
{
return GetRsaKeyPair(rsa.ExportParameters(true));
}
示例9: JsonWebKey
/// <summary>
/// Converts a RSA object to a WebKey of type RSA.
/// </summary>
/// <param name="rsaProvider">The RSA object to convert</param>
/// <param name="includePrivateParameters">True to include the RSA private key parameters</param>
/// <returns>A WebKey representing the RSA object</returns>
public JsonWebKey( RSA rsaProvider, bool includePrivateParameters = false )
: this( rsaProvider.ExportParameters( includePrivateParameters ) )
{
}
示例10: getModulus
public static byte[] getModulus(RSA rsa)
{
return rsa.ExportParameters(false).Modulus;
}
示例11: ComputeCombinedId
private static string ComputeCombinedId(RSA issuerKey, string claimValue) {
Requires.NotNull(issuerKey, "issuerKey");
Requires.NotNull(claimValue, "claimValue");
Contract.Ensures(Contract.Result<string>() != null);
int nameLength = Encoding.UTF8.GetByteCount(claimValue);
RSAParameters rsaParams = issuerKey.ExportParameters(false);
byte[] shaInput;
byte[] shaOutput;
int i = 0;
shaInput = new byte[rsaParams.Modulus.Length + rsaParams.Exponent.Length + nameLength];
rsaParams.Modulus.CopyTo(shaInput, i);
i += rsaParams.Modulus.Length;
rsaParams.Exponent.CopyTo(shaInput, i);
i += rsaParams.Exponent.Length;
i += Encoding.UTF8.GetBytes(claimValue, 0, claimValue.Length, shaInput, i);
using (SHA256 sha = SHA256.Create()) {
shaOutput = sha.ComputeHash(shaInput);
}
return Convert.ToBase64String(shaOutput);
}
示例12: ToAsn1Key
public static ASN1 ToAsn1Key(RSA rsa)
{
EnsureNotNull(rsa, "rsa");
RSAParameters parameters = rsa.ExportParameters(false);
ASN1 asnKey = new ASN1(0x30);
asnKey.Add(ASN1Convert.FromUnsignedBigInteger(parameters.Modulus));
asnKey.Add(ASN1Convert.FromUnsignedBigInteger(parameters.Exponent));
return asnKey;
}
示例13: ToCapiPrivateKeyBlob
public static byte[] ToCapiPrivateKeyBlob(RSA rsa) {
RSAParameters p = rsa.ExportParameters(true);
int keyLength = p.Modulus.Length; // in bytes
var blob = new byte[20 + (keyLength << 2) + (keyLength >> 1)];
blob[0] = 0x07; // Type - PRIVATEKEYBLOB (0x07)
blob[1] = 0x02; // Version - Always CUR_BLOB_VERSION (0x02)
// [2], [3] // RESERVED - Always 0
blob[5] = 0x24; // ALGID - Always 00 24 00 00 (for CALG_RSA_SIGN)
blob[8] = 0x52; // Magic - RSA2 (ASCII in hex)
blob[9] = 0x53;
blob[10] = 0x41;
blob[11] = 0x32;
byte[] bitlen = GetBytesLE(keyLength << 3);
blob[12] = bitlen[0]; // bitlen
blob[13] = bitlen[1];
blob[14] = bitlen[2];
blob[15] = bitlen[3];
// public exponent (DWORD)
int pos = 16;
int n = p.Exponent.Length;
while (n > 0) {
blob[pos++] = p.Exponent[--n];
}
// modulus
pos = 20;
byte[] part = p.Modulus;
int len = part.Length;
Array.Reverse(part, 0, len);
Buffer.BlockCopy(part, 0, blob, pos, len);
pos += len;
// private key
part = p.P;
len = part.Length;
Array.Reverse(part, 0, len);
Buffer.BlockCopy(part, 0, blob, pos, len);
pos += len;
part = p.Q;
len = part.Length;
Array.Reverse(part, 0, len);
Buffer.BlockCopy(part, 0, blob, pos, len);
pos += len;
part = p.DP;
len = part.Length;
Array.Reverse(part, 0, len);
Buffer.BlockCopy(part, 0, blob, pos, len);
pos += len;
part = p.DQ;
len = part.Length;
Array.Reverse(part, 0, len);
Buffer.BlockCopy(part, 0, blob, pos, len);
pos += len;
part = p.InverseQ;
len = part.Length;
Array.Reverse(part, 0, len);
Buffer.BlockCopy(part, 0, blob, pos, len);
pos += len;
part = p.D;
len = part.Length;
Array.Reverse(part, 0, len);
Buffer.BlockCopy(part, 0, blob, pos, len);
return blob;
}
示例14: getExponent
public static byte[] getExponent(RSA rsa)
{
return rsa.ExportParameters(false).Exponent;
}
示例15: ComputeCombinedId
/// <summary>
/// Does the actual calculation of a combined ID from a value and an RSA key.
/// </summary>
/// <param name="issuerKey">The key of the issuer of the token</param>
/// <param name="claimValue">the claim value to hash with.</param>
/// <returns></returns>
public static string ComputeCombinedId(RSA issuerKey, string claimValue)
{
int nameLength = Encoding.UTF8.GetByteCount(claimValue);
RSAParameters rsaParams = issuerKey.ExportParameters(false);
byte[] shaInput;
byte[] shaOutput;
int i = 0;
shaInput = new byte[rsaParams.Modulus.Length + rsaParams.Exponent.Length + nameLength];
rsaParams.Modulus.CopyTo(shaInput, i);
i += rsaParams.Modulus.Length;
rsaParams.Exponent.CopyTo(shaInput, i);
i += rsaParams.Exponent.Length;
i += Encoding.UTF8.GetBytes(claimValue, 0, claimValue.Length, shaInput, i);
using (SHA256 sha = SHA256.Create())
{
shaOutput = sha.ComputeHash(shaInput);
}
return Convert.ToBase64String(shaOutput);
}