Skip to content

Random IV in encrypt data example - #21043

Merged
IEvangelist merged 3 commits into
dotnet:masterfrom
duracellko:aes-encrypt
Nov 10, 2020
Merged

IEvangelist merged 3 commits into
dotnet:masterfrom
duracellko:aes-encrypt

Conversation

@duracellko

Copy link
Copy Markdown
Contributor

Already since .NET 2 and MSDN lot of examples about symmetric cryptography treat IV the same way as encryption key. Unfortunately this anti-pattern is quite often used in production code too. Main purpose of IV is to add some randomness to encrypted data. So that encrypting the same text gives different output each time. This way an attacker cannot tell if the encrypted message/password is the same or not.

Therefore I updated Encrypting data and Decrypting data using AES pages. There are following changes:

  1. The updated example stores IV together with encrypted data and it is not part of source code as secret.
  2. Decrypting example is organized the same way as encrypting example. In "snippets" folder.
  3. Streams are disposed by using statement and not Close method.

Open question: I used new using form in C# 8 without nested code block. Is this ok, or should the example be compatible with previous versions of C#?

@IEvangelist IEvangelist left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @duracellko

This looks good, thank you. We'll :shipit: when you make the suggested changes. Also, it is fine to use the C# 8 bits here - not a problem.

Comment thread docs/standard/security/decrypting-data.md Outdated
The following example illustrates how to create a new instance of the default implementation class for the <xref:System.Security.Cryptography.Aes> algorithm. The instance is used to perform decryption on a <xref:System.Security.Cryptography.CryptoStream> object. This example first creates a new instance of the **Aes** implementation class. Next it creates a **CryptoStream** object and initializes it to the value of a managed stream called `myStream`. Next, the **CreateDecryptor** method from the **Aes** class is passed the same key and IV that was used for encryption and is then passed to the **CryptoStream** constructor.
The following example illustrates how to create a new instance of the default implementation class for the <xref:System.Security.Cryptography.Aes> algorithm. The instance is used to perform decryption on a <xref:System.Security.Cryptography.CryptoStream> object. This example first creates a new instance of the **Aes** implementation class. Next it reads IV value from a managed stream called `myStream`. Next it creates a **CryptoStream** object and initializes it to the value of the managed stream called `myStream`. Next, the **CreateDecryptor** method from the **Aes** class is passed the read IV and the same key that was used for encryption and are then passed to the **CryptoStream** constructor.

```vb

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're adding VB and C# code, all code examples must have functional equivalents. For example, this VB should have a C# equivalent - otherwise you end up with a mix of the two languages and the language dropdown doesn't work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something? The C# code is just below the VB code. Or should C# code be always first?
I didn't change this part and in currently published version both languages work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern here is that when there is a snippet of VB, that needs to be immediately followed by C#. This is part of the language toggle on the platform, since this article has both VB and C#. Does that make sense?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this VB snippet is followed by C#.

Dim aes As Aes = Aes.Create()
Dim cryptStream As New CryptoStream(myStream, aes.CreateDecryptor(key, iv), CryptoStreamMode.Read)
Aes aes = Aes.Create();
CryptoStream cryptStream = new CryptoStream(myStream, aes.CreateDecryptor(key, iv), CryptoStreamMode.Read);

Unless we are talking about different snippet.

@IEvangelist IEvangelist self-assigned this Oct 12, 2020
Co-authored-by: David Pine <david.pine@microsoft.com>
@duracellko

Copy link
Copy Markdown
Contributor Author

I have a question. Would this repository join Hacktoberfest by adding tag? https://hacktoberfest.digitalocean.com/hacktoberfest-update

@IEvangelist

Copy link
Copy Markdown
Member

I have a question. Would this repository join Hacktoberfest by adding tag? https://hacktoberfest.digitalocean.com/hacktoberfest-update

I'm actually not sure, let's ask @BillWagner about that.

Comment thread docs/standard/security/decrypting-data.md Outdated
@IEvangelist IEvangelist reopened this Oct 15, 2020
@IEvangelist

Copy link
Copy Markdown
Member

Closed and reopened to trigger build validation... odd happens 😵

@IEvangelist IEvangelist reopened this Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants