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

Winforms, Scintilla - Saving shortcut inserts 's' letter to editor
Conversion Error
Why Installshield LE Visual Studio 2010 Primary Output puts generated files on desktop
Combine strings and separate them by another string at specific locations
Automapper not ignoring nested property
consuming json webservice in windows phone 7
Add local group to CN=Readers,CN=Roles in AD LDS partition in C#
proper way of sending values between classes
Importing C# dll to C++ managed code (.NET)
using data table sAjaxSource with parameter
Authenticating with Exchange Web Services from server after setting up impersonation context
Need to redirect to another page in asp.net MVC3
My UserControl Crashes Visual Studio Whenever I Add It To A Form
How can I get the correct enum Value from an int c#
MVC - Adding . at end of string in resource file makes it invisible
How to use wkHtmltoPdf to generate PDf files from statcic html files in C#

Categories

HOME
azure-active-directory
network-programming
display
popover
spss
opc-ua
overrides
sdk
css-selectors
swap
autocad
sitemap
ejb-3.0
runtime
solidity
chef-recipe
opendaylight
openstack-horizon
dompdf
amazon-data-pipeline
versioning
google-calendar-api3
views
passbook
udeploy
serenity-js
combinations
gnu-screen
pushbullet
inline-assembly
handle
bootstrap-accordion
datastax-enterprise
continuous-fourier
ballerinalang
marionette
csproj
vtd-xml
arp
intermec
akamai
android-scrollview
cache-control
border-layout
campaign-monitor
yelp
queryover
libconfig
julia-jump
fisheye
redbean
compiler-warnings
contenteditable
pexpect
cakephp-3.2
inner-join
android-doze-and-standby
spring-data-commons
enet
multi-targeting
totalview
printer-control-language
dynatable
pyautogui
consolidation
git-push
normalisation
cherry-pick
trendline
exchange-server-2007
vanilla-forums
syncano
autoscaling
pagecontrol
mri
backtrace
indices
novell
api-eveonline
windows-search
visual-c#-express-2010
step
xpand
cs193p
ant-contrib
webshim
opencobol
xps
svg-android
ardor3d
randomaccessfile
mencoder
listpicker
pudb
gpl
junit-rule
mail-gem
aspmenu-control
bindinglist
peoplepicker
jqtransform
xhtmlrenderer
n2
lwp
onunload
p4.net
konsole
manuals

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