當前位置: 首頁>>代碼示例>>C#>>正文


C# AesManaged.GetType方法代碼示例

本文整理匯總了C#中System.Security.Cryptography.AesManaged.GetType方法的典型用法代碼示例。如果您正苦於以下問題:C# AesManaged.GetType方法的具體用法?C# AesManaged.GetType怎麽用?C# AesManaged.GetType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Security.Cryptography.AesManaged的用法示例。


在下文中一共展示了AesManaged.GetType方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: File

        private async static Task File(string inFile, string outFile, RSACryptoServiceProvider key)
        {
            using (AesManaged aesManaged = new AesManaged())
            {
                // Create instance of AesManaged for
                // symetric encryption of the data.
                aesManaged.KeySize = 256;
                aesManaged.BlockSize = 128;
                aesManaged.Mode = CipherMode.CBC;
                using (ICryptoTransform transform = aesManaged.CreateEncryptor())
                {
                    RSAPKCS1KeyExchangeFormatter keyFormatter = new RSAPKCS1KeyExchangeFormatter(key);
                    byte[] keyEncrypted = keyFormatter.CreateKeyExchange(aesManaged.Key, aesManaged.GetType());

                    // Create byte arrays to contain
                    // the length values of the key and IV.
                    byte[] LenK = new byte[4];
                    byte[] LenIV = new byte[4];

                    int lKey = keyEncrypted.Length;
                    LenK = BitConverter.GetBytes(lKey);
                    int lIV = aesManaged.IV.Length;
                    LenIV = BitConverter.GetBytes(lIV);

                    // Write the following to the FileStream
                    // for the encrypted file (outFs):
                    // - length of the key
                    // - length of the IV
                    // - ecrypted key
                    // - the IV
                    // - the encrypted cipher content
                    using (FileStream outFs = new FileStream(outFile, FileMode.Create))
                    {

                        await outFs.WriteAsync(LenK, 0, 4);
                        await outFs.WriteAsync(LenIV, 0, 4);
                        await outFs.WriteAsync(keyEncrypted, 0, lKey);
                        await outFs.WriteAsync(aesManaged.IV, 0, lIV);

                        // Now write the cipher text using
                        // a CryptoStream for encrypting.
                        using (CryptoStream outStreamEncrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
                        {

                            // By encrypting a chunk at
                            // a time, you can save memory
                            // and accommodate large files.
                            int count = 0;
                            int offset = 0;

                            // blockSizeBytes can be any arbitrary size.
                            int blockSizeBytes = aesManaged.BlockSize / 8;
                            byte[] data = new byte[blockSizeBytes];
                            int bytesRead = 0;

                            using (FileStream inFs = new FileStream(inFile, FileMode.Open))
                            {
                                do
                                {
                                    count = await inFs.ReadAsync(data, 0, blockSizeBytes);
                                    offset += count;
                                    await outStreamEncrypted.WriteAsync(data, 0, count);
                                    bytesRead += blockSizeBytes;
                                }
                                while (count > 0);
                            }
                        }
                    }
                }
            }
        }
開發者ID:vitroums,項目名稱:inzynierka,代碼行數:71,代碼來源:Encrypt.cs


注:本文中的System.Security.Cryptography.AesManaged.GetType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。