Exemple #1
0
        public string EncryptString(string inStr, string inKey, bool?md5 = null)
        {
            string outStr    = string.Empty;
            var    hasher    = new ROHasher(md5 != null ? md5.Value : _md5);
            var    encryptor = new ROPwdCrypto(md5 != null ? md5.Value : _md5);

            try
            {
                outStr = Convert.ToBase64String(
                    encryptor.Encrypt(
                        hasher.ComputeHash(UTF8Encoding.UTF8.GetBytes(inKey)),
                        UTF8Encoding.UTF8.GetBytes(inStr))
                    );
            }
            catch (Exception ex)
            {
                outStr = null;
                if (ex == null)
                {
                    throw;
                }
            }
            hasher    = null;
            encryptor = null;
            return(outStr);
        }
Exemple #2
0
        protected string DecryptString(string inStr, string inKey, bool?md5 = null)
        {
            if (string.IsNullOrEmpty(inStr))
            {
                return(null);
            }

            string outStr    = "";
            var    hasher    = new ROHasher(md5 != null ? md5.Value : _md5);
            var    encryptor = new ROPwdCrypto(md5 != null ? md5.Value : _md5);

            try
            {
                outStr = UTF8Encoding.UTF8.GetString(
                    encryptor.Decrypt(
                        hasher.ComputeHash(UTF8Encoding.UTF8.GetBytes(inKey)),
                        Convert.FromBase64String(inStr))
                    );
            }
            catch
            {
                outStr = null;
            }
            hasher    = null;
            encryptor = null;
            return(outStr);
        }
Exemple #3
0
        public string RODecryptString(string inStr, string inKey)
        {
            try
            {
                var    hasher        = new ROHasher(DesMD5);
                byte[] encryptedData = Convert.FromBase64String(inStr);
                byte   ver           = encryptedData[0];
                int    ivSize        = 0;
                if (ver == 1)
                {
                    ivSize = 8;
                }
                else if (ver == 2)
                {
                    ivSize = 16;
                }
                else
                {
                    throw new Exception("unsupported encryption version");
                }

                SymmetricAlgorithm cipher = ver == 1 ? (SymmetricAlgorithm) new TripleDESCryptoServiceProvider() : (SymmetricAlgorithm) new AesCryptoServiceProvider();

                foreach (string key in (inKey ?? "")
                         .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                         .Where(s => !string.IsNullOrEmpty(s.Trim()))
                         .Select(s => s.Trim())
                         )
                {
                    try
                    {
                        cipher.IV   = encryptedData.Skip(1).Take(ivSize).ToArray();
                        cipher.Mode = CipherMode.CBC;
                        cipher.Key  = hasher.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)).Take(DesMD5 ? 16 : 32).ToArray();
                        string outStr = UTF8Encoding.UTF8.GetString(cipher.CreateDecryptor().TransformFinalBlock(encryptedData.Skip(1 + ivSize).ToArray(), 0, encryptedData.Length - (1 + ivSize)));
                        return(outStr);
                    }
                    catch
                    {
                    }
                }
                throw new Exception("no suitable secret column keys");
            }
            catch
            {
                throw;
            }
        }
Exemple #4
0
        public string ROEncryptString(string inStr, string inKey)
        {
            string outStr             = string.Empty;
            RandomNumberGenerator rng = new RNGCryptoServiceProvider();

            // general format
            // base64(version byte + byte[] of IV + encrypted content) + '-' + visible tail portion
            // version 1 3DES CBC with 8 byte IV
            // version 2 AES256 CBC with 16 byte IV

            byte[] ver = new byte[] { (byte)(DesMD5 ? 1 : 2) };
            byte[] iv  = new byte[DesMD5 ? 8 : 16];
            rng.GetBytes(iv);

            var hasher = new ROHasher(DesMD5);
            SymmetricAlgorithm cipher = DesMD5 ? (SymmetricAlgorithm) new TripleDESCryptoServiceProvider() : (SymmetricAlgorithm) new AesCryptoServiceProvider();

            cipher.Mode = CipherMode.CBC;
            cipher.IV   = iv;
            cipher.Key  = hasher.ComputeHash(UTF8Encoding.UTF8.GetBytes(inKey)).Take(DesMD5 ? 16 : 32).ToArray();
            byte[] encryptedBlock = cipher.CreateEncryptor().TransformFinalBlock(UTF8Encoding.UTF8.GetBytes(inStr), 0, UTF8Encoding.UTF8.GetBytes(inStr).Length);
            outStr = Convert.ToBase64String(ver.Concat(iv).Concat(encryptedBlock).ToArray());
            return(outStr);
        }