Ejemplo n.º 1
0
        public AppConfigMonitoringAgentConfiguration()
        {
            MongoConnectionString = ConfigurationManager.ConnectionStrings["mongo"].ConnectionString;

            CustomerId = ConfigurationManager.AppSettings["customer-id"];
            var publicKey = ConfigurationManager.AppSettings["customer-key"];

            Key = AsymmetricEncryptionKey.CreateFromString(publicKey, false);

            ServerAddress = ConfigurationManager.AppSettings["server-address"].TrimEnd('/', '\\');

            MongoLogDatabaseList = new List <MongoLogDatabase>();
            foreach (var mongoLogSetting in ConfigurationManager.AppSettings
                     .AllKeys.Where(k => k.StartsWith("mongo-log")))
            {
                var setting         = ConfigurationManager.AppSettings[mongoLogSetting];
                var splittedSetting = setting.Split('|');
                MongoLogDatabaseList.Add(new MongoLogDatabase()
                {
                    ConnectionString = splittedSetting[0],
                    CollectionName   = splittedSetting[1],
                });
            }

            UploadQueueFolder = new DirectoryInfo(ConfigurationManager.AppSettings["upload-temp-folder"]);
            if (!UploadQueueFolder.Exists)
            {
                Directory.CreateDirectory(UploadQueueFolder.FullName);
            }
        }
 public static Byte[] Decrypt(AsymmetricEncryptionKey key, Byte[] data)
 {
     using (var rsa = key.GetProvider())
     {
         return rsa.Decrypt(data, true);
     }
 }
Ejemplo n.º 3
0
            public static EncryptionKey Decrypt(AsymmetricEncryptionKey asymmetricKey, EncryptionKey encryptedSimmetricKey)
            {
                var IV  = Decrypt(asymmetricKey, encryptedSimmetricKey.IV);
                var Key = Decrypt(asymmetricKey, encryptedSimmetricKey.Key);

                return(new EncryptionKey()
                {
                    IV = IV, Key = Key
                });
            }
Ejemplo n.º 4
0
            public static EncryptionKey Encrypt(AsymmetricEncryptionKey asymmetricKey, EncryptionKey symmetricKey)
            {
                var IV  = Encrypt(asymmetricKey, symmetricKey.IV);
                var Key = Encrypt(asymmetricKey, symmetricKey.Key);

                return(new EncryptionKey()
                {
                    IV = IV, Key = Key
                });
            }
            public static AsymmetricEncryptionKey GenerateAsimmetricKey()
            {
                //Generate a public/private key pair.
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    //Save the public key information to an RSAParameters structure.
                    AsymmetricEncryptionKey key = new AsymmetricEncryptionKey(RSA);

                    return key;
                }
            }
Ejemplo n.º 6
0
        private void DecryptFile(string tempFolder, string keyFile, string encryptedFile, Customer customer)
        {
            var encryptionKeyString = File.ReadAllText(keyFile);
            var encryptedKey        = EncryptionKey.CreateFromSerializedString(encryptionKeyString);
            var asymmetricKey       = AsymmetricEncryptionKey.CreateFromString(customer.PrivateKey, true);
            var decryptedKey        = EncryptionUtils.Decrypt(asymmetricKey, encryptedKey);

            //now we can decrypt
            var decryptedFileName = Path.Combine(tempFolder, "decrypted.zip");

            EncryptionUtils.DecryptFile(decryptedKey, encryptedFile, decryptedFileName);
            Logger.DebugFormat("Decrypted file {0} ", decryptedFileName);

            //now we can unzip
            ZipFile.ExtractToDirectory(decryptedFileName, tempFolder);
            Logger.DebugFormat("Unzipped file {0} ", decryptedFileName);
        }
            /// <summary>
            /// Encrypt a file generating new simmetric key, the simmetric key is then 
            /// encrypted with an asimmetric key
            /// </summary>
            /// <param name="fileName"></param>
            /// <param name="destinationFileName"></param>
            /// <param name="key"></param>
            /// <returns></returns>
            public static String EncryptFile(String fileName, String destinationFileName, AsymmetricEncryptionKey key)
            {
                var newSimmetricKey = GenerateKey();
                using (FileStream sourceFs = new FileStream(fileName, FileMode.Open))
                using (FileStream destinationFs = new FileStream(destinationFileName, FileMode.Create))
                using (RijndaelManaged crypto = new RijndaelManaged())
                {
                    ICryptoTransform ct = crypto.CreateEncryptor(newSimmetricKey.Key, newSimmetricKey.IV);
                    using (CryptoStream cs = new CryptoStream(destinationFs, ct, CryptoStreamMode.Write))
                    {
                        sourceFs.CopyTo(cs);
                    }
                }

                var encryptedKey = Encrypt(key, newSimmetricKey);
                return encryptedKey.SerializeAsString();
            }
 public static String Decrypt(AsymmetricEncryptionKey key, String data)
 {
     var decrypted = Decrypt(key, HexEncoding.GetBytes(data));
     return Encoding.UTF8.GetString(decrypted);
 }
 public static String Encrypt(AsymmetricEncryptionKey key, String data)
 {
     var encrypted = Encrypt(key, Encoding.UTF8.GetBytes(data));
     return BitConverter.ToString(encrypted).Replace("-", "");
 }