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

Zip file being accessed by multiple user - Mem out of exception issue
Why I can't use FileStream in windows store app [duplicate]
Save file to server viaWCF
T4 runtime template path specification
JSON.NET CustomCreationConverter with nested objects
Can a C# Interface require or dictate a specific class?
MySQL Query Doesn't Return Value, But Exists In DB
Comparing user input username/password to mysql database username/password not working
MvxException: Failed to resolve type…IRepository
Stop geolocation service windows phone 8
Multidimensional array init without pre-existing values?
StreamReader OutOfMemoryException [closed]
Dynamically Create Draggable UserControl in Win8 Metro App
ASP.NET MVC 4 form post variables are null in receiving controller
Read Table outputed by SQL Server Stored Proc during each iteration of While loop
WPF verify call not made from GUI thread

Categories

HOME
url
openacc
file-upload
c++builder
skypedeveloper
hex
yuv
foreach
install
browserstack
sitemap
aws-sdk-cpp
ldap-query
point-of-sale
tracking
owncloud
taxonomy
amazon-data-pipeline
simd
web-parts
powerpivot
loader
jq
handle
gsuite
rollupjs
workflow-foundation
marionette
angular-schema-form
matlab-compiler
cocoa-scripting
ragel
form-fields
mapquest
pygraphviz
motion
jquery-inputmask
redbean
directadmin
flume-ng
apiman
f#-fake
cakephp-3.2
android-doze-and-standby
nao-robot
history
dcom
openidm
openpgp.js
nsalert
chain
node.js-client
tsc
firebase-job-dispatcher
distcc
papyrus
tinybox2
recurring
gdi
3d-rendering
archiva
innertext
ideavim
gitk
persian
page-flipping
eggplant
pdcurses
grails-3.0.9
esb-toolkit-2.1
copy-on-write
veeam
accpac
perfect-scrollbar
optimistic-locking
random-access
fxmlloader
os.walk
xpand
banana-pi
traceur
ekevent
grunt-contrib-imagemin
objectdatasource
nokogiri
wpd
leap-year
phone-state-listener
django-tinymce
big-endian
doxia
php-gettext
optimistic-concurrency
code-organization
log-shipping
peoplepicker
functional-specifications
excellibrary
xcdatamodel
google-local-search
jquery-attributes
authkit
j++

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