public void AesManaged_ReencryptionWithSameKey_ReturnsSameText() { // Arrange var aesManaged = new AesManaged(); var key = Convert.ToBase64String(aesManaged.Key); // Act var encrypted1 = AesEncryptamajig.Encrypt(_plainText, key); var roundtrip1 = AesEncryptamajig.Decrypt(encrypted1, key); Debug.WriteLine("Plain Text: " + _plainText); Debug.WriteLine("Encrypted: " + encrypted1); Debug.WriteLine("Roundtrip: " + roundtrip1); // Act 2 (encrypt the same data again) var encrypted2 = AesEncryptamajig.Encrypt(_plainText, key); var roundtrip2 = AesEncryptamajig.Decrypt(encrypted2, key); Debug.WriteLine("Encrypted again: " + encrypted2); Debug.WriteLine("Roundtrip again: " + roundtrip2); // Assert Assert.NotEqual(_plainText, encrypted1); Assert.NotEqual(_plainText, encrypted2); Assert.Equal(_plainText, roundtrip1); Assert.Equal(_plainText, roundtrip2); Assert.NotEqual(encrypted1, encrypted2); }
static void Main(string[] args) { var encrypted = AesEncryptamajig.Encrypt(_plainText, _key); var roundtrip = AesEncryptamajig.Decrypt(encrypted, _key); Debug.WriteLine(_plainText); Debug.WriteLine(encrypted); Debug.WriteLine(roundtrip); }
public string GetApiData(string username, string keyName) { var row = _database.GitHubApiData.FindAllByUserNameAndKeyName(UserName: username, KeyName: keyName).FirstOrDefault(); if (row != null) { // TODO: use secure string? or am I being too paranoid? var encryptedData = row.DataRaw; var raw = AesEncryptamajig.Decrypt(encryptedData, username + Configuration.Current().GetClientSecret()); return(raw); } return(null); }
public void AesManaged_EncryptionRoundtripWithCustomInputs_ReturnsOriginalText() { // Arrange // Act var encrypted = AesEncryptamajig.Encrypt(_plainText, _key); var roundtrip = AesEncryptamajig.Decrypt(encrypted, _key); Debug.WriteLine(_plainText); Debug.WriteLine(encrypted); Debug.WriteLine(roundtrip); // Assert Assert.NotEqual(_plainText, encrypted); Assert.Equal(_plainText, roundtrip); }
static void Main(string[] args) { Console.WriteLine("AES Test :"); var encrypted = AesEncryptamajig.Encrypt(_plainText, _key); var roundtrip = AesEncryptamajig.Decrypt(encrypted, _key); Console.WriteLine($"Plain Text : {_plainText}"); Console.WriteLine($"Encrypted : {encrypted}"); Console.WriteLine($"Roundtrip : {roundtrip}"); Console.WriteLine("======================================"); Console.WriteLine("SHA256 Test :"); var hashed = SHA256Encryptamajig.HashToHex(_plainText); var resultFromOnlineHash = "9BBEF19476623CA56C17DA75FD57734DBF82530686043A6E491C6D71BEFE8F6E"; Console.WriteLine($"Library Hash : {hashed}"); Console.WriteLine($"Online Hash : {resultFromOnlineHash}"); Console.ReadLine(); }
public void AesManaged_EncryptionRoundtripWithRandomKey_ReturnsOriginalText() { // Arrange var aesManaged = new AesManaged(); var key = Convert.ToBase64String(aesManaged.Key); // Act var encrypted = AesEncryptamajig.Encrypt(_plainText, key); var roundtrip = AesEncryptamajig.Decrypt(encrypted, key); Debug.WriteLine(_plainText); Debug.WriteLine(encrypted); Debug.WriteLine(roundtrip); // Assert Assert.NotEqual(_plainText, encrypted); Assert.Equal(_plainText, roundtrip); }
public void Decrypt_WithDifferentKey_ThrowsException() { // Arrange var aesManaged = new AesManaged(); var originalKey = Convert.ToBase64String(aesManaged.Key); // This should generate a new key aesManaged = new AesManaged(); var newKey = Convert.ToBase64String(aesManaged.Key); // Act var encrypted = AesEncryptamajig.Encrypt(_plainText, originalKey); Debug.WriteLine("Org Key = " + originalKey); Debug.WriteLine("New Key = " + newKey); string roundtrip = AesEncryptamajig.Decrypt(encrypted, newKey); // Assert throw new Exception("This should have thrown a CryptographicException!"); }
public string DecryptSecret(string cipher) { return(AesEncryptamajig.Decrypt(cipher, Convert.ToBase64String(Key))); }
/// <summary> /// Loads this instance. /// </summary> /// <returns>DebtPortfolio.</returns> public DebtPortfolio Load() => JsonConvert.DeserializeObject <DebtPortfolio>( AesEncryptamajig.Decrypt(File.ReadAllText(FilePath), Key.ToString()));