Exemple #1
0
        /// <summary>
        /// Encrypt the input string using Triple DES algorithm.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>Encrypted string</returns>
        private static string TdesEncryption(string input)
        {
            byte[]       Results;
            UTF8Encoding UTF8     = new UTF8Encoding();
            string       key      = "V@nP001_2016";
            string       ivString = "C1tYgR0uP";

            byte[] ivBytes = UTF8.GetBytes(ivString);

            MD5 md5 = MD5.Create();

            byte[] keyBytes = md5.ComputeHash(UTF8.GetBytes(key));
            byte[] TDESKey  = new byte[24];
            Array.Copy(keyBytes, TDESKey, 16);
            TripleDES des = TripleDES.Create();

            des.Key     = TDESKey;
            des.IV      = ivBytes;
            des.Mode    = CipherMode.CBC;
            des.Padding = PaddingMode.PKCS7;

            byte[] plainText = UTF8.GetBytes(input);
            try
            {
                ICryptoTransform Encryptor = des.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(plainText, 0, plainText.Length);
            }
            finally
            {
                des.Dispose();
                md5.Dispose();
            }
            return(Convert.ToBase64String(Results));
        }
        internal static byte[] TripleDESKeyWrapEncrypt(byte[] rgbKey, byte[] rgbWrappedKeyData)
        {
            byte[] rgbCKS;

            using (var sha = SHA1.Create())
            {
                // checksum the key
                rgbCKS = sha.ComputeHash(rgbWrappedKeyData);
            }

            // generate a random IV
            byte[] rgbIV = new byte[8];
            using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(rgbIV);
            }

            // rgbWKCS = rgbWrappedKeyData | (first 8 bytes of the hash)
            byte[]           rgbWKCKS  = new byte[rgbWrappedKeyData.Length + 8];
            TripleDES        tripleDES = null;
            ICryptoTransform enc1      = null;
            ICryptoTransform enc2      = null;

            try
            {
                tripleDES = TripleDES.Create();
                // Don't add padding, use CBC mode: for example, a 192 bits key will yield 40 bytes of encrypted data
                tripleDES.Padding = PaddingMode.None;
                enc1 = tripleDES.CreateEncryptor(rgbKey, rgbIV);
                enc2 = tripleDES.CreateEncryptor(rgbKey, s_rgbTripleDES_KW_IV);

                Buffer.BlockCopy(rgbWrappedKeyData, 0, rgbWKCKS, 0, rgbWrappedKeyData.Length);
                Buffer.BlockCopy(rgbCKS, 0, rgbWKCKS, rgbWrappedKeyData.Length, 8);
                byte[] temp1 = enc1.TransformFinalBlock(rgbWKCKS, 0, rgbWKCKS.Length);
                byte[] temp2 = new byte[rgbIV.Length + temp1.Length];
                Buffer.BlockCopy(rgbIV, 0, temp2, 0, rgbIV.Length);
                Buffer.BlockCopy(temp1, 0, temp2, rgbIV.Length, temp1.Length);
                // temp2 = REV (rgbIV | E_k(rgbWrappedKeyData | rgbCKS))
                Array.Reverse(temp2);

                return(enc2.TransformFinalBlock(temp2, 0, temp2.Length));
            }
            finally
            {
                if (enc2 != null)
                {
                    enc2.Dispose();
                }
                if (enc1 != null)
                {
                    enc1.Dispose();
                }
                if (tripleDES != null)
                {
                    tripleDES.Dispose();
                }
            }
        }
Exemple #3
0
        internal static byte[] TripleDESKeyWrapDecrypt(byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData)
        {
            // Check to see whether the length of the encrypted key is reasonable
            if (rgbEncryptedWrappedKeyData.Length != 32 && rgbEncryptedWrappedKeyData.Length != 40 &&
                rgbEncryptedWrappedKeyData.Length != 48)
            {
                throw new CryptographicException(SR.Cryptography_Xml_KW_BadKeySize);
            }

            TripleDES        tripleDES = null;
            ICryptoTransform dec1      = null;
            ICryptoTransform dec2      = null;

            try
            {
                tripleDES = TripleDES.Create();
                // Assume no padding, use CBC mode
                tripleDES.Padding = PaddingMode.None;
                dec1 = tripleDES.CreateDecryptor(rgbKey, s_rgbTripleDES_KW_IV);

                byte[] temp2 = dec1.TransformFinalBlock(rgbEncryptedWrappedKeyData, 0, rgbEncryptedWrappedKeyData.Length);
                Array.Reverse(temp2);
                // Get the IV and temp1
                byte[] rgbIV = new byte[8];
                Buffer.BlockCopy(temp2, 0, rgbIV, 0, 8);
                byte[] temp1 = new byte[temp2.Length - rgbIV.Length];
                Buffer.BlockCopy(temp2, 8, temp1, 0, temp1.Length);

                dec2 = tripleDES.CreateDecryptor(rgbKey, rgbIV);
                byte[] rgbWKCKS = dec2.TransformFinalBlock(temp1, 0, temp1.Length);

                // checksum the key
                byte[] rgbWrappedKeyData = new byte[rgbWKCKS.Length - 8];
                Buffer.BlockCopy(rgbWKCKS, 0, rgbWrappedKeyData, 0, rgbWrappedKeyData.Length);
                using (var sha = SHA1.Create())
                {
                    byte[] rgbCKS = sha.ComputeHash(rgbWrappedKeyData);
                    for (int index = rgbWrappedKeyData.Length, index1 = 0; index < rgbWKCKS.Length; index++, index1++)
                    {
                        if (rgbWKCKS[index] != rgbCKS[index1])
                        {
                            throw new CryptographicException(SR.Cryptography_Xml_BadWrappedKeySize);
                        }
                    }
                    return(rgbWrappedKeyData);
                }
            }
            finally
            {
                dec2?.Dispose();
                dec1?.Dispose();
                tripleDES?.Dispose();
            }
        }
Exemple #4
0
        public void InitializeKey(bool reset = false)
        {
            string keyFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemKey.json");

            void saveKeyResource()
            {
                __KeyResource kr = new __KeyResource
                {
                    Key  = CurrentTripleDES.Key,
                    IV   = CurrentTripleDES.IV,
                    Mode = CurrentTripleDES.Mode,
                };

                File.WriteAllText(keyFile, JsonConvert.SerializeObject(kr));
            }

            if (reset)
            {
                CurrentTripleDES.Dispose();
                CurrentTripleDES = TripleDES.Create();
                saveKeyResource();
                return;
            }

            if (File.Exists(keyFile))
            {
                try
                {
                    var kr = JsonConvert.DeserializeObject <__KeyResource>(File.ReadAllText(keyFile));
                    CurrentTripleDES.Key  = kr.Key;
                    CurrentTripleDES.IV   = kr.IV;
                    CurrentTripleDES.Mode = kr.Mode;
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, ex.Message);
                    saveKeyResource();
                }
            }
            else
            {
                saveKeyResource();
            }
        }
Exemple #5
0
        /// <summary>
        /// Decrypt the encrypted string using Triple DES algorithm.
        /// </summary>
        /// <param name="password">The password.</param>
        /// <returns>decrypted string</returns>
        private static string TdesDecryption(string password)
        {
            byte[]       Results;
            UTF8Encoding UTF8     = new UTF8Encoding();
            string       key      = "V@nP001_2016";
            String       ivString = "C1tYgR0uP";

            byte[] ivBytes = UTF8.GetBytes(ivString);

            MD5 md5 = MD5.Create();

            byte[] keyBytes = md5.ComputeHash(UTF8.GetBytes(key));
            byte[] TDESKey  = new byte[24];
            Array.Copy(keyBytes, TDESKey, 16);

            TripleDES des = TripleDES.Create();

            des.Key     = TDESKey;
            des.IV      = ivBytes;
            des.Mode    = CipherMode.CBC;
            des.Padding = PaddingMode.PKCS7;

            byte[] DataToDecrypt = Convert.FromBase64String(password);

            try
            {
                ICryptoTransform Decryptor = des.CreateDecryptor();
                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            finally
            {
                des.Dispose();
                md5.Dispose();
            }
            return(UTF8.GetString(Results));
        }
Exemple #6
0
 public void Dispose() => des.Dispose();