public static string Encrypt(this object value, string cryptoKey, EncryptionType type = EncryptionType.TripleDes, bool utf8 = false) { if (value.IsNull()) { return(null); } string result = null; switch (type) { case EncryptionType.TripleDes: result = TripleDesEncryption.Encrypt(cryptoKey, value.ToString(), utf8); break; } result = WebUtility.UrlEncode(result); return(result); }
public async Task <ActionResult> ShowSecret(ShowSecretViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var adminStore = new IdentityServer3AdminStore(); ProtectedSecretQueryValues queryValues = new ProtectedSecretQueryValues() { ClientId = model.ClientId, Value = model.Hash }; var record = await adminStore.FindSecretProtectedValue(queryValues); var myCrypto = new TripleDesEncryption(model.PassCode); model.OpenSecret = myCrypto.Decrypt(record.ProtectedValue); return(RedirectToAction("ShowOpenSecret", new { clientId = model.ClientId, hash = model.Hash, openSecret = model.OpenSecret, passCode = model.PassCode })); }
public async Task <ActionResult> Secret(SecretViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var adminStore = new IdentityServer3AdminStore(); var myCrypto = new TripleDesEncryption(model.PassCode); var protectedClientSecret = myCrypto.Encrypt(model.OpenClientSecret); Secret secret = null; if (string.Compare(model.SecretType, "SharedSecret", StringComparison.OrdinalIgnoreCase) == 0) { var hashedClientSecret = model.OpenClientSecret.Sha256(); secret = new Secret(hashedClientSecret) { Type = model.SecretType }; ProtectedSecretHandle protectedSecretHandle = new ProtectedSecretHandle() { ClientId = model.ClientId, Value = hashedClientSecret, ProtectedValue = protectedClientSecret }; await adminStore.AddSecretProtectedValue(protectedSecretHandle); } if (string.Compare(model.SecretType, "X509Thumbprint", StringComparison.OrdinalIgnoreCase) == 0) { secret = new Secret(model.ThumbPrint) { Type = model.SecretType }; } var secrets = new List <Secret> { secret }; await adminStore.AddClientSecretsToClientAsync(model.ClientId, secrets); return(RedirectToAction("Index")); }
public async Task Test_Add_Protected_Secret_Async() { var dao = new IdentityServer3CassandraDao(); await dao.EstablishConnectionAsync(); var value = Guid.NewGuid().ToString(); var valueProtected = Guid.NewGuid().ToString(); TripleDesEncryption tde = new TripleDesEncryption("test"); var eValueProtected = tde.Encrypt(valueProtected); ProtectedSecretHandle handle = new ProtectedSecretHandle() { ClientId = Guid.NewGuid().ToString(), Value = value, ProtectedValue = eValueProtected }; await dao.AddSecretProtectedValue(handle); ProtectedSecretQueryValues queryValues = new ProtectedSecretQueryValues() { ClientId = handle.ClientId, Value = handle.Value }; var record = await dao.FindSecretProtectedValue(queryValues); var fetchedValueProtected = tde.Decrypt(record.ProtectedValue); Assert.AreEqual(valueProtected, fetchedValueProtected); await dao.DeleteSecretProtectedValue(queryValues); record = await dao.FindSecretProtectedValue(queryValues); Assert.IsNull(record); }
private static void TestTripleDES() { var trippleDes = new TripleDesEncryption(); // encrypt with key 1, then encrypt with key 2 and finally encrypt with key 3 var key = trippleDes.GenerateRandomNumber(24); // encrypt with key 1, then encrypt with key 2 and finally encrypt again with key 1 //var key = trippleDes.GenerateRandomNumber(16); var iv = trippleDes.GenerateRandomNumber(8); const string originalText = "Text to encrypt"; var encrypted = trippleDes.Encrypt(Encoding.UTF8.GetBytes(originalText), key, iv); var decrypted = trippleDes.Decrypt(encrypted, key, iv); var decryptedMessage = Encoding.UTF8.GetString(decrypted); Console.WriteLine($"Original Text: {originalText}"); Console.WriteLine($"Encrypted value: {Convert.ToBase64String(encrypted)}"); Console.WriteLine($"Decrypted Value: {Convert.ToBase64String(decrypted)}"); Console.WriteLine($"Decrypted Text: {decryptedMessage}"); }
public static string BuildUrlForRedirect(string currentUrl, string standardUrl, string secureUrl, SSLRedirectTo redirectTo, string sessionId, string cartId, string currentSessionId, string currentCartId, bool useClearText ) { string url = string.Empty; RemoveAllEncoding(ref currentUrl); RemoveAllEncoding(ref standardUrl); RemoveAllEncoding(ref secureUrl); TripleDesEncryption enc = new TripleDesEncryption(); switch (redirectTo) { case SSLRedirectTo.NonSSL: url = UrlRewriter.SwitchUrlToStandard(currentUrl); break; case SSLRedirectTo.SSL: url = UrlRewriter.SwitchUrlToSecure(currentUrl); break; } //if (differentTld) { // Uri temp = new Uri(url); // NameValueCollection queryString = HttpUtility.ParseQueryString(temp.Query); // object obj = queryString.GetValues(sessionId); // if (!string.IsNullOrEmpty(currentSessionId.Trim())) { // string sesval = currentSessionId; // if ((!useClearText)) { // sesval = enc.Encode(currentSessionId); // } // if (obj != null) { // queryString[sessionId] = sesval; // } // else { // queryString.Add(sessionId, sesval); // } // } // obj = queryString.GetValues(cartId); // if (!string.IsNullOrEmpty(currentCartId.Trim())) { // string cidval = currentCartId; // if ((!useClearText)) { // cidval = enc.Encode(currentCartId); // } // if (obj != null) { // queryString[cartId] = cidval; // } // else { // queryString.Add(cartId, cidval); // } // } // if (!string.IsNullOrEmpty(temp.Query)) { // url = temp.AbsoluteUri.Replace(temp.Query, ""); // } // else { // url = temp.AbsoluteUri; // } // if (queryString.HasKeys()) { // url = url + "?"; // foreach (string item in queryString.AllKeys) { // url = url + item + "=" + HttpUtility.UrlEncode(queryString[item]) + "&"; // } // url = url.TrimEnd('&'); // } //} return url; }
public static string BuildUrlForRedirect(string currentUrl, string standardUrl, string secureUrl, SSLRedirectTo redirectTo, string sessionId, string cartId, string currentSessionId, string currentCartId, bool useClearText ) { string url = string.Empty; RemoveAllEncoding(ref currentUrl); RemoveAllEncoding(ref standardUrl); RemoveAllEncoding(ref secureUrl); TripleDesEncryption enc = new TripleDesEncryption(); switch (redirectTo) { case SSLRedirectTo.NonSSL: url = UrlRewriter.SwitchUrlToStandard(currentUrl); break; case SSLRedirectTo.SSL: url = UrlRewriter.SwitchUrlToSecure(currentUrl); break; } //if (differentTld) { // Uri temp = new Uri(url); // NameValueCollection queryString = HttpUtility.ParseQueryString(temp.Query); // object obj = queryString.GetValues(sessionId); // if (!string.IsNullOrEmpty(currentSessionId.Trim())) { // string sesval = currentSessionId; // if ((!useClearText)) { // sesval = enc.Encode(currentSessionId); // } // if (obj != null) { // queryString[sessionId] = sesval; // } // else { // queryString.Add(sessionId, sesval); // } // } // obj = queryString.GetValues(cartId); // if (!string.IsNullOrEmpty(currentCartId.Trim())) { // string cidval = currentCartId; // if ((!useClearText)) { // cidval = enc.Encode(currentCartId); // } // if (obj != null) { // queryString[cartId] = cidval; // } // else { // queryString.Add(cartId, cidval); // } // } // if (!string.IsNullOrEmpty(temp.Query)) { // url = temp.AbsoluteUri.Replace(temp.Query, ""); // } // else { // url = temp.AbsoluteUri; // } // if (queryString.HasKeys()) { // url = url + "?"; // foreach (string item in queryString.AllKeys) { // url = url + item + "=" + HttpUtility.UrlEncode(queryString[item]) + "&"; // } // url = url.TrimEnd('&'); // } //} return(url); }