Пример #1
0
 public byte[] Encrypt(byte[] bytes)
 {
     using (MemoryStream ms = new MemoryStream())
         using (CryptoStream cs = new CryptoStream(ms, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
         {
             cs.Write(bytes, 0, bytes.Length);
             //cs.FlushFinalBlock();
             cs.Close();
             return(ms.ToArray());
         }
 }
Пример #2
0
            public string Encrypt(string str)
            {
                byte[]       bytes = Encoding.Unicode.GetBytes(str);
                MemoryStream ms    = new MemoryStream();
                CryptoStream cs    = new CryptoStream(ms, rijndael.CreateEncryptor(), CryptoStreamMode.Write);

                cs.Write(bytes, 0, bytes.Length);
                cs.Close();
                bytes = ms.ToArray();
                return(Convert.ToBase64String(bytes));
            }
Пример #3
0
    /// <summary>
    /// Encriptamos el objecto
    /// </summary>
    /// <param name="pObject">Objecto a Encriptar</param>
    /// <param name="pKey">Clave 1</param>
    /// <param name="pIV">Clave 2</param>
    /// <returns>Retorna el objecto Encriptado</returns>
    private static byte[] EncryptObjectToBytes(object pObject, byte[] pKey, byte[] pIV)
    {
        //Verifica si los parametros son nulo
        if (pObject == null)
        {
            throw new ArgumentNullException();
        }
        if (pKey == null || pKey.Length <= 0)
        {
            throw new ArgumentNullException();
        }
        if (pIV == null || pIV.Length <= 0)
        {
            throw new ArgumentNullException();
        }

        //Creamos la variable byte[] que almacena el objecto encriptado
        byte[] encrypted;

        //Creamos el algoritmo
        System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create();

        //Usamos el algoritmo
        using (rijndael)
        {
            //Le pasamos los valores de las claves al algoritmno
            rijndael.Key = pKey;
            rijndael.IV  = pIV;

            //Creamos el Encriptador
            System.Security.Cryptography.ICryptoTransform encryptor = rijndael.CreateEncryptor(rijndael.Key, rijndael.IV);

            //Se crean las corrientes para el cifrado
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (System.Security.Cryptography.CryptoStream csEncrypt = new System.Security.Cryptography.CryptoStream(msEncrypt, encryptor, System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Escribe los datos en la secuencia
                        swEncrypt.Write(pObject);
                    }

                    //Almacenamos la encriptacion en el objecto
                    encrypted = msEncrypt.ToArray();
                }
            }
        }

        //Retornamos el objecto encriptado
        return(encrypted);
    }
Пример #4
0
        /// <summary>
        /// Constructs a new instance of <see cref="AesCrypto"/> from the specified values.
        /// </summary>
        /// <param name="salt">The <see cref="Salt"/> used in encryption.</param>
        /// <param name="passphrase">The <see cref="Passphrase"/> used in encryption.</param>
        public AesCrypto(string passphrase, byte[] salt)
        {
            Passphrase = passphrase;
            Salt = salt;

            _encryptor = Rijndael.Create();
            _encryptor.Padding = PaddingMode.PKCS7;
            _pdb = new Rfc2898DeriveBytes(Passphrase, Salt);
            _encryptor.Key = _pdb.GetBytes(_encryptor.KeySize / 8);
            _encryptor.IV = _pdb.GetBytes(_encryptor.BlockSize / 8);

            _encryptorTransform = _encryptor.CreateEncryptor();
            _decryptorTransform = _encryptor.CreateDecryptor();
        }
Пример #5
0
        public string Encrypt(string value)
        {
            System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create();
            byte[]           tmp       = null;
            ICryptoTransform encryptor = rijndael.CreateEncryptor(key, iv);

            using (MemoryStream ms = new MemoryStream()) {
                using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) {
                    StreamWriter writer = new StreamWriter(cs);
                    writer.Write(value);
                    writer.Flush();
                }
                tmp = ms.ToArray();
            }
            return(Convert.ToBase64String(tmp));
        }
Пример #6
0
        /// <summary>
        /// Encode a string using Rijndael with specified password and IV strings.
        /// </summary>
        /// <param name="sourceString">The string to encode</param>
        /// <param name="password">The password string</param>
        /// <param name="IV">The IV string</param>
        /// <returns>The encoded string</returns>
        public string EncodeString(string sourceString, string password, String IV)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException("Please specify the password", nameof(password));
            }

            if (string.IsNullOrEmpty(IV))
            {
                throw new ArgumentException("Please specify the Initialize Vector", nameof(IV));
            }

            if (!string.IsNullOrEmpty(sourceString))
            {
                byte[] encrypted;
                // Create an Rijndael object
                // with the specified key and IV.
                using (System.Security.Cryptography.Rijndael rijAlg = System.Security.Cryptography.Rijndael.Create())
                {
                    rijAlg.Key = GeneratePassword(password);
                    rijAlg.IV  = GeneratePassword(IV);

                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.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(sourceString);
                            }
                            encrypted = msEncrypt.ToArray();
                        }
                    }
                }
                return(Convert.ToBase64String(encrypted));
            }
            else
            {
                return(string.Empty);
            }
        }
Пример #7
0
    static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
        {
            throw new ArgumentNullException("plainText");
        }
        if (Key == null || Key.Length <= 0)
        {
            throw new ArgumentNullException("Key");
        }
        if (IV == null || IV.Length <= 0)
        {
            throw new ArgumentNullException("IV");
        }
        byte[] encrypted;
        // Create an Rijndael object
        // with the specified key and IV.
        using (System.Security.Cryptography.Rijndael rijAlg = System.Security.Cryptography.Rijndael.Create())
        {
            rijAlg.Key = Key;
            rijAlg.IV  = IV;

            // Create an encryptor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.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 the encrypted bytes from the memory stream.
        return(encrypted);
    }
Пример #8
0
        // Encrypt a byte array into a byte array using a key and an IV

        public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.Rijndael alg = System.Security.Cryptography.Rijndael.Create();

            // Algorithm. Rijndael is available on all platforms.

            alg.Key = Key;
            alg.IV  = IV;
            System.Security.Cryptography.CryptoStream cs =
                new System.Security.Cryptography.CryptoStream(ms, alg.CreateEncryptor(),
                                                              System.Security.Cryptography.CryptoStreamMode.Write);

            //CryptoStream is for pumping our data.

            cs.Write(clearData, 0, clearData.Length);
            cs.Close();
            byte[] encryptedData = ms.ToArray();
            return(encryptedData);
        }
Пример #9
0
        public string Encode(string input)
        {
            System.Security.Cryptography.Rijndael alg = System.Security.Cryptography.Rijndael.Create();
            alg.Key     = Encoding.UTF8.GetBytes("kaudkwudhtbenwnakaudkwudhtbenwna");
            alg.IV      = Encoding.UTF8.GetBytes("HR$2pIjHR$2pIj12");
            alg.Padding = PaddingMode.Zeros;

            ICryptoTransform encryptor = alg.CreateEncryptor(alg.Key, alg.IV);

            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(input);
                    }
                    return(Encoding.Unicode.GetString(msEncrypt.ToArray()));
                }
            }
        }
Пример #10
0
        private static void SetupEncryption()
        {
            rijn = Rijndael.Create();
            rijn.KeySize = ENC_Key.Length * 8;
            rijn.Key = ENC_Key;
            rijn.IV = ENC_IV;
            rijn.Padding = PaddingMode.ISO10126;
            rijn.Mode = CipherMode.CBC;

            encryptor = rijn.CreateEncryptor();
            decryptor = rijn.CreateDecryptor();
        }
Пример #11
0
 public void setUpRijndael(byte[] key, byte[] iv)
 {
     _rijndael = new RijndaelManaged();
     _rijndael.Key = key;
     _rijndael.IV = iv;
     _rijEncryptor = _rijndael.CreateEncryptor(_rijndael.Key, _rijndael.IV);
     _rijDecryptor = _rijndael.CreateDecryptor(_rijndael.Key, _rijndael.IV);
 }
Пример #12
0
        protected virtual Stream GetOutputStream(Rijndael alg, AppServerConfigOptions options, string configFilePath)
        {
            Stream outStream = new FileStream(configFilePath, FileMode.CreateNew);

            switch (options)
            {
                case AppServerConfigOptions.FileEncrypted:
                    outStream = new CryptoStream(outStream, alg.CreateEncryptor(), CryptoStreamMode.Write);
                    outStream = new GZipStream(outStream, CompressionMode.Compress);
                    break;
            }

            return outStream;
        }
Пример #13
0
 private static byte[] AES_Encrypt(byte[] clearData, byte[] Key, byte[] IV)
 {
     System.IO.MemoryStream memoryStream            = new System.IO.MemoryStream();
     System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create();
     rijndael.Key = Key;
     rijndael.IV  = IV;
     System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, rijndael.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
     cryptoStream.Write(clearData, 0, clearData.Length);
     cryptoStream.Close();
     return(memoryStream.ToArray());
 }
Пример #14
0
        public static String Encrypt(byte[] clearText, String publicKey, Rijndael aes)
        {
            StringWriter cipherText = new StringWriter();

            ArrayList message = new ArrayList();
            message.AddRange(RSAEncrypt(aes.Key, publicKey));
            message.AddRange(RSAEncrypt(aes.IV, publicKey));
            message.AddRange(AESEncrypt(clearText, aes.CreateEncryptor()));

            String messageArmor = Convert.ToBase64String((byte[]) message.ToArray(Type.GetType("System.Byte")));

            cipherText.WriteLine(BeginMessage);
            cipherText.WriteLine("Version: PractiSES {0} (Win32)",
                                 Assembly.GetExecutingAssembly().GetName().Version.ToString(2));
            cipherText.WriteLine();
            cipherText.WriteLine(Util.Wrap(messageArmor, Wrap));
            cipherText.WriteLine(EndMessage);

            return cipherText.ToString();
        }
Пример #15
0
    internal static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
    {
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.Security.Cryptography.Rijndael alg = System.Security.Cryptography.Rijndael.Create();
        alg.Key = Key;
        alg.IV  = IV;

        System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, alg.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
        cs.Write(clearData, 0, clearData.Length);
        cs.Close();

        byte[] encryptedData = ms.ToArray();
        return(encryptedData);
    }
Пример #16
0
    internal static void Encrypt(string fileIn, string fileOut)
    {
        System.IO.FileStream fsIn  = new System.IO.FileStream(fileIn, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        System.IO.FileStream fsOut = new System.IO.FileStream(fileOut, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);

        System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(Password,
                                                                                                                    new byte[] { 0x49, 0x49, 0x35, 0x6e, 0x76, 0x4d,
                                                                                                                                 0x65, 0x64, 0x76, 0x76, 0x64, 0x65, 0x76 });
        System.Security.Cryptography.Rijndael alg = System.Security.Cryptography.Rijndael.Create();
        alg.Key = pdb.GetBytes(32);
        alg.IV  = pdb.GetBytes(16);

        System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(fsOut,
                                                                                                     alg.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

        int bufferLen = 4096;

        byte[] buffer = new byte[bufferLen];
        int    bytesRead;

        do
        {
            // read a chunk of data from the input file
            bytesRead = fsIn.Read(buffer, 0, bufferLen);
            // encrypt it
            cs.Write(buffer, 0, bytesRead);
        } while (bytesRead != 0);

        cs.Close();
        fsIn.Close();
    }
Пример #17
0
 public static byte[] smethod_3(byte[] byte_0, string string_0)
 {
     System.Security.Cryptography.Rijndael           rijndael           = System.Security.Cryptography.Rijndael.Create();
     System.Security.Cryptography.Rfc2898DeriveBytes rfc2898DeriveBytes = new System.Security.Cryptography.Rfc2898DeriveBytes(string_0, new byte[]
     {
         38,
         220,
         255,
         0,
         173,
         237,
         122,
         238,
         197,
         254,
         7,
         175,
         77,
         8,
         34,
         60
     });
     rijndael.Key = rfc2898DeriveBytes.GetBytes(32);
     rijndael.IV  = rfc2898DeriveBytes.GetBytes(16);
     System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
     System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, rijndael.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
     cryptoStream.Write(byte_0, 0, byte_0.Length);
     cryptoStream.Close();
     return(memoryStream.ToArray());
 }
Пример #18
0
        /// <summary>
        /// This method returns a writeable data stream.
        /// </summary>
        /// <returns></returns>
        protected virtual Stream DataStreamWrite()
        {
            Stream outStream = null; ;
            RijndaelAlg = new RijndaelManaged();

            try
            {
                mFileStream = new FileStream(BinaryDataFileName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
                outStream = mFileStream;

                if (Encrypted)
                {
                    mEncrStream = new CryptoStream(outStream, RijndaelAlg.CreateEncryptor(), CryptoStreamMode.Write);
                    outStream = mEncrStream;
                }

                if (Compressed)
                {
                    mCompStream = new GZipStream(outStream, CompressionMode.Compress);
                    outStream = mCompStream;
                }

                outStream = new StreamCounter(outStream);
            }
            catch (Exception ex)
            {
                //Just make sure that we don't leave anything open in case there is an error.
                if (outStream != null)
                    outStream.Close();

                mFileStream = null;
                mEncrStream = null;
                mCompStream = null;
            }

            return outStream;
        }
Пример #19
0
            public static string Encrypt(string strEncrypt, bool retStrOriginal)
            {
                byte[] bytes  = System.Text.Encoding.UTF8.GetBytes(Secret.AES.Key);
                byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(Secret.AES.IV);
                byte[] bytes3 = System.Text.Encoding.UTF8.GetBytes(strEncrypt);
                string result = string.Empty;

                System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create();
                try
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, rijndael.CreateEncryptor(bytes, bytes2), System.Security.Cryptography.CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(bytes3, 0, bytes3.Length);
                            cryptoStream.FlushFinalBlock();
                            result = Convert.ToBase64String(memoryStream.ToArray());
                        }
                    }
                }
                catch
                {
                    if (retStrOriginal)
                    {
                        result = strEncrypt;
                    }
                    else
                    {
                        result = string.Empty;
                    }
                }
                rijndael.Clear();
                return(result);
            }