public void TestSymetrical() { const string original = "this is a test string"; using (var aes = new SimpleAes()) { var enc = aes.Encrypt(original); Debug.WriteLine("encrypted = " + enc); string decrypted = aes.Decrypt(enc); Assert.AreEqual(original, decrypted); var secondEnc = aes.Encrypt(original); Assert.AreNotEqual(secondEnc, enc); } }
private void SetInternal(string key, object value, DateTime?expiresAt) { try { if (_directoryValid && !HasExceededQuota()) { //check for a non-expiring cache: var cachePath = GetFilePath(key); if (File.Exists(cachePath)) { File.Delete(cachePath); } //check for other caches: var fileName = GetFileNameSearchPattern(key); var existingCaches = Directory.EnumerateFiles(_directory, fileName); foreach (var existingCache in existingCaches) { File.Delete(Path.Combine(_directory, existingCache)); } var newCachePath = GetFilePath(key, expiresAt); var item = value as string; if (item != null) { if (CacheConfiguration.Current.DiskCache.EncryptItems) { var encryptedBytes = SimpleAes.Encrypt(item); File.WriteAllBytes(newCachePath, encryptedBytes); } else { File.WriteAllText(newCachePath, item); } } } } catch (Exception ex) { Log.Warn("DiskCache.SetInternal - failed, item not cached. Message: {0}", ex.Message); } }
private static HttpResponseMessage Post(object data, RequestClaim claim, string base64Secret, TextWriter message, TextWriter error) { var type = data.GetType(); var ie = type.GetInterfaces().FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable <>)); if (ie == null) { if (!ValidateAttributes.IsValid(data, error)) { return(null); } } else { type = ie.GetGenericArguments()[0]; foreach (object o in (IEnumerable)data) { if (!ValidateAttributes.IsValid(o, error)) { return(null); } } } var baseAddress = new Uri(_domain); using (var handler = new HttpClientHandler()) { using (var client = new HttpClient(handler) { BaseAddress = baseAddress }) { client.DefaultRequestHeaders.Date = new DateTimeOffset(DateTime.Now); using (var aes = new SimpleAes()) { claim.Token = Hash(client.DefaultRequestHeaders.GetValues("Date").First(), claim.ResourceId, base64Secret); var jsonClaim = JsonConvert.SerializeObject(claim); var encryptedString = aes.Encrypt(jsonClaim); var cookieContainer = new CookieContainer(); cookieContainer.Add(baseAddress, new Cookie("token", encryptedString)); handler.CookieContainer = cookieContainer; } client.DefaultRequestHeaders.Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Accept .Add(new MediaTypeWithQualityHeaderValue("text/html")); var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, _jsonContentType); string requestUri = $"api/{type.Name}/{claim.ResourceId}"; message.WriteLine($"Posting to {baseAddress.AbsoluteUri}{requestUri}"); HttpResponseMessage response; try { //response = client.GetAsync(requestUri).Result; response = client.PostAsync(requestUri, content).Result; } catch (Exception e) { while (e.InnerException != null) { e = e.InnerException; } error.WriteLine(e.Message); return(null); } TextWriter m = response.IsSuccessStatusCode ? message : error; m.WriteLine($"server returned {(int)response.StatusCode} ({response.ReasonPhrase})"); #if DEBUG bool debug = true; #else bool debug = false; #endif if (!response.IsSuccessStatusCode && response.Content.Headers.ContentLength > 0) { var tempFile = Path.GetTempFileName() + ".html"; using (var fileStream = File.Create(tempFile)) { using (var responseStream = response.Content.ReadAsStreamAsync().Result) { responseStream.CopyTo(fileStream); } } System.Diagnostics.Process.Start(tempFile); } else if (debug) { var msg = response.Content.ReadAsStringAsync().Result; if (msg != string.Empty) { m.WriteLine(msg); } } return(response); } } }
public string EncryptAESToBase64String(string inputString) { return(Convert.ToBase64String(SimpleAes.Encrypt(inputString)).TrimEnd(Padding).Replace('+', '-').Replace('/', '_')); }
public byte[] EncryptAES(string inputString) { return(SimpleAes.Encrypt(inputString)); }