コード例 #1
0
        private static string DecryptForObscurity(string dataToDecrypt, int randomization)
        {
            if (string.IsNullOrWhiteSpace(dataToDecrypt))
            {
                return(dataToDecrypt);
            }

            byte[]       decryptedDataBytes = null;
            MemoryStream mstream            = null;
            CryptoStream cstream            = null;

            try
            {
                byte[] key = RandomStringGenerator.GetBytesFromString(encryptKeyArray[randomization]);
                byte[] iv  = RandomStringGenerator.GetBytesFromString(initVectorArray[randomization]);

                byte[] encryptedBytes = RandomStringGenerator.FromEncodedString(dataToDecrypt);

                TripleDESCryptoServiceProvider des3provider = new TripleDESCryptoServiceProvider();
                des3provider.Mode = CipherMode.CBC;
                ICryptoTransform des3decrypt = des3provider.CreateDecryptor(key, iv);

                mstream = new MemoryStream();
                cstream = new CryptoStream(mstream, des3decrypt, CryptoStreamMode.Write);
                cstream.Write(encryptedBytes, 0, encryptedBytes.Length);
                cstream.FlushFinalBlock();

                decryptedDataBytes = mstream.ToArray();
            }
            catch
            {
                //swallow errors
            }
            finally
            {
                if (mstream != null)
                {
                    mstream.Close();
                }
                if (cstream != null)
                {
                    cstream.Close();
                }
            }

            if (decryptedDataBytes == null)
            {
                return(null);
            }
            else
            {
                return(RandomStringGenerator.GetStringFromBytes(decryptedDataBytes));
            }
        }
コード例 #2
0
        public static string Decrypt(string dataToDecrypt)
        {
            if (string.IsNullOrWhiteSpace(dataToDecrypt))
            {
                return(dataToDecrypt);
            }

            byte[]       decryptedDataBytes = null;
            MemoryStream mstream            = null;
            CryptoStream cstream            = null;

            try
            {
                byte[] encryptedBytes = RandomStringGenerator.FromEncodedString(dataToDecrypt);
                var    des3Decrypt    = CreateDecryptor();

                mstream = new MemoryStream();
                cstream = new CryptoStream(mstream, des3Decrypt, CryptoStreamMode.Write);
                cstream.Write(encryptedBytes, 0, encryptedBytes.Length);
                cstream.FlushFinalBlock();

                decryptedDataBytes = mstream.ToArray();
            }
            catch
            {
                //swallow errors
            }
            finally
            {
                if (mstream != null)
                {
                    mstream.Close();
                }
                if (cstream != null)
                {
                    cstream.Close();
                }
            }

            if (decryptedDataBytes == null)
            {
                return(null);
            }

            return(RandomStringGenerator.GetStringFromBytes(decryptedDataBytes));
        }