C#/C#2016.02.03 18:23

class ImpDataEncryption : IDataEncryption

    {        


        string IDataEncryption.SHA1Encrypt(string key, string textToEncrypt)

        {

            byte[] keyArray = Encoding.UTF8.GetBytes(key);

            byte[] toEncryptArray = Encoding.UTF8.GetBytes(textToEncrypt);


            RijndaelManaged rijndaelManaged = new RijndaelManaged();

            rijndaelManaged.Mode = CipherMode.CBC;

            rijndaelManaged.Padding = PaddingMode.PKCS7;

            rijndaelManaged.KeySize = 128;

            rijndaelManaged.BlockSize = 128;


            byte[] keyBytes = new byte[16];

            int len = keyArray.Length;

            if (len > keyBytes.Length)

            {

                len = keyBytes.Length;

            }


            Array.Copy(keyArray, keyBytes, len);

            rijndaelManaged.Key = keyBytes;

            rijndaelManaged.IV = keyBytes;


            ICryptoTransform iCryptoTransform = rijndaelManaged.CreateEncryptor();

            byte[] resultArray = iCryptoTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);


            return Convert.ToBase64String(resultArray, 0, resultArray.Length);

        }


        string IDataEncryption.SHA1Decrypt(string key, string encryptText)

        {

            byte[] keyArray = Encoding.UTF8.GetBytes(key);

            byte[] toEncryptArray = Convert.FromBase64String(encryptText);


            RijndaelManaged rijndaelManaged = new RijndaelManaged();

            rijndaelManaged.Mode = CipherMode.CBC;

            rijndaelManaged.Padding = PaddingMode.PKCS7;

            rijndaelManaged.KeySize = 128;

            rijndaelManaged.BlockSize = 128;


            byte[] keyBytes = new byte[16];

            int len = keyArray.Length;

            if (len > keyBytes.Length)

            {

                len = keyBytes.Length;

            }


            Array.Copy(keyArray, keyBytes, len);

            rijndaelManaged.Key = keyBytes;

            rijndaelManaged.IV = keyBytes;


            ICryptoTransform iCryptoTransform = rijndaelManaged.CreateDecryptor();

            byte[] resultArray = iCryptoTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);


            return UTF8Encoding.UTF8.GetString(resultArray);

        }


        string IDataEncryption.PBKDF2(string password, string salt, int iteration)

        {

            byte[] saltBytes = Encoding.UTF8.GetBytes(salt);


            var pbkdf2 = new Rfc2898DeriveBytes(password, saltBytes, iteration);

            var key = pbkdf2.GetBytes(64);


            return Convert.ToBase64String(key);

        }


        bool IDataEncryption.PBKDF2Compare(string passwordHash1, string passwordHash2)

        {

            if (passwordHash1 == null || passwordHash2 == null) { return false; }


            int minLength = Math.Min(passwordHash1.Length, passwordHash2.Length);

            int result = 0;


            for (int i = 0; i < minLength; i++)

            {

                result |= passwordHash1[i] ^ passwordHash2[i];

            }


            return 0 == result;

        }

        

    }

저작자 표시 비영리 동일 조건 변경 허락
신고

Posted by jangjeonghun

티스토리 툴바