First .NET Medium ICryptoTransform Is one-way , That is, data can only be transformed from one state to another , On the contrary, it is not possible . Of course, manual operation ICryptoTransform It's more complicated , adopt CryptoStream It's easier to operate a ICryptoTransform. because ICryptoTransform Is one-way , So the two processes of encryption and decryption need two ICryptoTransform, This one goes through SymmetricAlgorithm Class CreateEncryptor and CreateDecryptor Method to create . While using CryptoStream fuck Make these two ICryptoTransform when , Often encryption is just using CryptoStreamMode.Write, Decryption uses Read, This is also reasonable in name Rational , But in fact CryptoStream The mode of operation is not limited to this .

About ICryptoTransform and CryptoStream Relationship model of , You can see this picture :

In fact, the so-called Read and Write Just put ICryptoTransform The two data states of the operation change places , The final operation is still to ICryptoTransform The source data is converted to the target data ( A one-way ), It's just that different modes operate differently .

For example, let's use CryptoStreamMode.Write To encrypt :

//+ using System.IO;
//+ using System.Security.Cryptography;
staticvoid Main()
using (var aes =Aes.Create())
var data =Encoding.Unicode.GetBytes("Mgen!");
Console.WriteLine(BitConverter.ToString(Encrypt_Write(aes.CreateEncryptor(), data).ToArray()));
} staticMemoryStream Encrypt_Write(ICryptoTransform ict, byte[] data)
using (var ms =newMemoryStream())
using (var cstream =newCryptoStream(ms, ict, CryptoStreamMode.Write))
cstream.Write(data, , data.Length);
return ms;

use CryptoStreamMode.Read Encryption can also be done :

//+ using System.IO;
//+ using System.Security.Cryptography;
staticvoid Main()
using (var aes =Aes.Create())
var data =Encoding.Unicode.GetBytes("Mgen!");
Console.WriteLine(BitConverter.ToString(Encrypt_Read(aes.CreateEncryptor(), data).ToArray()));
} staticMemoryStream Encrypt_Read(ICryptoTransform ict, byte[] data)
using (var ms =newMemoryStream(data))
using (var cstream =newCryptoStream(ms, ict, CryptoStreamMode.Read))
using (var destMs =newMemoryStream())
byte[] buffer =newbyte[];
int readLen; while ((readLen = cstream.Read(buffer, , )) >)
destMs.Write(buffer, , readLen);
return destMs;

Both will successfully encrypt the data , And enter the :


It's just about encryption , Decryption is exactly the same ,CryptoStreamMode.Write or Read Can be decrypted !

from :

.NET(C#): Flexible use of CryptoStream, Encryption doesn't have to be CryptoStreamMode.Write More articles about

  1. ASP.NET MVC5+EF6+EasyUI Background management system (62)-EF Link string encryption

    Series catalog Preface : This section provides a simple function , This function seems simple , After looking for it, I didn't find it EF Encrypted help document for linked database strings , I can only write by myself , This is also more in line with their own encryption requirements Sometimes we publish programs to avoid out of program SQL Link string ...

  2. 【 turn 】 Encryption and decryption algorithm sha1、md5、des、aes Detailed explanation of source code

    Original address : Catalog : 1..Net(C#) Under the platform Des Encrypt and decrypt the source code 2..Net(C#) Under the platform Aes Encrypt and decrypt the source code 3..N ...

  3. .Net(c#) Encryption and decryption Aes and Des

    .Net(c#) Encryption and decryption tool class : /// <summary> /// .Net Encryption and decryption help class /// </summary> public class NetCryptoHelp ...

  4. C# Commonly used encryption class help

    public static class EncryptUtil { #region MD5 encryption /// <summary> /// MD5 encryption /// </summary> p ...

  5. c# DES Encryption and decryption

    class DESHelper { string _iv = "9AUP"; string _key = "9d"; /// <summary> / ...

  6. DES,AeS encryption ,MD5,SHA encryption

    1.DES There are 4 Two parameters are involved in the operation : Plaintext . Ciphertext . secret key . vector . Among them 4 The relationship between the two can be understood as : Ciphertext = Plaintext + secret key + vector : Plaintext = Ciphertext - secret key - vector : Why the vector parameter ? Because if there's an article , There are a few words that repeat , ...

  7. Java and .NET Use DES The difference between symmetric encryption

    Java and .NET There are encapsulation in the system class library of DES The implementation of symmetric encryption , But the exposed interfaces are different , Sometimes it's hard to solve the problem , such as Java The encrypted result is .NET It can't be decrypted in the program , Due to the recent cross project Ja ...

  8. .Net Encryption and decryption in

    Back to the blog list turn  .Net Encryption and decryption in Li Chaoqiang Release time : 2015/11/23 12:55 read : 33 Collection : 3 give the thumbs-up : 0 Comment on : 0 In some important application scenarios , Transmitting data over the network requires encryption ...

  9. .net AES Encryption and decryption

    using System;      using System.Collections.Generic;      using System.Text;      using System.Secur ...

Random recommendation

  1. Windows Several practical commands for intranet penetration and power promotion

    1. Get operating system information View all environment variables :set Identify the system architecture :echo %PROCESSOR_ARCHITECTURE%  AMD64 Identify the operating system name and version :systeminfo View a specific user ...

  2. soft / Hard link instructions :ln

    grammar : ln  [ Options ]  The original document   Target file Options : -s Create a soft connection ( When creating a soft link , If the folder is inconsistent , The original file should use the absolute path ) Hard link features : 1. Have the same i Nodes and storage block block , It can be seen as the same ...

  3. MySQL Advanced query for

    Advanced query 1. Link query ( Extensions to columns ) The first form select * from Info,Nation # It forms a Cartesian product select * from Info,Nation where Info.Nati ...

  4. C# Capture the corresponding folder path through the file path

      try { OpenFileDialog openFileDialog = new OpenFileDialog(); string str = comboBox_hexFilePath.Text ...

  5. mvc Beginners controller Parameter transfer feeling

    Pass parameters from the view to controller There are many ways Method 1 ( recommend ): route config.Routes.MapHttpRoute( name: "DefaultApi", rout ...

  6. How to install and configure tomcat 8

    Link address : Apache tomcat It's the most popular java Website development services ...

  7. javascript Summary of execution sequence

    As web Developer , Be sure to be right js Execution order of , I have a certain understanding of the analytic principle , Otherwise, I can't control this small and easy-to-use language javascript It is a language to realize the dynamic effect of web page , It is also mainly responsible for the interaction with the server , He abandoned things like java The bundle of classes in ...

  8. django -- Recommendation algorithm

    Item based recommendation algorithm of collaborative filtering algorithm At present, personalized recommendation algorithms are mainly divided into three categories :1. Recommendation based on collaborative filtering :2. Recommendation and... Based on content filtering 3. Social recommendation . This paper mainly discusses recommendation based on collaborative filtering , The algorithm can also be divided into two categories : 1 ...

  9. django Foundation two

    One . What is architecture ? frame , namely framework, In particular, it refers to a constrained supporting structure designed to solve an open problem , Using frameworks can help you develop specific systems quickly , In short , It's when you perform on a stage that someone else has built . For all We ...

  10. SpringBoot Advanced

    One . Form validation Two .AOP Processing requests AOP It's a programming paradigm . It has nothing to do with language , It's a kind of programming idea . Process oriented to object oriented . Look at the world from another Angle , Change your posture to deal with the problem . 2.1AOP example -http request MAVEN Add dependency :o ...