Ejemplo n.º 1
0
        private ICryptoTransform GetDecryptorAes(KeyIvModel keyiv)
        {
            ICryptoTransform result;

            using (Aes myAes = Aes.Create())
            {
                myAes.Mode    = _cipheMode;
                myAes.KeySize = _keySize;
                myAes.Padding = _paddingMode;
                result        = myAes.CreateDecryptor(keyiv.Key, keyiv.IV);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public byte[] EncryptToByte(byte[] plaintext, KeyIvModel keyiv)
        {
            var encryptor = GetEncryptorAes(keyiv);

            byte[] cipherText;
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    csEncrypt.Write(plaintext, 0, plaintext.Length);
                }
                cipherText = msEncrypt.ToArray();
            }
            return(cipherText);
        }
Ejemplo n.º 3
0
        public string Decrypt(string ciphertext, KeyIvModel keyiv)
        {
            var    cipherbyte = Convert.FromBase64String(ciphertext);
            var    decryptor  = GetDecryptorAes(keyiv);
            string plaintext;

            using (MemoryStream msDecrypt = new MemoryStream(cipherbyte))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }
            return(plaintext);
        }
Ejemplo n.º 4
0
 public void Encrypt(string filename, string outfile, KeyIvModel keyiv)
 {
     using (FileStream fsCrypt = new FileStream(outfile, FileMode.Create))
     {
         using (CryptoStream cs = new CryptoStream(fsCrypt, GetEncryptorAes(keyiv), CryptoStreamMode.Write))
         {
             using (FileStream fsIn = new FileStream(filename, FileMode.Open))
             {
                 byte[] buffer = new byte[1048576];
                 int    read;
                 while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
                 {
                     cs.Write(buffer, 0, read);
                 }
             }
         }
     }
 }
Ejemplo n.º 5
0
        public byte[] DecryptFromByte(byte[] ciphertext, KeyIvModel keyiv)
        {
            var decryptor = GetDecryptorAes(keyiv);

            byte[] plain;
            using (MemoryStream msDecrypt = new MemoryStream(ciphertext))
            {
                using (CryptoStream cryptoStream = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader stream = new StreamReader(cryptoStream))
                    {
                        string sf = stream.ReadToEnd();
                        plain = System.Text.Encoding.Default.GetBytes(sf);
                    }
                }
            }
            return(plain);
        }
Ejemplo n.º 6
0
        public void Decrypt(string filename, string outfile, KeyIvModel keyiv)
        {
            var decryptor = GetDecryptorAes(keyiv);

            using (FileStream fsCrypt = new FileStream(filename, FileMode.Open))
            {
                using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (FileStream fsOut = new FileStream(outfile, FileMode.Create))
                    {
                        int data;
                        while ((data = cs.ReadByte()) != -1)
                        {
                            fsOut.WriteByte((byte)data);
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public string Encrypt(string plaintext, KeyIvModel keyiv)
        {
            var encryptor = GetEncryptorAes(keyiv);

            byte[] cipherText;
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(plaintext);
                    }
                    cipherText = msEncrypt.ToArray();
                }
            }
            var result = Convert.ToBase64String(cipherText);

            return(result);
        }