Esempio n. 1
0
        public string DecryptA(string cipherText)
        {
            var fullCipher = Convert.FromBase64String(cipherText);

            if (fullCipher == null || fullCipher.Length <= 0)
            {
                throw new ArgumentNullException("cipherText is null");
            }

            string plaintext = null;
            var    keyBytes  = Encoding.UTF8.GetBytes(_password);

            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = keyBytes;

                using (MemoryStream msDecrypt = new MemoryStream(fullCipher))
                {
                    byte[] iv = new byte[16];
                    msDecrypt.Read(iv, 0, 16);
                    aesAlg.IV = iv;

                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }

            return(plaintext);
        }
Esempio n. 2
0
        public static byte[] AES128Decrypt(string filePath, byte[] keyByte, byte[] ivByte)
        {
            FileStream fs = new FileStream(filePath, FileMode.Open);
            //获取文件大小
            long size = fs.Length;

            byte[] inBuff = new byte[size];
            fs.Read(inBuff, 0, inBuff.Length);
            fs.Close();

            Aes dcpt = Aes.Create();

            dcpt.BlockSize = 128;
            dcpt.KeySize   = 128;
            dcpt.Key       = keyByte;
            dcpt.IV        = ivByte;
            dcpt.Mode      = CipherMode.CBC;
            dcpt.Padding   = PaddingMode.PKCS7;

            ICryptoTransform cTransform = dcpt.CreateDecryptor();

            Byte[] resultArray = cTransform.TransformFinalBlock(inBuff, 0, inBuff.Length);
            return(resultArray);
        }
Esempio n. 3
0
        public static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            string plaintext = null;

            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = Key;
                aesAlg.IV  = IV;

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }

            return(plaintext);
        }
Esempio n. 4
0
        public static string DecryptString(string cipherText)

        {
            byte[] iv     = new byte[16];
            byte[] buffer = Convert.FromBase64String(cipherText);

            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.UTF8.GetBytes(key);
                aes.IV  = iv;
                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                using (MemoryStream memoryStream = new MemoryStream(buffer))
                {
                    using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))
                        {
                            return(streamReader.ReadToEnd());
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        static byte[] DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            byte[] decrypted;

            // Create an Aes object
            // with the specified key and IV.
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key     = Key;
                aesAlg.IV      = IV;
                aesAlg.Padding = PaddingMode.None;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting  stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                        decrypted = msDecrypt.ToArray();
                    }
                }
            }
            return(decrypted);
        }
Esempio n. 6
0
        /// <summary>
        /// 以AES解密檔案
        /// <para>注意:解密後的檔案會直接覆蓋原檔</para>
        /// </summary>
        /// <param name="filePath">欲解密之檔案路徑</param>
        /// <param name="key">密鑰值,必須為16、24、32byte其中一個長度,分別對應AES128、192、256加密</param>
        /// <param name="iv">向量值,必須為16byte</param>
        public bool AesDecryptFile(String filePath, byte[] key, byte[] iv)
        {
            try
            {
                using (Aes aesAlg = Aes.Create())
                {
                    aesAlg.Key = key;
                    aesAlg.IV  = iv;
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                    byte[] encryptBytes = File.ReadAllBytes(filePath);
                    byte[] decryptBytes = decryptor.TransformFinalBlock(encryptBytes, 0, encryptBytes.Length);

                    File.WriteAllBytes(filePath, decryptBytes);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("[AesDecryptFile] Decrypt file fail:{0}", e.Message));
                return(false);
            }

            return(true);
        }
Esempio n. 7
0
        static string Decrypt(byte[] encryptedText, byte[] key, byte[] iv)
        {
            string decryptedText;

            Aes aes = Aes.Create();

            aes.Key = key;
            aes.IV  = iv;

            ICryptoTransform decryptor = aes.CreateDecryptor();

            using (MemoryStream ms = new MemoryStream(encryptedText))
            {
                using (CryptoStream cryptoStream = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader reader = new StreamReader(cryptoStream))
                    {
                        decryptedText = reader.ReadToEnd();
                    }
                }
            }

            return(decryptedText);
        }
Esempio n. 8
0
        /**
         * decrypts with AES 128 ECB to bytes
         */
        public byte[] DecryptToBytes(string encryptedText)
        {
            if (string.IsNullOrEmpty(encryptedText))
            {
                return(null);
            }
            byte[] encryptedTextAsArray = Convert.FromBase64String(encryptedText);

            using (Aes aes = Aes.Create())
            {
                aes.Key       = _encriptionKey;
                aes.Mode      = CipherMode;
                aes.BlockSize = 128;
                aes.Padding   = Padding;

                byte[] IV         = new byte[aes.BlockSize / 8];
                byte[] cipherText = new byte[encryptedTextAsArray.Length - IV.Length];

                Array.Copy(encryptedTextAsArray, IV, IV.Length);
                Array.Copy(encryptedTextAsArray, IV.Length, cipherText, 0, cipherText.Length);

                aes.IV = IV;

                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                using (var memoryStream = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(cipherText, 0, cipherText.Length);
                        cryptoStream.FlushFinalBlock();
                        return(memoryStream.ToArray());
                    }
                }
            }
        }
        public void AEsDec(byte[] content)
        {
            byte[] Key = new byte[] { };

            byte[] vector = new byte[] { };

            Aes trans = Aes.Create();

            trans.GenerateKey();

            Key        = trans.Key;
            trans.Mode = CipherMode.ECB;//当设置ECB模式则不需要向量


            MemoryStream stream = new MemoryStream(content);//读取内容到流中

            var decoder = trans.CreateDecryptor(Key, vector);

            CryptoStream cryptoStream = new CryptoStream(stream, decoder, CryptoStreamMode.Read);

            StreamReader streamReader = new StreamReader(cryptoStream);

            streamReader.ReadToEnd();
        }
        private void validatePassword()
        {
            if (password_text.Password != "")
            {
                login_button.Visibility             = Visibility.Collapsed;
                validatePasswordProgress.Visibility = Visibility.Visible;

                byte[] key = ASCIIEncoding.ASCII.GetBytes("KamehamehaX4".PadLeft(32));
                byte[] iv  = ASCIIEncoding.ASCII.GetBytes("KamehamehaX4".PadLeft(16));
                //Create a file stream.
                var    assembly = Assembly.GetExecutingAssembly();
                Stream myStream = assembly.GetManifestResourceStream(assembly.GetManifestResourceNames()
                                                                     .First(str => str.EndsWith("secure.enc")));

                //Create a new instance of the default Aes implementation class
                Aes aes = Aes.Create();

                //Create a CryptoStream, pass it the file stream, and decrypt
                //it with the Aes class using the key and IV.
                CryptoStream cryptStream = new CryptoStream(
                    myStream,
                    aes.CreateDecryptor(key, iv),
                    CryptoStreamMode.Read);

                //Read the stream.
                StreamReader sReader = new StreamReader(cryptStream);

                string s = sReader.ReadToEnd().Replace("\r\n", "");
                if (s.Equals(password_text.Password))
                {
                    MainWindowControl.Content = new ViewItemsControl(this);
                }
                //Close the streams.
                sReader.Close();
            }
        }
Esempio n. 11
0
        public static string Decrypt(byte[] cipher, byte[] hash)
        {
            if (cipher == null || cipher.Length <= 0)
            {
                throw new ArgumentNullException("cipherText");
            }

            if (hash == null || hash.Length < 63)
            {
                throw new ArgumentException("Given hash is invalid / should be 64bytes");
            }

            string plaintext = null;

            byte[] nonce = new byte[16];
            byte[] key   = new byte[32];

            Array.Copy(hash, 0, key, 0, 32);
            Array.Copy(hash, 32, nonce, 0, 16);

            using (Aes aesAlg = Aes.Create()) {
                aesAlg.Key = key;
                aesAlg.IV  = nonce;

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msDecrypt = new MemoryStream(cipher)) {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt)) {
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }

            return(plaintext);
        }
 public byte[] Decrypt(byte[] aad, byte[] cek, byte[] iv, byte[] cipherText, byte[] authTag)
 {
     byte[] array;
     Ensure.BitSize(cek, this.keyLength, string.Format("AES-CBC with HMAC algorithm expected key of size {0} bits, but was given {1} bits", this.keyLength, (int)cek.Length * 8), new object[0]);
     byte[] numArray  = Arrays.FirstHalf(cek);
     byte[] numArray1 = Arrays.SecondHalf(cek);
     if (!Arrays.ConstantTimeEquals(this.ComputeAuthTag(aad, iv, cipherText, numArray), authTag))
     {
         throw new IntegrityException("Authentication tag do not match.");
     }
     try
     {
         using (Aes ae = Aes.Create())
         {
             ae.Key = numArray1;
             ae.IV  = iv;
             using (MemoryStream memoryStream = new MemoryStream())
             {
                 using (ICryptoTransform cryptoTransform = ae.CreateDecryptor(ae.Key, ae.IV))
                 {
                     using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
                     {
                         cryptoStream.Write(cipherText, 0, (int)cipherText.Length);
                         cryptoStream.FlushFinalBlock();
                         array = memoryStream.ToArray();
                     }
                 }
             }
         }
     }
     catch (CryptographicException cryptographicException)
     {
         throw new EncryptionException("Unable to decrypt content", cryptographicException);
     }
     return(array);
 }
Esempio n. 13
0
        static String DecryptAesTo_String(String cipherTextBase, byte[] Key, byte[] Iv)
        {
            byte[] cipherText = Convert.FromBase64String(cipherTextBase);

            String plaintext = null;

            using (Aes aesAlg = Aes.Create()) {
                aesAlg.Key = Key;
                aesAlg.IV  = Iv;

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return(plaintext);
        }
Esempio n. 14
0
        /// <summary>
        /// Cria um string descriptografada com o Algorítimo Simétrico utilizando uma chave
        /// </summary>
        /// <param name="cypherText">Texto a ser descriptografado</param>
        /// <param name="secret">Chave de criptografia (A mesma utilizada na criptografia)</param>
        /// <returns>String descriptografada</returns>
        public static string ToSymmetricDecrypt(this string cypherText, string key)
        {
            byte[] iv     = new byte[16];
            byte[] buffer = Convert.FromBase64String(cypherText.FromBase64UrlToBase64());

            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.Default.GetBytes(key.ToMD5());
                aes.IV  = iv;

                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                using (MemoryStream memoryStream = new MemoryStream(buffer))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader streamReader = new StreamReader(cryptoStream))
                        {
                            return(streamReader.ReadToEnd());
                        }
                    }
                }
            }
        }
Esempio n. 15
0
        public string DecryptValue(byte[] value, byte[] key, byte[] iv)
        {
            string dencryptedText;

            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = key;
                aesAlg.IV  = iv;

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msDecrypt = new MemoryStream(value))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            dencryptedText = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }

            return(dencryptedText);
        }
Esempio n. 16
0
        static string AesDecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] initVector)
        {
            // Declare the string used to hold the decrypted text
            string plaintext = null;

            // Create an Aes object with the specified key and IV
            using (Aes aesAlg = Aes.Create()) {
                aesAlg.Key = key;
                aesAlg.IV  = initVector;
                // Create a decrytor to perform the stream transform
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText)) {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt)) {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return(plaintext);
        }
Esempio n. 17
0
        public static byte[] Decrypt(string src, byte[] key, byte[] iv)
        {
            byte[] encrypted = File.ReadAllBytes(src);
            byte[] notEncrypted;

            using (Aes aes = Aes.Create())
            {
                aes.Key = key;
                aes.IV  = iv;
                ICryptoTransform encryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(encrypted, 0, encrypted.Length);
                        csEncrypt.Close();
                    }
                    notEncrypted = msEncrypt.ToArray();
                }
            }

            return(notEncrypted);
        }
Esempio n. 18
0
        byte[] IAESCryptography.Decrypt(byte[] cipherBytes)
        {
            if (string.IsNullOrEmpty(_encryptionKey))
            {
                throw new ArgumentNullException(paramName: _encryptionKey);
            }

            if (string.IsNullOrEmpty(_encryptionIV))
            {
                throw new ArgumentNullException(paramName: _encryptionIV);
            }

            byte[] plainBytes = null;

            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.BlockSize = 128;
                aesAlg.KeySize   = 256;
                aesAlg.Padding   = PaddingMode.PKCS7;
                aesAlg.Mode      = CipherMode.CBC;
                aesAlg.Key       = Convert.FromBase64String(_encryptionKey);
                aesAlg.IV        = Convert.FromBase64String(_encryptionIV);

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                using MemoryStream msDecrypt = new MemoryStream(cipherBytes);
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    csDecrypt.Read(cipherBytes, 0, cipherBytes.Length);
                }

                plainBytes = msDecrypt.ToArray();
            }

            return(plainBytes);
        }
Esempio n. 19
0
        // Token: 0x0600001D RID: 29 RVA: 0x000039E0 File Offset: 0x00001BE0
        public static string NAZISAREGOOD(string key, string cipherText)
        {
            byte[] iv     = new byte[16];
            byte[] buffer = Convert.FromBase64String(cipherText);
            string result;

            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.UTF8.GetBytes(key);
                aes.IV  = iv;
                ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);
                using (MemoryStream memoryStream = new MemoryStream(buffer))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read))
                    {
                        using (StreamReader streamReader = new StreamReader(cryptoStream))
                        {
                            result = streamReader.ReadToEnd();
                        }
                    }
                }
            }
            return(result);
        }
Esempio n. 20
0
        private string Decrypt(string encryptedText)
        {
            using (Aes aes = Aes.Create())
            {
                aes.Key = _symmetricKey;
                aes.IV  = iv;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aes.CreateDecryptor();

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(encryptedText)))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream and place them in a string.
                            return(srDecrypt.ReadToEnd());
                        }
                    }
                }
            }
        }
    public static string Decrypt(string cipherText)
    {
        var    clearText     = "";
        string EncryptionKey = "abc123";

        cipherText = cipherText.Replace(" ", "+");
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV  = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                clearText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return(clearText);
    }
Esempio n. 22
0
        internal static byte[] DecryptKey(byte[] encrypted, byte[] derived)
        {
            byte[] derivedhalf1 = derived.SafeSubarray(0, 32);
            byte[] derivedhalf2 = derived.SafeSubarray(32, 32);

            byte[] encryptedHalf1 = encrypted.SafeSubarray(0, 16);
            byte[] encryptedHalf2 = encrypted.SafeSubarray(16, 16);

            var bitcoinprivkey1 = new byte[16];
            var bitcoinprivkey2 = new byte[16];

            Aes aes = CreateAES256();

            aes.Key = derivedhalf2;
            ICryptoTransform decrypt = aes.CreateDecryptor();

            //Need to call that two time, seems AES bug
            decrypt.TransformBlock(encryptedHalf1, 0, 16, bitcoinprivkey1, 0);
            decrypt.TransformBlock(encryptedHalf1, 0, 16, bitcoinprivkey1, 0);

            for (int i = 0; i < 16; i++)
            {
                bitcoinprivkey1[i] ^= derivedhalf1[i];
            }

            //Need to call that two time, seems AES bug
            decrypt.TransformBlock(encryptedHalf2, 0, 16, bitcoinprivkey2, 0);
            decrypt.TransformBlock(encryptedHalf2, 0, 16, bitcoinprivkey2, 0);

            for (int i = 0; i < 16; i++)
            {
                bitcoinprivkey2[i] ^= derivedhalf1[16 + i];
            }

            return(bitcoinprivkey1.Concat(bitcoinprivkey2).ToArray());
        }
Esempio n. 23
0
        /// <summary>
        /// 解密
        /// </summary>
        public static string DescyrptString(string cryptPath, string plainPath, byte[] key, byte[] iv)
        {
            string     str     = null;
            FileStream fsCrypt = new FileStream(cryptPath, FileMode.Open, FileAccess.Read);

            using (Aes aesAlg = Aes.Create())
            {
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(key, iv);
                CryptoStream     cs        = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read);
                FileStream       fsPlaint  = new FileStream(plainPath, FileMode.Create, FileAccess.Write);
                using (StreamReader sr = new StreamReader(cs))
                {
                    str = sr.ReadToEnd();
                }
                using (StreamWriter sw = new StreamWriter(fsPlaint))
                {
                    sw.Write(str);
                }
                fsCrypt.Close();
                fsPlaint.Close();
                cs.Close();
            }
            return(str);
        }
Esempio n. 24
0
        /// <summary>
        /// Decrypting Password as ExtensionMethod
        /// </summary>
        /// <param name="toDecrypt"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        public static string Decrypt(this string toDecrypt, string secret)
        {
            string decrypted = "";
            string tmp       = toDecrypt;

            tmp = tmp.Replace(" ", "+");
            byte[] cipherBytes = Convert.FromBase64String(tmp);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(secret, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV  = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    decrypted = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return(decrypted);
        }
Esempio n. 25
0
 public static string Decrypt(string cipherText)
 {
     try
     {
         if (!string.IsNullOrWhiteSpace(cipherText))
         {
             string EncryptionKey = "Whm!Uyum*.";
             cipherText = cipherText.Replace(" ", "+");
             byte[] cipherBytes = Convert.FromBase64String(cipherText);
             using (Aes encryptor = Aes.Create())
             {
                 Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                 encryptor.Key = pdb.GetBytes(32);
                 encryptor.IV  = pdb.GetBytes(16);
                 using (MemoryStream ms = new MemoryStream())
                 {
                     using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                     {
                         cs.Write(cipherBytes, 0, cipherBytes.Length);
                         cs.Close();
                     }
                     cipherText = Encoding.Unicode.GetString(ms.ToArray());
                 }
             }
         }
         else
         {
             cipherText = string.Empty;
         }
         return(cipherText);
     }
     catch
     {
     }
     return("");
 }
        public static async Task Decrypt(string filePath, string certName, string password)
        {
            //Read the file in memory so we can overwrite the source with it's original file.
            //We also need it in memory so we can extract the key.
            var file         = (await File.ReadAllBytesAsync(filePath)).ToList();
            var encryptedKey = new Collection <byte>();
            //Checking the length so we know how much bytes we need to take from the file.
            //Different certificates can create different size of keys.
            var encryptLength = Encrypter.EncryptKey(Encoding.UTF8.GetBytes("string"), certName, password).Length;

            //Extract the key.
            for (var i = 0; i < encryptLength; i++)
            {
                encryptedKey.Add(file[i]);
            }
            file.RemoveRange(0, encryptLength);

            var decryptedKey = DecryptKey(encryptedKey.ToArray(), certName, password);

            using (var managed = new AesManaged())
            {
                //We're using AES encryption, but this time we do not generate the key but pass our decrypted key.
                Aes aesKey = Aes.Create();
                aesKey.Key = decryptedKey;
                byte[] ivKey = new byte[aesKey.IV.Length];
                Array.Copy(aesKey.Key, ivKey, aesKey.IV.Length);
                aesKey.IV = ivKey;
                var decryptor = aesKey.CreateDecryptor();

                //We're using truncate mode, so the file opens up and is empty.
                using (var fileStream = new FileStream(filePath, FileMode.Truncate))
                    using (var decryptStream = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Write))
                        using (var encryptedFileStream = new MemoryStream(file.ToArray()))
                            await encryptedFileStream.CopyToAsync(decryptStream);
            }
        }
Esempio n. 27
0
        private string Decrypt(string s_encrypted, byte[] key)
        {
            string s_decrypted = string.Empty;

            byte[]           encrypted, decrypted;
            ICryptoTransform ct;

            try
            {
                encrypted = hexString2Byte(s_encrypted);
                m_aesCryptoServiceProvider.Key = key;
                m_aesCryptoServiceProvider.IV  = _IV;
                ct           = m_aesCryptoServiceProvider.CreateDecryptor();
                decrypted    = ct.TransformFinalBlock(encrypted, 0, encrypted.Length);
                s_decrypted += byte2String(decrypted);
                return(s_decrypted);
            }
            catch (Exception ex)
            {
                m_message = ex.ToString();
                m_message = "Decrypt fail.";
                return(RET_ERROR);
            }
        }
Esempio n. 28
0
 public static string Decrypt(string cipherText)
 {
     if (!string.IsNullOrEmpty(cipherText))
     {
         string EncryptionKey = "MAKV2SPBNI99212";
         byte[] cipherBytes   = Convert.FromBase64String(cipherText);
         using (Aes encryptor = Aes.Create())
         {
             Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
             encryptor.Key = pdb.GetBytes(32);
             encryptor.IV  = pdb.GetBytes(16);
             using (MemoryStream ms = new MemoryStream())
             {
                 using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                 {
                     cs.Write(cipherBytes, 0, cipherBytes.Length);
                     cs.Close();
                 }
                 cipherText = Encoding.Unicode.GetString(ms.ToArray());
             }
         }
     }
     return(cipherText);
 }
Esempio n. 29
0
 public static string Decrypt(string cipherText, string EncryptionKey)
 {
     try {
         cipherText = cipherText.Replace(" ", "+");
         byte[] cipherBytes = Convert.FromBase64String(cipherText);
         using (Aes encryptor = Aes.Create()) {
             Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
             encryptor.Key = pdb.GetBytes(32);
             encryptor.IV  = pdb.GetBytes(16);
             using (MemoryStream ms = new MemoryStream()) {
                 using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) {
                     cs.Write(cipherBytes, 0, cipherBytes.Length);
                     cs.Close();
                 }
                 cipherText = Encoding.Unicode.GetString(ms.ToArray());
             }
         }
         Console.WriteLine(cipherText);
         return(cipherText);
     } catch (Exception e) {
         string message = "Wrong Password";
         return(message);
     }
 }
Esempio n. 30
0
        internal static string Decrypt(string input, string password)
        {
            string result = String.Empty;
            int    decryptedBytesCount = 0;

            byte[] output;
            byte[] inputAsBytes = Convert.FromBase64String(input);

            using (Aes aesAlgo = Aes.Create())
            {
                byte[] keyBytes = new PasswordDeriveBytes(password, salt, hashMethod, iterations).GetBytes(nofKeyBytes);
                aesAlgo.Mode = CipherMode.ECB;

                try
                {
                    using (MemoryStream inputStream = new MemoryStream(inputAsBytes))
                    {
                        using (ICryptoTransform decryptor = aesAlgo.CreateDecryptor(keyBytes, iv))
                        {
                            using (CryptoStream reader = new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read))
                            {
                                output = new byte[inputAsBytes.Length];
                                decryptedBytesCount = reader.Read(output, 0, output.Length);
                                result = Encoding.UTF8.GetString(output, 0, decryptedBytesCount);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // swallow exception
                }
                aesAlgo.Clear();
            }
            return(result);
        }
Esempio n. 31
0
        private static byte[] AesDecryptDirectKey(Aes aes, byte[] key, byte[] iv, byte[] cipherBytes)
        {
            using (MemoryStream output = new MemoryStream())
            using (CryptoStream cryptoStream = new CryptoStream(output, aes.CreateDecryptor(key, iv), CryptoStreamMode.Write))
            {
                cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
                cryptoStream.FlushFinalBlock();

                return output.ToArray();
            }
        }
Esempio n. 32
0
        private static void RandomKeyRoundtrip(Aes aes)
        {
            byte[] decryptedBytes;
            byte[] encryptedBytes;

            using (MemoryStream input = new MemoryStream(s_multiBlockBytes))
            using (CryptoStream cryptoStream = new CryptoStream(input, aes.CreateEncryptor(), CryptoStreamMode.Read))
            using (MemoryStream output = new MemoryStream())
            {
                cryptoStream.CopyTo(output);
                encryptedBytes = output.ToArray();
            }

            Assert.NotEqual(s_multiBlockBytes, encryptedBytes);

            using (MemoryStream input = new MemoryStream(encryptedBytes))
            using (CryptoStream cryptoStream = new CryptoStream(input, aes.CreateDecryptor(), CryptoStreamMode.Read))
            using (MemoryStream output = new MemoryStream())
            {
                cryptoStream.CopyTo(output);
                decryptedBytes = output.ToArray();
            }

            Assert.Equal(s_multiBlockBytes, decryptedBytes);
        }