c#


CryptoStream: Why CryptoStreamMode.Write to encrypt and CryptoStreamMode.Read to decrypt?


Let e = 'password' and I am transforming it to 'as9kio0736' in a CryptoStream.
Let d = 'as9kio0736' and I am transforming it to 'password in a CryptoStream.
When I am transforming d back to 'password' why is it not considered writing in a CryptoStream?
using (MemoryStream msEncrypt = new MemoryStream()) {
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
swEncrypt.Write(plainText);
}
}
}
using (MemoryStream msDecrypt = new MemoryStream(cipherText)) {
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
using (StreamReader srDecrypt = new StreamReader(csDecrypt)) {
plaintext = srDecrypt.ReadToEnd();
}
}
}
You can use CryptoStream in either direction for either operation; it's just where your data is and what you want to do with it.
If the data to process is already in a Stream (and you're okay with the stream getting drained and disposed), use CryptoStream in read mode and read the data out (including by using cryptoStream.CopyTo(someOtherStream)). If the data is in a byte[] and you want to write it to a Stream, use CryptoStream.Write.
In the .NET Core tests you can find examples both ways.
Using Read for both encrypt and decrypt: https://github.com/dotnet/corefx/blob/61fb32299a276f1aa4103c85dcec215dfddc252d/src/Common/tests/System/Security/Cryptography/AlgorithmImplementations/AES/AesCipherTests.cs#L466-L480
Using Write for both encrypt and decrypt: https://github.com/dotnet/corefx/blob/61fb32299a276f1aa4103c85dcec215dfddc252d/src/Common/tests/System/Security/Cryptography/AlgorithmImplementations/AES/AesCipherTests.cs#L609-L631
Heck, those are even in the same files. It's all just a matter of preference.

Related Links

Installing json.net via NU get error 'Newtonsoft.Json' already has a dependency defined for Microsoft C#
Only insert data into a database if it doesn't exist already
Entity Framework not Finding Table
Windows Service Communicate WIth UI
How can I prevent screen recording using C#?
Angular $http.post() to C# Web API Controller
Return parameter in controller instead a View
How can I pause the audio source and animation at the same time in unity 3d
When should I use ServiceFabricIntegrationOptions.UseUniqueServiceUrl
Regex match and replace operators in math operation
ASP.NET login web application on intranet against Active Directory Domain
Why is the hub context connection ID inaccessible at runtime in my VERY simple method?
How to prevent the CSharpCodeProvider to output access modifiers
How can I get the value of a radio button when the page is posted with mvc
Console two line output
ThreadAbortException with RazorEngine

Categories

HOME
joomla
logging
stanford-nlp
system-calls
gsm
wifi
windows-server-2008
highmaps
nltk
coordinates
esxi
ebay
gnuradio
wavefront
apache-httpclient-4.x
elastalert
distinct
adroitlogic
hdf5
tmux
logstash-configuration
sha1
multibinding
owncloud
internet-explorer-7
ofbiz
gpo
minimagick
aws-iot
windows-mobile
typeorm
custom-fields
dynamics-ax-2012-r3
federated
opencsv
ibm-bpm
visual-studio-debugging
postgresql-9.6
apache-falcon
powerbi-embedded
tunnel
kdevelop
instances
linkurious
keychain
mixed-models
mtm
pexpect
codepen
nao-robot
ionic2-select
flask-socketio
wink
webmatrix
programming-pearls
liferay-ide
blackberry
stripes
optional-parameters
access-violation
vga
jde
boost-propertytree
pebble-js
markerspiderfier
yfiles
redquerybuilder
transport
persian
musicbrainz
jsoncpp
opennebula
accpac
backtrace
cordova-3
xcode5
growl
xmlserializer
redmine-plugins
behance-api
twitter-finagle
smartxls
satisfiability
iwork
lastinsertid
scidb
mandelbrot
exi
gwt2
msbuild-task
mmo
cron4j
hypertable
xcode4.6.3
bash4
idtabs
automapper-2
virtual-printer
sessiontracking
functional-specifications
moss2007-security
hs-err
llblgen
nstoolbar
numerical-computing
carbon-emacs
atmega16
office-2003

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App