Skip to content

Commit 8dd6f7c

Browse files
committed
Sample code for AES encryption from Microsoft website
1 parent caf21bb commit 8dd6f7c

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed

.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
*.swp
2+
*.*~
3+
project.lock.json
4+
.DS_Store
5+
*.pyc
6+
nupkg/
7+
8+
# Visual Studio Code
9+
#.vscode
10+
11+
# Rider
12+
.idea
13+
14+
# User-specific files
15+
*.suo
16+
*.user
17+
*.userosscache
18+
*.sln.docstates
19+
20+
# Build results
21+
[Dd]ebug/
22+
[Dd]ebugPublic/
23+
[Rr]elease/
24+
[Rr]eleases/
25+
x64/
26+
x86/
27+
build/
28+
bld/
29+
[Bb]in/
30+
[Oo]bj/
31+
[Oo]ut/
32+
msbuild.log
33+
msbuild.err
34+
msbuild.wrn
35+
36+
# Visual Studio 2015
37+
.vs/

.vscode/launch.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
// Use IntelliSense to find out which attributes exist for C# debugging
3+
// Use hover for the description of the existing attributes
4+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/padding-oracle-attack.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
17+
"console": "internalConsole",
18+
"stopAtEntry": false
19+
},
20+
{
21+
"name": ".NET Core Attach",
22+
"type": "coreclr",
23+
"request": "attach",
24+
"processId": "${command:pickProcess}"
25+
}
26+
]
27+
}

.vscode/tasks.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/padding-oracle-attack.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/padding-oracle-attack.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"${workspaceFolder}/padding-oracle-attack.csproj",
36+
"/property:GenerateFullPaths=true",
37+
"/consoleloggerparameters:NoSummary"
38+
],
39+
"problemMatcher": "$msCompile"
40+
}
41+
]
42+
}

Program.cs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System;
2+
using System.IO;
3+
using System.Security.Cryptography;
4+
5+
namespace Padding_Oracle_Attack
6+
{
7+
class PaddingOracleAttack
8+
{
9+
public static void Main()
10+
{
11+
string original = "Here is some data to encrypt!";
12+
13+
// Create a new instance of the Aes
14+
// class. This generates a new key and initialization
15+
// vector (IV).
16+
using (Aes myAes = Aes.Create())
17+
{
18+
19+
// Encrypt the string to an array of bytes.
20+
byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
21+
22+
// Decrypt the bytes to a string.
23+
string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
24+
25+
//Display the original data and the decrypted data.
26+
Console.WriteLine("Original: {0}", original);
27+
Console.WriteLine("Round Trip: {0}", roundtrip);
28+
}
29+
}
30+
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
31+
{
32+
// Check arguments.
33+
if (plainText == null || plainText.Length <= 0)
34+
throw new ArgumentNullException("plainText");
35+
if (Key == null || Key.Length <= 0)
36+
throw new ArgumentNullException("Key");
37+
if (IV == null || IV.Length <= 0)
38+
throw new ArgumentNullException("IV");
39+
byte[] encrypted;
40+
41+
// Create an Aes object
42+
// with the specified key and IV.
43+
using (Aes aesAlg = Aes.Create())
44+
{
45+
aesAlg.Key = Key;
46+
aesAlg.IV = IV;
47+
48+
// Create an encryptor to perform the stream transform.
49+
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
50+
51+
// Create the streams used for encryption.
52+
using (MemoryStream msEncrypt = new MemoryStream())
53+
{
54+
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
55+
{
56+
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
57+
{
58+
//Write all data to the stream.
59+
swEncrypt.Write(plainText);
60+
}
61+
encrypted = msEncrypt.ToArray();
62+
}
63+
}
64+
}
65+
66+
67+
// Return the encrypted bytes from the memory stream.
68+
return encrypted;
69+
70+
}
71+
72+
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
73+
{
74+
// Check arguments.
75+
if (cipherText == null || cipherText.Length <= 0)
76+
throw new ArgumentNullException("cipherText");
77+
if (Key == null || Key.Length <= 0)
78+
throw new ArgumentNullException("Key");
79+
if (IV == null || IV.Length <= 0)
80+
throw new ArgumentNullException("IV");
81+
82+
// Declare the string used to hold
83+
// the decrypted text.
84+
string plaintext = null;
85+
86+
// Create an Aes object
87+
// with the specified key and IV.
88+
using (Aes aesAlg = Aes.Create())
89+
{
90+
aesAlg.Key = Key;
91+
aesAlg.IV = IV;
92+
93+
// Create a decryptor to perform the stream transform.
94+
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
95+
96+
// Create the streams used for decryption.
97+
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
98+
{
99+
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
100+
{
101+
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
102+
{
103+
104+
// Read the decrypted bytes from the decrypting stream
105+
// and place them in a string.
106+
plaintext = srDecrypt.ReadToEnd();
107+
}
108+
}
109+
}
110+
111+
}
112+
113+
return plaintext;
114+
}
115+
}
116+
}

padding-oracle-attack.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)