/// <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); }
/// <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)); }
/// <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)); }
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(); } }