Example #1
0
        public static RSA FromXml(string xmlKey)
        {
            var rsa = RSA.Create();
            var ps  = RSAParametersConvert.XmlToRSAParameters(xmlKey);

            rsa.ImportParameters(ps);
            return(rsa);
        }
Example #2
0
        public static RSA FromPem(string pemKey)
        {
            var rsa = RSA.Create();
            var ps  = RSAParametersConvert.PemToRSAParameters(pemKey);

            rsa.ImportParameters(ps);
            return(rsa);
        }
Example #3
0
    /// <summary>
    /// Sets a specific crypto service provider instance.
    /// </summary>
    /// <param name="privateKey">The RSA private key.</param>
    /// <param name="syncEncryptKey">true if set the secret encryption key from the crypto service provider; otherwise, false.</param>
    public void SetCrypto(string privateKey, bool syncEncryptKey = false)
    {
        var key = RSAParametersConvert.Parse(privateKey);

        if (!key.HasValue)
        {
            ClearCrypto(syncEncryptKey);
            return;
        }

        SetCrypto(key.Value, syncEncryptKey);
    }
Example #4
0
    /// <summary>
    /// Creates an RSA hash signature provider.
    /// </summary>
    /// <param name="secret">The RSA key.</param>
    /// <param name="hashAlgorithmName">The hash algorithm name.</param>
    /// <param name="signAlgorithmName">The signature algorithm name.</param>
    /// <returns>An RSA hash signature provider instance.</returns>
    private static RSASignatureProvider Create(string secret, HashAlgorithmName hashAlgorithmName, string signAlgorithmName)
    {
        if (string.IsNullOrWhiteSpace(secret))
        {
            throw new ArgumentNullException(nameof(secret), "secret should not be null, empty or consists only of white-space characters.");
        }
        var p = RSAParametersConvert.Parse(secret);

        if (p == null)
        {
            throw new FormatException("secret is not a valid RSA key. A PEM string or XML string expected.");
        }
        return(new RSASignatureProvider(p.Value, hashAlgorithmName, signAlgorithmName));
    }
Example #5
0
 /// <summary>
 /// Gets the secret encrypted.
 /// </summary>
 /// <param name="key">The secret encryption key to use to override the original one set.</param>
 /// <param name="padding">The optional padding mode for decryption.</param>
 /// <returns>The Base64 string with secret encrypted.</returns>
 public string EncryptSecret(string key, RSAEncryptionPadding padding = null)
 {
     return(EncryptSecret(RSAParametersConvert.Parse(key), padding));
 }
Example #6
0
 public static string ToPem(this RSA rsa, bool includePrivateKey, bool isPkcs8)
 {
     return(RSAParametersConvert.ToPem(rsa.ExportParameters(includePrivateKey), includePrivateKey, isPkcs8));
 }
Example #7
0
 public static string ToXml(this RSA rsa, bool includePrivateKey)
 {
     return(RSAParametersConvert.ToXml(rsa.ExportParameters(includePrivateKey), includePrivateKey));
 }
Example #8
0
        public override void Process()
        {
            var codeTokenReq = new CodeTokenRequest(new CodeTokenRequestBody
            {
                Code = "hijklmn\r\nopq\trst"
            }, "abcd", "efg")
            {
                ScopeString = "test plain"
            };
            var tokenUrl = codeTokenReq.ToJsonString();

            codeTokenReq = CodeTokenRequest.Parse(tokenUrl);
            tokenUrl     = codeTokenReq.ToQueryData().ToString();
            codeTokenReq = CodeTokenRequest.Parse(tokenUrl);
            tokenUrl     = codeTokenReq.ToJsonString();
            codeTokenReq = CodeTokenRequest.Parse(tokenUrl);
            ConsoleLine.WriteLine(codeTokenReq.ToQueryData().ToString());
            ConsoleLine.WriteLine();

            // JWT HS512
            var hs  = HashSignatureProvider.CreateHS512("a secret string");
            var jwt = new JsonWebToken <HttpClientVerb.NameAndDescription>(new HttpClientVerb.NameAndDescription
            {
                Name        = "abcd",
                Description = "efg"
            }, hs);
            var header = jwt.ToAuthenticationHeaderValue();

            jwt = JsonWebToken <HttpClientVerb.NameAndDescription> .Parse(header.ToString(), hs);

            var jwtStr = jwt.ToEncodedString();

            ConsoleLine.WriteLine(jwtStr != header.Parameter ? "Failed JWT HS512 testing." : jwtStr);
            ConsoleLine.WriteLine();

            // RSA.
            var rsa        = RSA.Create();
            var privateKey = rsa.ExportParameters(true).ToPrivatePEMString(true);

            ConsoleLine.WriteLine(privateKey);
            var publicKey = rsa.ExportParameters(false).ToPublicPEMString();

            ConsoleLine.WriteLine(publicKey);
            var privateKeyP = RSAParametersConvert.Parse(privateKey).Value;
            var privateKeyS = privateKeyP.ToPrivatePEMString(true);
            var publicKeyP  = RSAParametersConvert.Parse(publicKey).Value;
            var publicKeyS  = publicKeyP.ToPublicPEMString();

            ConsoleLine.WriteLine("They are {0}.", (privateKey == privateKeyS) && (publicKey == publicKeyS) ? "same" : "different");
            ConsoleLine.WriteLine();

            // JWT RS512
            using (var rs = RSASignatureProvider.CreateRS512(rsa))
            {
                jwt    = new JsonWebToken <HttpClientVerb.NameAndDescription>(jwt.Payload, rs);
                header = jwt.ToAuthenticationHeaderValue();
                jwt    = JsonWebToken <HttpClientVerb.NameAndDescription> .Parse(header.ToString(), rs);

                jwtStr = jwt.ToEncodedString();
                ConsoleLine.WriteLine(jwtStr != header.Parameter ? "Failed JWT RS512 testing." : header.Parameter);
                ConsoleLine.WriteLine();
            }
        }