public string Encriptar(string textoQueEncriptaremos, HasAlgorimt hashAlgorithm, Keysize keySize)
        {
            try
            {
                // Valido los campos
                if (((passBase != "") && ((saltValue != "") && ((passwordIterations > 0) && (initVector != "")))))
                {
                    if (initVector.Length >= 16)
                        initVector = initVector.Substring(1, 16);
                    else
                        initVector = ((initVector + new string(' ', 16))).Substring(1, 16).Replace(" ", "%");

                    if (saltValue.Length >= 16)
                        saltValue = saltValue.Substring(1, 16);
                    else
                        saltValue = ((saltValue + new string(' ', 16))).Substring(1, 16).Replace(" ", "%");

                    byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
                    byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
                    byte[] plainTextBytes = Encoding.UTF8.GetBytes(textoQueEncriptaremos);
                    Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(passBase, saltValueBytes, passwordIterations);
                    byte[] keyBytes = password.GetBytes(((int)keySize / 8));
                    RijndaelManaged symmetricKey = new RijndaelManaged();
                    symmetricKey.Mode = CipherMode.CBC;
                    ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
                    MemoryStream memoryStream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
                    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                    cryptoStream.FlushFinalBlock();
                    byte[] cipherTextBytes = memoryStream.ToArray();
                    memoryStream.Close();
                    //cryptoStream.Close();
                    string cipherText = Convert.ToBase64String(cipherTextBytes);
                    return cipherText;
                }
                else
                {
                    return "";
                }
            }
            catch
            {
                throw;
            }
        }
Example #2
0
        public string Encriptar(string textoQueEncriptaremos, HasAlgorimt hashAlgorithm, Keysize keySize)
        {
            try
            {
                // Valido los campos
                if (((passBase != "") && ((saltValue != "") && ((passwordIterations > 0) && (initVector != "")))))
                {
                    if (initVector.Length >= 16)
                    {
                        initVector = initVector.Substring(1, 16);
                    }
                    else
                    {
                        initVector = ((initVector + new string(' ', 16))).Substring(1, 16).Replace(" ", "%");
                    }

                    if (saltValue.Length >= 16)
                    {
                        saltValue = saltValue.Substring(1, 16);
                    }
                    else
                    {
                        saltValue = ((saltValue + new string(' ', 16))).Substring(1, 16).Replace(" ", "%");
                    }

                    byte[]             initVectorBytes = Encoding.ASCII.GetBytes(initVector);
                    byte[]             saltValueBytes  = Encoding.ASCII.GetBytes(saltValue);
                    byte[]             plainTextBytes  = Encoding.UTF8.GetBytes(textoQueEncriptaremos);
                    Rfc2898DeriveBytes password        = new Rfc2898DeriveBytes(passBase, saltValueBytes, passwordIterations);
                    byte[]             keyBytes        = password.GetBytes(((int)keySize / 8));
                    RijndaelManaged    symmetricKey    = new RijndaelManaged();
                    symmetricKey.Mode = CipherMode.CBC;
                    ICryptoTransform encryptor    = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
                    MemoryStream     memoryStream = new MemoryStream();
                    CryptoStream     cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
                    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                    cryptoStream.FlushFinalBlock();
                    byte[] cipherTextBytes = memoryStream.ToArray();
                    memoryStream.Close();
                    //cryptoStream.Close();
                    string cipherText = Convert.ToBase64String(cipherTextBytes);
                    return(cipherText);
                }
                else
                {
                    return("");
                }
            }
            catch
            {
                throw;
            }
        }
Example #3
0
        public string Desencriptar(string textoEncriptado, HasAlgorimt hashAlgorithm, Keysize keySize)
        {
            this.InicioClaves("8CMW38!N37", "GRUP0@PL1R3D!N3T81@W38700LS", "B1@W38GRUP0@PL1R3D!N3T81@W38700LS!N3T", 5);
            try
            {
                if (((passBase != "") &&
                     ((saltValue != "") &&
                      ((passwordIterations > 0) &&
                       (initVector != "")))))
                {
                    if (initVector.Length >= 16)
                    {
                        initVector = initVector.Substring(1, 16);
                    }
                    else
                    {
                        initVector = ((initVector + new string(' ', 16))).Substring(1, 16).Replace(" ", "%");
                    }

                    if (saltValue.Length >= 16)
                    {
                        saltValue = saltValue.Substring(1, 16);
                    }
                    else
                    {
                        saltValue = ((saltValue + new string(' ', 16))).Substring(1, 16).Replace(" ", "%");
                    }

                    byte[]             initVectorBytes = Encoding.ASCII.GetBytes(initVector);
                    byte[]             saltValueBytes  = Encoding.ASCII.GetBytes(saltValue);
                    byte[]             plainTextBytes  = Convert.FromBase64String(textoEncriptado);
                    Rfc2898DeriveBytes password        = new Rfc2898DeriveBytes(passBase, saltValueBytes, passwordIterations);
                    byte[]             keyBytes        = password.GetBytes(((int)keySize / 8));
                    RijndaelManaged    symmetricKey    = new RijndaelManaged();
                    symmetricKey.Mode = CipherMode.CBC;
                    ICryptoTransform decryptor    = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
                    MemoryStream     MemoryStream = new MemoryStream();
                    CryptoStream     CryptoStream = new CryptoStream(MemoryStream, decryptor, CryptoStreamMode.Write);
                    CryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                    CryptoStream.FlushFinalBlock();
                    byte[] cipherTextBytes = MemoryStream.ToArray();
                    MemoryStream.Close();
                    string plainText = Encoding.UTF8.GetString(cipherTextBytes);
                    return(plainText);
                }
                else
                {
                    return("");
                }
            }
            catch
            {
                throw;
            }
        }