public string Encrypt(string plainString)
        {
            var plainBytes         = BytesHelper.GetUTF8StringBytes(plainString);
            var secureHeadAndBytes = Encrypt(plainBytes);

            return(secureHeadAndBytes != null?Convert.ToBase64String(secureHeadAndBytes) : null);
        }
        //class
        private static byte[] SaltString(string data, string salt, uint saltIterations, uint bytesCount)
        {
            byte[]             saltedBytes = null;
            Rfc2898DeriveBytes deriveBytes = null;

            try
            {
                deriveBytes = new Rfc2898DeriveBytes(data, BytesHelper.GetUTF8StringBytes(salt), (int)saltIterations);
                saltedBytes = deriveBytes.GetBytes((int)bytesCount);
            }
            catch (Exception)
            {
                saltedBytes = null;
            }
            finally
            {
                if (deriveBytes is IDisposable &&
                    deriveBytes != null)
                {
                    ((IDisposable)deriveBytes).Dispose();
                    deriveBytes = null;
                }
            }

            return(saltedBytes);
        }
Exemple #3
0
        //instance
        public SecureHead(
            string salt,
            string initiaVector,
            uint saltIterations,
            uint saltedInitiaVectorBytesCount,
            uint saltedPasswordBytesCount,
            uint secureBytesCount)
        {
            if (string.IsNullOrEmpty(initiaVector) == true)
            {
                throw new ArgumentException("initiaVector should NOT be null or empty.");
            }
            if (string.IsNullOrEmpty(salt) == true)
            {
                throw new ArgumentException("salt should NOT be null or empty.");
            }
            if (saltIterations == 0)
            {
                throw new ArgumentException("saltIterations should NOT zero.");
            }
            if (saltedInitiaVectorBytesCount == 0)
            {
                throw new ArgumentException("saltedInitiaVectorBytesCount should NOT zero.");
            }
            if (saltedPasswordBytesCount == 0)
            {
                throw new ArgumentException("saltedPasswordBytesCount should NOT zero.");
            }

            saltBytes         = BytesHelper.GetUTF8StringBytes(salt);
            initiaVectorBytes = BytesHelper.GetUTF8StringBytes(initiaVector);

            SaltBytesCount         = (saltBytes != null) ? (uint)saltBytes.Length : 0;
            InitiaVectorBytesCount = (initiaVectorBytes != null) ? (uint)initiaVectorBytes.Length : 0;
            Salt           = salt;
            InitiaVector   = initiaVector;
            SaltIterations = saltIterations;
            SaltedInitialVectorBytesCount = saltedInitiaVectorBytesCount;
            SaltedPasswordBytesCount      = saltedPasswordBytesCount;
            SecureBytesCount = secureBytesCount;

            TotalBytesCount = (uint)(GetPart1BytesCount() + GetPart2BytesCount() + SaltBytesCount + InitiaVectorBytesCount);
        }