Пример #1
0
        /// <summary>
        /// 暗号化された文字列を復号化する
        /// </summary>
        public static string DecryptString(string str, string password)
        {
            var aes = new System.Security.Cryptography.AesCryptoServiceProvider();

            var passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);

            aes.Key = ResizeBytesArray(passwordBytes, aes.Key.Length);
            aes.IV  = ResizeBytesArray(passwordBytes, aes.IV.Length);

            var data        = System.Convert.FromBase64String(str);
            var stream      = new System.IO.MemoryStream(data);
            var decryptor   = aes.CreateDecryptor();
            var cryptStreem = new System.Security.Cryptography.CryptoStream(
                stream,
                decryptor,
                System.Security.Cryptography.CryptoStreamMode.Read
                );
            var reader = new System.IO.StreamReader(
                cryptStreem,
                System.Text.Encoding.UTF8
                );

            try {
                var result = reader.ReadToEnd();
                return(result);
            } finally {
                reader.Close();
                cryptStreem.Close();
                stream.Close();
            }
        }
Пример #2
0
        public string Decrypt(byte[] inputDataBytes, byte[] keyBytes, byte[] ivBytes)
        {
            if (inputDataBytes == null || inputDataBytes.Length <= 0)
            {
                throw new ArgumentNullException("plainText");
            }
            if (keyBytes == null || keyBytes.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }
            if (ivBytes == null || ivBytes.Length <= 0)
            {
                throw new ArgumentNullException("IV");
            }

            System.IO.MemoryStream memoryStream;

            using (System.Security.Cryptography.AesCryptoServiceProvider csp = new System.Security.Cryptography.AesCryptoServiceProvider())
            {
                csp.Key = keyBytes;
                csp.IV  = ivBytes;
                //csp.CreateEncryptor

                // Create an encryptor to perform the stream transform.
                System.Security.Cryptography.ICryptoTransform cryptoTransformer = csp.CreateDecryptor(csp.Key, csp.IV);

                // Create the streams used for encryption.
                using (memoryStream = new System.IO.MemoryStream(inputDataBytes))
                    using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, cryptoTransformer, System.Security.Cryptography.CryptoStreamMode.Read))
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(cryptoStream))
                        {
                            return(sr.ReadToEnd());
                        }
            }
        }
Пример #3
0
    public static string Decrypt_Aes(this object value, string aesKey, string aesIV)
    {
        byte[] cipherText = GetBytes(value as string);
            byte[] Key = GetBytes(aesKey);
            byte[] IV = GetBytes(aesIV);
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an AesCryptoServiceProvider object
            // with the specified key and IV.
            using (System.Security.Cryptography.AesCryptoServiceProvider aesAlg = new System.Security.Cryptography.AesCryptoServiceProvider())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;

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

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, decryptor, System.Security.Cryptography.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;
    }
Пример #4
0
        //-------------------------------------------------

        public static string AesDecrypt(string password, string base64)
        {
            // Turn input string into a byte array.
            var input = System.Convert.FromBase64String(base64);
            // Create an instance of the Rijndael class.
            var cipher = new System.Security.Cryptography.AesCryptoServiceProvider();
            // Calculate salt to make it harder to guess key by using a dictionary attack.
            var passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
            var hmac          = new System.Security.Cryptography.HMACSHA1(passwordBytes);
            var salt          = hmac.ComputeHash(passwordBytes);
            // Generate Secret Key from the password and salt.
            // Note: Set number of iterations to 10 in order for JavaScript example to work faster.
            var secretKey = new System.Security.Cryptography.Rfc2898DeriveBytes(passwordBytes, salt, 10);
            // Create a encryptor from the existing SecretKey bytes by using
            // 32 bytes (256 bits) for the secret key and
            // 16 bytes (128 bits) for the initialization vector (IV).
            var key = secretKey.GetBytes(32);
            var iv  = secretKey.GetBytes(16);
            // Get cryptor as System.Security.Cryptography.ICryptoTransform class.
            var cryptor = cipher.CreateDecryptor(key, iv);
            // Create new Input.
            var inputBuffer = new System.Byte[input.Length];

            // Copy data bytes to input buffer.
            System.Buffer.BlockCopy(input, 0, inputBuffer, 0, inputBuffer.Length);
            // Create a MemoryStream to hold the output bytes.
            var stream = new System.IO.MemoryStream();
            // Create a CryptoStream through which we are going to be processing our data.
            var mode         = System.Security.Cryptography.CryptoStreamMode.Write;
            var cryptoStream = new System.Security.Cryptography.CryptoStream(stream, cryptor, mode);

            // Start the decrypting process.
            cryptoStream.Write(inputBuffer, 0, inputBuffer.Length);
            // Finish decrypting.
            cryptoStream.FlushFinalBlock();
            // Convert data from a memoryStream into a byte array.
            var outputBuffer = stream.ToArray();

            // Close both streams.
            stream.Close();
            //cryptoStream.Close();
            // Convert encrypted data into a base64-encoded string.
            var s = System.Text.Encoding.UTF8.GetString(outputBuffer);

            return(s);
        }
Пример #5
0
 public static byte[] AESDecrypt(byte[] input)
 {
     using (var aes = new System.Security.Cryptography.AesCryptoServiceProvider())
     {
         aes.Key = aeskey;
         aes.IV  = aesvector;
         using (var ms = new System.IO.MemoryStream())
         {
             var cs = new System.Security.Cryptography.CryptoStream(ms, aes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
             if (cs == null)
             {
                 return(null);
             }
             cs.Write(input, 0, input.Length);
             cs.FlushFinalBlock();
             return(ms.ToArray());
         }
     }
 }
Пример #6
0
        public static string aesDecryptBase64(string SourceStr, ulong Key)
        {
            string decrypt = "";

            try
            {
                var    aes    = new System.Security.Cryptography.AesCryptoServiceProvider();
                var    md5    = new System.Security.Cryptography.MD5CryptoServiceProvider();
                var    sha256 = new System.Security.Cryptography.SHA256CryptoServiceProvider();
                byte[] key    = sha256.ComputeHash(BitConverter.GetBytes(Key));
                byte[] iv     = md5.ComputeHash(BitConverter.GetBytes(Key));
                aes.Key = key;
                aes.IV  = iv;

                byte[] dataByteArray = Convert.FromBase64String(SourceStr);
                using (var ms = new System.IO.MemoryStream())
                {
                    using (var cs = new System.Security.Cryptography.CryptoStream(ms, aes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        cs.Write(dataByteArray, 0, dataByteArray.Length);
                        cs.FlushFinalBlock();
                        decrypt = Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
            }
            catch (Exception e)
            {
            }
            return(decrypt);
        }