/// <summary>
        /// Ask for clear text of data by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns>cleartext</returns>
        public virtual async Task <DecryptedOutput> GetDecryptDataByIdAsync(string id)

        {
            var entity = await sensitiveRepository.GetByIdAsync(Int64.Parse(id));

            if (entity == null)
            {
                throw new NotFoundException(
                          $"Could not find data with ID {id}");
            }

            var data = JsonSerializer.Deserialize <AesModel>(entity.Data);

            var encryptionKey = config.GetKeyByName(entity.EncryptionKeyName);

            if (string.IsNullOrWhiteSpace(encryptionKey))
            {
                throw new MissingEncryptionKeyException($"Could not retrieve {entity.EncryptionKeyName}");
            }

            var cleartext = dataProtector.Decrypt(encryptionKey, data);

            return(new DecryptedOutput()
            {
                Id = id,
                Value = cleartext
            });
        }
Ejemplo n.º 2
0
        public string getStringValue(string stringName, bool decrypt)
        {
            string strTemp = internGetStringValue(stringName);

            if (decrypt)
            {
                DataProtector myProtect = new DataProtector(DataProtector.Store.USE_MACHINE_STORE);
                strTemp = myProtect.Decrypt(strTemp);
            }
            return(strTemp);
        }
Ejemplo n.º 3
0
        string DecryptDPAPI(string enc)
        {
            DataProtector dp = new
                               DataProtector(DataProtector.Store.USE_MACHINE_STORE);

            byte[] dataToDecrypt =
                Convert.FromBase64String(enc);
            // Optional entropy parameter is null.
            // If entropy was used within the Encrypt method, the same entropy
            // parameter must be supplied here
            return(Encoding.UTF8.GetString(dp.Decrypt(dataToDecrypt, null)));
        }
Ejemplo n.º 4
0
        internal static IList Decrypt(this IEnumerable source, EncryptionSettings settings)
        {
            DataProtector encryptionAlgorithm = AeadAes256CbcHmac256EncryptionAlgorithm.GetOrCreate(settings.DataEncryptionKey, settings.EncryptionType);
            ISerializer   serializer          = settings.GetSerializer();

            Type  type = serializer.GetType().BaseType.GetGenericArguments()[0];
            IList list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(type));

            foreach (var item in source)
            {
                byte[] plaintextData = encryptionAlgorithm.Decrypt((byte[])item);
                list.Add(serializer.Deserialize(plaintextData));
            }

            return(list);
        }
Ejemplo n.º 5
0
        private String DPAPIDecryptFromReg(String Key)
        {
            string sResult = "";

            DataProtector dp = new DataProtector(DataProtection.DataProtector.Store.USE_MACHINE_STORE);

            try
            {
                Byte[] entropy       = { 9, 4, 7, 5, 1, 3, 2 };
                Byte[] dataToDecrypt = { };

                //A 32-bit application on a 64-bit OS will be looking at the HKLM\Software\Wow6432Node node by default.
                //To read the 64-bit version of the key, you'll need to specify the RegistryView:
                using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
                {
                    //RegistryKey regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\DBSAMS\\ECPORTAL", true);
                    using (var regKey = hklm.OpenSubKey("SOFTWARE\\DBSAMS\\ECPORTAL", true))
                    {
                        if (regKey == null)
                        {
                            sResult = Key;
                            return(sResult);
                        }
                        dataToDecrypt = (Byte[])regKey.GetValue(Key);
                    }
                }

                if (dataToDecrypt != null && entropy != null)
                {
                    sResult = Encoding.ASCII.GetString(dp.Decrypt(dataToDecrypt, entropy));
                }
                else
                {
                    sResult = Key;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Decrypt err msg: " + ex.Message);
                return(sResult = "");
            }

            return(sResult);
        }
Ejemplo n.º 6
0
        public static string DecryptString(string txtString)
        {
            // If the variable is blank, return the input
            if (txtString.Equals(string.Empty))
            {
                return txtString;
            }

            // Create an instance of the encryption API
            // We assume the key has been encrypted on this machine and not by a user
            DataProtector dp = new DataProtector(Store.Machine);

            // Use the API to decrypt the connection string
            // API works with bytes so we need to convert to and from byte arrays
            byte[] decryptedData = dp.Decrypt(Convert.FromBase64String(txtString), null);

            // Return the decyrpted data to the string
            return Encoding.ASCII.GetString(decryptedData);
        }
Ejemplo n.º 7
0
        public static string DecryptString(string txtString)
        {
            // If the variable is blank, return the input
            if (txtString.Equals(string.Empty))
            {
                return(txtString);
            }

            // Create an instance of the encryption API
            // We assume the key has been encrypted on this machine and not by a user
            DataProtector dp = new DataProtector(Store.Machine);

            // Use the API to decrypt the connection string
            // API works with bytes so we need to convert to and from byte arrays
            byte[] decryptedData = dp.Decrypt(Convert.FromBase64String(txtString), null);

            // Return the decyrpted data to the string
            return(Encoding.ASCII.GetString(decryptedData));
        }
        /// <summary>
        /// Decrypts the string.
        /// </summary>
        /// <param name="inString">The in string.</param>
        /// <returns>System.String.</returns>
        public static string DecryptString(string inString)
        {
            System.Diagnostics.EventLog.WriteEntry("DrSched.asmx decryptString inString=", inString);
            string        results = "";
            DataProtector dp      = new DataProtector(Store.MachineStore);

            byte[] dataToDecrypt = Convert.FromBase64String(inString);

            try
            {
                results = Encoding.Unicode.GetString(dp.Decrypt(dataToDecrypt));
                System.Diagnostics.EventLog.WriteEntry("DrSched.asmx decryptString results=", results);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("DrSched.asmx", "Exception in decryptString " + ex.Message);
            }
            return(results);
        }