当前位置: 首页>>代码示例>>C#>>正文


C# CryptoStream.Write方法代码示例

本文整理汇总了C#中CryptoStream.Write方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.Write方法的具体用法?C# CryptoStream.Write怎么用?C# CryptoStream.Write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CryptoStream的用法示例。


在下文中一共展示了CryptoStream.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestKnownEnc

    static Boolean TestKnownEnc(Aes aes, Byte[] Key, Byte[] IV, Byte[] Plain, Byte[] Cipher)
    {

        Byte[]  CipherCalculated;
        
        Console.WriteLine("Encrypting the following bytes:");
        PrintByteArray(Plain);
        Console.WriteLine("With the following Key:");
        PrintByteArray(Key);
        Console.WriteLine("and IV:");
        PrintByteArray(IV);
 		Console.WriteLine("Expecting this ciphertext:");
		PrintByteArray(Cipher);        
        
        ICryptoTransform sse = aes.CreateEncryptor(Key, IV);
        MemoryStream 	ms = new MemoryStream();
        CryptoStream    cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
        cs.Write(Plain,0,Plain.Length);
        cs.FlushFinalBlock();
        CipherCalculated = ms.ToArray();
        cs.Close();

		Console.WriteLine("Computed this cyphertext:");
        PrintByteArray(CipherCalculated);
        

        if (!Compare(Cipher, CipherCalculated)) {
        	Console.WriteLine("ERROR: result is different from the expected");
        	return false;
        }
        
        Console.WriteLine("OK");
        return true;
    }
开发者ID:koson,项目名称:.NETMF_for_LPC17xx,代码行数:34,代码来源:AESKnownEnc2.cs

示例2: ForCodeCoverage

    public static bool ForCodeCoverage()
    {
        AesManaged aes = new AesManaged();
        using (ICryptoTransform cte = aes.CreateEncryptor(), ctd = aes.CreateDecryptor())
        {
            byte[] plain1 = new byte[64];
            for (int i = 0; i < plain1.Length; i++)
                plain1[i] = (byte)i;

            byte[] encr1 = cte.TransformFinalBlock(plain1, 0, plain1.Length);
            using(MemoryStream ms = new MemoryStream())
            using(CryptoStream cs = new CryptoStream(ms, ctd, CryptoStreamMode.Write))
            {
                cs.Write(encr1, 0, 16);
                cs.Write(encr1, 16, 16);
                cs.Write(encr1, 32, 16);
                cs.Write(encr1, 48, encr1.Length-48);
                cs.FlushFinalBlock();
                byte[] plain2 = ms.ToArray();

                if (!Compare(plain1, plain2))
                {
                    Console.WriteLine("CodeCoverage case failed");
                    return false;
                }
                return true;
            }
        }
    }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:29,代码来源:AESTrans2.cs

示例3: AES_Decrypt

    private static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
    {
        byte[] decryptedBytes = null;

        using (MemoryStream ms = new MemoryStream())
        {
            using (RijndaelManaged AES = new RijndaelManaged())
            {
                try
                {
                    AES.KeySize = AESKeySize_256;
                    AES.Key = passwordBytes;
                    AES.Mode = AESCipherMode_ECB;
                    AES.Padding = AESPadding_PKCS7;

                    using (CryptoStream cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                        cs.Close();
                    }
                    decryptedBytes = ms.ToArray();
                }
                catch (CryptographicException e)
                {
                    throw e;
                }
            }
        }

        return decryptedBytes;
    }
开发者ID:neiltuma,项目名称:steve_prj,代码行数:31,代码来源:testSSO.aspx.cs

示例4: Encrypt

    public static string Encrypt(string originalString, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            // 把字符串放到byte数组中
            byte[] inputByteArray = Encoding.Default.GetBytes(originalString);

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  //原文使用ASCIIEncoding.ASCII方法的

                                                            //GetBytes方法
            MemoryStream ms = new MemoryStream();         //使得输入密码必须输入英文文本
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();

            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            return ret.ToString();
    }
开发者ID:qq5013,项目名称:JXNG,代码行数:25,代码来源:CookiesHelper.cs

示例5: Decrypt

    public static string Decrypt(string originalString, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        byte[] inputByteArray = new byte[originalString.Length / 2];
        for (int x = 0; x < originalString.Length / 2; x++)
        {
            int i = (Convert.ToInt32(originalString.Substring(x * 2, 2), 16));
            inputByteArray[x] = (byte)i;
        }

        //建立加密对象的密钥和偏移量,此值重要,不能修改

        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);

        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();

        //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
        StringBuilder ret = new StringBuilder();

        return System.Text.Encoding.Default.GetString(ms.ToArray());
    }
开发者ID:qq5013,项目名称:JXNG,代码行数:27,代码来源:CookiesHelper.cs

示例6: Encrypt

 public static string Encrypt(string plainText, string passPhrase)
 {
     byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
     using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
     {
         byte[] keyBytes = password.GetBytes(keysize / 8);
         using (RijndaelManaged symmetricKey = new RijndaelManaged())
         {
             symmetricKey.Mode = CipherMode.CBC;
             using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
             {
                 using (MemoryStream memoryStream = new MemoryStream())
                 {
                     using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                     {
                         cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                         cryptoStream.FlushFinalBlock();
                         byte[] cipherTextBytes = memoryStream.ToArray();
                         return Convert.ToBase64String(cipherTextBytes);
                     }
                 }
             }
         }
     }
 }
开发者ID:jammerxd,项目名称:Twitch-Desktop-Manager,代码行数:25,代码来源:Encryption.cs

示例7: Decrypt

 public static string Decrypt(string stringToDecrypt)
 {
     string sEncryptionKey = "thuynnxkey";
     byte[] key = { };
     byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
     byte[] inputByteArray = new byte[stringToDecrypt.Length];
     try
     {
         key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
         DESCryptoServiceProvider des = new DESCryptoServiceProvider();
         inputByteArray = Convert.FromBase64String(stringToDecrypt);
         MemoryStream ms = new MemoryStream();
         CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
         cs.Write(inputByteArray, 0, inputByteArray.Length);
         cs.FlushFinalBlock();
         cs.Close();
         Encoding encoding = Encoding.UTF8;
         return encoding.GetString(ms.ToArray());
     }
     catch (System.Exception ex)
     {
         SiAuto.Main.LogString("Lỗi :", ex.ToString());
         return (string.Empty);
     }
 }
开发者ID:trantrung2608,项目名称:ilinkbay,代码行数:25,代码来源:Utils.cs

示例8: Decrypt

    public static string Decrypt(string inName, string password)
    {
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, keySalt);

        Rijndael alg = Rijndael.Create();
        alg.Key = pdb.GetBytes(32);
        alg.IV = pdb.GetBytes(16);

        using (var fin = new FileStream(inName, FileMode.Open, FileAccess.Read))
        using (var fout = new MemoryStream())
        using (var cs = new CryptoStream(fout, alg.CreateDecryptor(), CryptoStreamMode.Write))
        {
            byte[] buffer = new byte[4096];
            int bytesRead;

            do
            {
                bytesRead = fin.Read(buffer, 0, buffer.Length);
                cs.Write(buffer, 0, bytesRead);
            }
            while (bytesRead != 0);

            cs.FlushFinalBlock();
            return Encoding.ASCII.GetString(fout.ToArray());
        }
    }
开发者ID:Diullei,项目名称:Storm,代码行数:26,代码来源:host.cs

示例9: Encrypt

    public static string Encrypt(this string text, string lKey)
    {
        try
        {
            using (Aes aes = new AesManaged())
            {
                Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(Encoding.UTF8.GetString(IVa, 0, IVa.Length), Encoding.UTF8.GetBytes(lKey));
                aes.Key = deriveBytes.GetBytes(128 / 8);
                aes.IV = aes.Key;
                using (MemoryStream encryptionStream = new MemoryStream())
                {
                    using (CryptoStream encrypt = new CryptoStream(encryptionStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        byte[] cleanText = Encoding.UTF8.GetBytes(text);
                        encrypt.Write(cleanText, 0, cleanText.Length);
                        encrypt.FlushFinalBlock();
                    }

                    byte[] encryptedData = encryptionStream.ToArray();
                    string encryptedText = Convert.ToBase64String(encryptedData);


                    return encryptedText;
                }
            }
        }
        catch
        {
            return String.Empty;
        }
    }
开发者ID:mgerasika,项目名称:portmone-app,代码行数:31,代码来源:Crypto.cs

示例10: Encrypt

    //This method is to encrypt the password given by user.
    public static string Encrypt(string data, string password)
    {
        if (String.IsNullOrEmpty(data))
            throw new ArgumentException("No data given");
        if (String.IsNullOrEmpty(password))
            throw new ArgumentException("No password given");

        // setup the encryption algorithm
        Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, 8);
        TripleDES tdes = TripleDES.Create();
        //Rijndael aes = Rijndael.Create();

        tdes.IV = keyGenerator.GetBytes(tdes.BlockSize / 8);
        tdes.Key = keyGenerator.GetBytes(tdes.KeySize / 8);

        // encrypt the data
        byte[] rawData = Encoding.Unicode.GetBytes(data);
        using (MemoryStream memoryStream = new MemoryStream())
        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateEncryptor(), CryptoStreamMode.Write))
        {
            memoryStream.Write(keyGenerator.Salt, 0, keyGenerator.Salt.Length);
            cryptoStream.Write(rawData, 0, rawData.Length);
            cryptoStream.Close();

            byte[] encrypted = memoryStream.ToArray();
            return Convert.ToBase64String(encrypted);
        }
    }
开发者ID:progressiveinfotech,项目名称:PRO_FY13_40_Helpdesk-Support-and-Customization_TerexBest,代码行数:29,代码来源:Activate.aspx.cs

示例11: EncryptData

	private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
	 {
	     //Create the file streams to handle the input and output files.
	     FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
	     FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
	     fout.SetLength(0);

	     //Create variables to help with read and write.
	     byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
	     long rdlen = 0;              //This is the total number of bytes written.
	     long totlen = fin.Length;    //This is the total length of the input file.
	     int len;                     //This is the number of bytes to be written at a time.

	     SymmetricAlgorithm des = new DESCryptoServiceProvider();
         des.Padding = PaddingMode.PKCS7;
	     CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);

	     Console.WriteLine("Encrypting...");

	     //Read from the input file, then encrypt and write to the output file.
	     while(rdlen < totlen)
	     {
	         len = fin.Read(bin, 0, 100);
	         encStream.Write(bin, 0, len);
	         rdlen = rdlen + len;
             Console.WriteLine("{0} bytes processed", rdlen);
         }
	     encStream.Close();
    }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:29,代码来源:EncDecFileScn.cs

示例12: Decrypt

    public static string Decrypt(this string text, string lKey)
    {
        try
        {
            using (Aes aes = new AesManaged())
            {
                Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(Encoding.UTF8.GetString(IVa, 0, IVa.Length), Encoding.UTF8.GetBytes(lKey));
                aes.Key = deriveBytes.GetBytes(128 / 8);
                aes.IV = aes.Key;

                using (MemoryStream decryptionStream = new MemoryStream())
                {
                    using (CryptoStream decrypt = new CryptoStream(decryptionStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        byte[] encryptedData = Convert.FromBase64String(text);


                        decrypt.Write(encryptedData, 0, encryptedData.Length);
                        decrypt.Flush();
                    }

                    byte[] decryptedData = decryptionStream.ToArray();
                    string decryptedText = Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);


                    return decryptedText;
                }
            }
        }
        catch
        {
            return String.Empty;
        }
    }
开发者ID:mgerasika,项目名称:portmone-app,代码行数:34,代码来源:Crypto.cs

示例13: Encrypt

 /// <summary>
 /// Encrypt some text and return an encrypted byte array.
 /// </summary>
 /// <param name="TextValue">The Text to encrypt</param>
 /// <returns>byte[] of the encrypted value</returns>
 public byte[] Encrypt(string TextValue)
 {
     //Translates our text value into a byte array.
     Byte[] bytes = UTFEncoder.GetBytes(TextValue);
     //Used to stream the data in and out of the CryptoStream.
     MemoryStream memoryStream = new MemoryStream();
     /*
     * We will have to write the unencrypted bytes to the
     * stream, then read the encrypted result back from the
     * stream.
     */
     #region Write the decrypted value to the encryption stream
     CryptoStream cs = new CryptoStream(memoryStream,
     EncryptorTransform, CryptoStreamMode.Write);
     cs.Write(bytes, 0, bytes.Length);
     cs.FlushFinalBlock();
     #endregion
     #region Read encrypted value back out of the stream
     memoryStream.Position = 0;
     byte[] encrypted = new byte[memoryStream.Length];
     memoryStream.Read(encrypted, 0, encrypted.Length);
     #endregion
     //Clean up.
     cs.Close();
     memoryStream.Close();
     return encrypted;
 }
开发者ID:peelybird,项目名称:Beetree.AESCrypto,代码行数:32,代码来源:SimpleAES.cs

示例14: Decrypt

    //cmThe function used to decrypt the text
    private static string Decrypt(string strText, string SaltKey)
    {
        byte[] byKey = { };
        byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
        byte[] inputByteArray = new byte[strText.Length + 1];

        try
        {
            byKey = System.Text.Encoding.UTF8.GetBytes(SaltKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(strText);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;

            return encoding.GetString(ms.ToArray());

        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
开发者ID:Vinhbaba,项目名称:dskfeorfqlhvsea,代码行数:27,代码来源:Cipher.cs

示例15: Encrypt

    public static byte[] Encrypt(byte[] data, string password)
    {
        byte[] result = null;
            byte[] passwordBytes = Encoding.Default.GetBytes(password);

            using (MemoryStream ms = new MemoryStream())
            {
                using (RijndaelManaged AES = new RijndaelManaged())
                {
                    AES.KeySize = keySize;
                    AES.BlockSize = 128;
                    var key = new Rfc2898DeriveBytes(passwordBytes, salt, 1000);
                    AES.Key = key.GetBytes(AES.KeySize / 8);
                    AES.IV = key.GetBytes(AES.BlockSize / 8);
                    AES.Mode = CipherMode.CBC;
                    using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                        cs.Close();
                    }
                    result = ms.ToArray();
                }

            }
            return result;
    }
开发者ID:Syriamanal,项目名称:xprotect,代码行数:26,代码来源:Encryption.cs


注:本文中的CryptoStream.Write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。