Dispose() protected final method

protected final Dispose ( bool disposing ) : void
disposing bool
return void
Example #1
0
        public override string Encrypt(string data)
        {
            SHA256Managed sha256hasher = new SHA256Managed();

            try
            {
                UTF8Encoding encoder = new UTF8Encoding();

                byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(string.Format(base.hashFormat, data)));

                return Convert.ToBase64String(hashedDataBytes);
            }
            catch (Exception e)
            {
                var exception = new CryptologyException("SHA256Encryptor.Encrypt()", "An error occurred while encrypting.", e);
                exception.Data.Add("hashFormat", hashFormat);
                exception.Data.Add("data", data);

                throw exception;
            }
            finally
            {
                sha256hasher.Dispose();
            }
        }
    private static SqlDataRecord FillRecord(Int32 pk, SqlDataRecord record)
    {
        Int32 age = SlowRandom(16, 99);
        string sourceString = "Age: " + age.ToString();
        DateTime sourceDate = DateTime.UtcNow;

        var data = /*salt + */sourceString;
                
        string key = "Top Secret Key";

        var encData = AES.EncryptBytes(data, key);
        //var encDataBytes = Encoding.Unicode.GetBytes(encData);
        var decData = AES.DecryptBytes(encData, key);

        var sha = new SHA256Managed();
        byte[] dataSHA256 = sha.ComputeHash(encData/*Bytes*/);
        sha.Dispose();

        // конвертирую хеш из byte[16] в строку шестнадцатиричного формата
        // (вида «3C842B246BC74D28E59CCD92AF46F5DA»)
        // это опциональный этап, если вам хеш нужен в строковом виде
        // string sha512hex = BitConverter.ToString(dataSHA512).Replace("-", string.Empty); 

        record.SetInt32(0, pk);
        record.SetDateTime(1, sourceDate);        
        record.SetString(2, sourceString);
        record.SetString(3, Convert.ToBase64String(dataSHA256)); // sha256
        record.SetString(4, Convert.ToBase64String(encData)); // Encrypted
        record.SetString(5, decData); // Decrypted

        return record;
    }
        public EncryptSettingsProvider()
        {
            //read settings from configuration
            var useHashingString = ConfigurationManager.AppSettings["UseHashingForEncryption"];
            var useHashing = System.String.Compare(useHashingString, "false", System.StringComparison.OrdinalIgnoreCase) != 0;

            _encryptionPrefix = ConfigurationManager.AppSettings["EncryptionPrefix"];
            if (string.IsNullOrWhiteSpace(_encryptionPrefix))
            {
                _encryptionPrefix = "encryptedHidden_";
            }

            var key = ConfigurationManager.AppSettings["EncryptionKey"];
            if (useHashing)
            {
                var hash = new SHA256Managed();
                _encryptionKey = hash.ComputeHash(Encoding.UTF8.GetBytes(key));
                hash.Clear();
                hash.Dispose();
            }
            else
            {
                _encryptionKey = Encoding.UTF8.GetBytes(key);
            }
        }
        /// <summary>
        /// A method is used to validate the signed data by using specified public key.
        /// </summary>
        /// <param name="signedData">A parameter represents the signed data which will be validate.</param>
        /// <param name="originalData">A parameter represents the original data which is used to execute the validation.</param>
        /// <param name="publicKeyBlob">A parameter represents the binaries data of the public key part of a unique key-pairs.</param>
        /// <returns>Return 'true' indicating the signed data pass the validation.</returns>
        private static bool VerifySignedData(byte[] signedData, byte[] originalData, byte[] publicKeyBlob)
        {
            if (null == signedData || 0 == signedData.Length)
            {
                throw new ArgumentNullException("signedData");
            }

            if (null == originalData || 0 == originalData.Length)
            {
                throw new ArgumentNullException("originalData");
            }

            if (null == publicKeyBlob || 0 == publicKeyBlob.Length)
            {
                throw new ArgumentNullException("publicKeyBlob");
            }

            using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider())
            {
                rsaProvider.ImportCspBlob(publicKeyBlob);
                SHA256Managed sha = new SHA256Managed();
                bool result = rsaProvider.VerifyData(originalData, sha, signedData);
                sha.Dispose();
                return result;
            }
        }
        /// <summary>
        /// A method is used to sign the data with specified key-pairs. The signed data only pass the validation by using the public key part of the specified key-pairs.
        /// </summary>
        /// <param name="originalData">A parameter represents the binaries data which will be signed with old key-pairs.</param>
        /// <param name="fullkeyBlob">A parameter represents the binaries data of the unique key-pairs which is match the asymmetric encrypt algorithm.</param>
        /// <returns>A return value represents the signed data.</returns>
        private static byte[] SignData(byte[] originalData, byte[] fullkeyBlob)
        {
            if (null == originalData || 0 == originalData.Length)
            {
                throw new ArgumentNullException("originalData");
            }

            if (null == fullkeyBlob || 0 == fullkeyBlob.Length)
            {
                throw new ArgumentNullException("fullkeyBlob");
            }

            using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider())
            {
                rsaProvider.ImportCspBlob(fullkeyBlob);
                SHA256Managed sha = new SHA256Managed();
                byte[] signedData = rsaProvider.SignData(originalData, sha);
                sha.Dispose();
                return signedData;
            }
        }
Example #6
0
            public static string ComputeHash(string plaintext, Supported_HA hash, byte[] salt)
            {
                int minSaltLength = 4;
                int maxSaltLenght = 6;
                byte[] SaltBytes = null;

                if(salt!=null)
                {
                    SaltBytes = salt;
                }
                else
                {
                    Random r = new Random();
                    int SaltLenght = r.Next(minSaltLength, maxSaltLenght);
                    SaltBytes = new byte[SaltLenght];
                    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                    rng.GetNonZeroBytes(SaltBytes);
                    rng.Dispose();
                }

                byte[] plaintData = ASCIIEncoding.UTF8.GetBytes(plaintext);
                byte[] plainDataAndSalt = new byte[plaintData.Length + SaltBytes.Length];

                for(int x=0; x<plaintData.Length;x++)
                {
                    plainDataAndSalt[x] = plaintData[x];
                    
                }
                for (int n = 0; n < SaltBytes.Length; n++)
                    plainDataAndSalt[plaintData.Length + n] = SaltBytes[n];

                byte[] hashValue = null;

                switch(hash)
                {
                    case Supported_HA.SHA256:
                        SHA256Managed sha= new SHA256Managed();
                        hashValue= sha.ComputeHash(plainDataAndSalt);
                        sha.Dispose();
                        break;

                    case Supported_HA.SHA384:
                        SHA384Managed sha1 = new SHA384Managed();
                        hashValue = sha1.ComputeHash(plainDataAndSalt);
                        sha1.Dispose();
                        break;
                    case Supported_HA.SHA512:
                        SHA512Managed sha2 = new SHA512Managed();
                        hashValue = sha2.ComputeHash(plainDataAndSalt);
                        sha2.Dispose();
                        break;
                }

                byte[] resuflt = new byte[hashValue.Length + SaltBytes.Length];
                for (int x = 0; x < hashValue.Length; x++)
                    resuflt[x] = hashValue[x];
                for (int n = 0; n < SaltBytes.Length; n++)
                    resuflt[hashValue.Length + n] = SaltBytes[n];
                return Convert.ToBase64String(resuflt);
            }