/// <summary> /// /// </summary> /// <param name="Data">The Data to decrypt</param> /// <param name="Key">The Key or Password used to decrypt the Data</param> /// /// <param name="Salt"></param> /// <param name="Algorithm">A Symmetric Algorithm used to decrypt the Data</param> /// <param name="Base64Encoded">True if the Data is Base64 Encoded</param> /// <returns>A decrypted String of the Data</returns> public static string Decrypt(string Data, byte[] Key, byte[] Salt, SymmetricAlgorithms Algorithm, bool Base64Encoded) { // Determine the Algorithm SymmetricAlgorithm symAlgorithm = null; switch (Algorithm) { case SymmetricAlgorithms.DES: symAlgorithm = new DESCryptoServiceProvider(); break; case SymmetricAlgorithms.RC2: symAlgorithm = new RC2CryptoServiceProvider(); break; case SymmetricAlgorithms.Rijndael: symAlgorithm = new RijndaelManaged(); break; case SymmetricAlgorithms.TripleDES: symAlgorithm = new TripleDESCryptoServiceProvider(); break; } if (symAlgorithm == null) { return(string.Empty); } try { symAlgorithm.IV = Salt; symAlgorithm.Key = Key; byte[] cryptData; if (Base64Encoded) { cryptData = Convert.FromBase64String(Data); } else { cryptData = System.Text.Encoding.UTF8.GetBytes(Data); } return(Cryptographer.Decrypt(cryptData, symAlgorithm)); } catch { return(string.Empty); } finally { if (symAlgorithm != null) { symAlgorithm.Clear(); symAlgorithm = null; } } }
/// <summary> /// Decrypts the Properties of an Object /// </summary> /// <param name="Target"></param> private static void DecryptProperties(object Target) { if (Target == null) { return; } if (Target is System.Data.DataTable) { return; } object oValue = null; try { Type type = Target.GetType(); PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { Type propType = EncryptableAttribute.GetPropertyType(prop.PropertyType); if (prop.PropertyType.IsClass && propType.FullName != "System.String" && propType.FullName != "System.String[]" && propType.FullName != "System.Object" && propType.FullName != "System.Object[]" && propType.FullName.Contains("List") == false) { oValue = prop.GetValue(Target, null); DecryptProperties(oValue); continue; } object[] attribs = prop.GetCustomAttributes(typeof(EncryptableAttribute), true); if (attribs.Length == 0) { continue; } EncryptableAttribute attrib = (EncryptableAttribute)attribs[0]; oValue = prop.GetValue(Target, null); if (oValue != null && !string.IsNullOrEmpty(oValue.ToString())) { oValue = Cryptographer.Decrypt(oValue.ToString(), attrib.Key, attrib.Salt, attrib.Algorithm, true); SetPropertyValue(prop, Target, oValue, ""); // Set the Value } } } catch (Exception ex) { throw ex; } }