예제 #1
0
        static public KIVPair LoadKeyIV()
        {
            if (!File.Exists(CONFIG_FILE_NAME))
            {
                return(null);
            }

            using (FileStream fs = new FileStream(CONFIG_FILE_NAME, FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();

                try
                {
                    KIVPair myKIV = (KIVPair)formatter.Deserialize(fs);
                    return(myKIV);
                }
                catch (Exception e)
                {
                    WriteLog("Falha no Load do kiv, motivo: " + e.Message);
                    return(null);
                }
                finally
                {
                    fs.Close();
                }
            }
        }
예제 #2
0
 public static void DefineKIV()
 {
     try
     {
         using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider())
         {
             // Encrypt the string to an array of bytes.
             KIVPair kiv = new KIVPair(myAes.Key, myAes.IV);
             WriteConfig(kiv);
         }
     }
     catch (Exception e)
     {
         WriteLog(e.Message);
         WriteLog(e.StackTrace);
     }
 }
예제 #3
0
        public static void WriteConfig(KIVPair kiv)
        {
            try
            {
                // Construct a BinaryFormatter and use it to serialize the data to the stream.

                FileStream fs = null;

                BinaryFormatter formatter = new BinaryFormatter();

                using (fs = new FileStream(CONFIG_FILE_NAME, FileMode.Create))
                {
                    formatter.Serialize(fs, kiv);
                }
            }
            catch (Exception e)
            {
                WriteLog("Falha ao salvar configurações, motivo: " + e.Message + "\n" + e.StackTrace);
            }
        }
예제 #4
0
        public static byte[] EncryptStringToBytes_Aes(string plainText)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
            {
                throw new ArgumentNullException("plainText");
            }

            byte[] encrypted;

            try
            {
                // Create an AesManaged object
                // with the specified key and IV.
                using (AesManaged aesAlg = new AesManaged())
                {
                    KIVPair kiv = LoadKeyIV();
                    // Create an encryptor to perform the stream transform.
                    ICryptoTransform encryptor = aesAlg.CreateEncryptor(kiv.K, kiv.IV);

                    // Create the streams used for encryption.
                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                //Write all data to the stream.
                                swEncrypt.Write(plainText);
                            }
                            encrypted = msEncrypt.ToArray();
                        }
                    }
                    return(encrypted);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #5
0
        public static string DecryptStringFromBytes_Aes(byte[] cipherText)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
            {
                throw new ArgumentNullException("cipherText");
            }


            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an AesManaged object
            // with the specified key and IV.
            using (AesManaged aesAlg = new AesManaged())
            {
                KIVPair kiv = LoadKeyIV();

                // Create a decryptor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(kiv.K, kiv.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }

            return(plaintext);
        }