Skip to content

Commit 35d719f

Browse files
committed
added emulator for missing Iso10126 padding in .net core
1 parent 1622ef0 commit 35d719f

File tree

6 files changed

+124
-3
lines changed

6 files changed

+124
-3
lines changed

Blazer.Net.Tests/Blazer.Net.Tests.xproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<RootNamespace>Blazer.Net.Tests</RootNamespace>
1212
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
1313
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14-
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
14+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
1515
</PropertyGroup>
1616

1717
<PropertyGroup>

Blazer.Net.Tests/EncryptionTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.IO;
3+
using System.Linq;
24
using System.Security.Cryptography;
35
using System.Text;
46

57
using Force.Blazer;
8+
using Force.Blazer.Encyption;
69

710
using NUnit.Framework;
811

@@ -112,5 +115,38 @@ public void Invalid_Archive_If_Invalid_EncyptFull_Password()
112115
var ex = Assert.Throws<InvalidOperationException>(() => IntegrityHelper.CheckCompressDecompress(data, blazerCompressionOptions, s => new BlazerOutputStream(s, new BlazerDecompressionOptions("1") { EncyptFull = true })));
113116
Assert.That(ex.Message, Is.EqualTo("This is not Blazer archive"));
114117
}
118+
119+
[Test]
120+
public void Different_Paddings_Should_Not_Cause_Errors()
121+
{
122+
var data = Enumerable.Range(0, 100).Select(x => (byte)x).ToArray();
123+
var pass = new Rfc2898DeriveBytes("test", 8, 4096);
124+
var key = pass.GetBytes(32);
125+
var aes = Aes.Create();
126+
aes.Key = key;
127+
aes.IV = new byte[16];
128+
aes.Mode = CipherMode.CBC;
129+
#if NETCORE
130+
aes.Padding = PaddingMode.PKCS7;
131+
#else
132+
aes.Padding = PaddingMode.ISO10126;
133+
#endif
134+
var memoryStream = new MemoryStream();
135+
var cs = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
136+
cs.Write(data, 0, data.Length);
137+
cs.Flush();
138+
cs.Close();
139+
140+
var aes2 = Aes.Create();
141+
aes2.Key = key;
142+
aes2.IV = new byte[16];
143+
aes2.Mode = CipherMode.CBC;
144+
aes2.Padding = PaddingMode.PKCS7;
145+
var cs2 = new CryptoStream(new MemoryStream(memoryStream.ToArray()), new Iso10126TransformEmulator(aes2.CreateDecryptor()), CryptoStreamMode.Read);
146+
var data2 = new byte[128];
147+
var len = cs2.Read(data2, 0, data2.Length);
148+
Assert.That(len, Is.EqualTo(data.Length));
149+
CollectionAssert.AreEqual(data, data2.Take(data.Length));
150+
}
115151
}
116152
}

Blazer.Net/Blazer.Net.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Compile Include="Algorithms\BufferInfo.cs" />
6666
<Compile Include="BlazerFlushMode.cs" />
6767
<Compile Include="BlazerPatternedHelper.cs" />
68+
<Compile Include="Encyption\ISO10126TransformEmulator.cs" />
6869
<Compile Include="Helpers\FileHeaderHelper.cs" />
6970
<Compile Include="Algorithms\StreamEncoderHigh.cs" />
7071
<Compile Include="Algorithms\Crc32C\Crc32C.cs" />

Blazer.Net/Blazer.Net.xproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<RootNamespace>Blazer.Net</RootNamespace>
1212
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
1313
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14-
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
14+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
1515
</PropertyGroup>
1616

1717
<PropertyGroup>

Blazer.Net/Encyption/DecryptHelper.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,15 @@ public static Stream ConvertStreamToDecyptionStream(Stream inner, string passwor
114114
// zero. it is ok - we use random password (due salt), so, anyway it will be different
115115
aes.IV = new byte[16];
116116
aes.Mode = CipherMode.CBC;
117+
var cryptoTransform = aes.CreateDecryptor();
118+
#if NETCORE
117119
aes.Padding = PaddingMode.PKCS7; // here we will use such padding
118-
return new CryptoStream(inner, aes.CreateDecryptor(), CryptoStreamMode.Read);
120+
cryptoTransform = new Iso10126TransformEmulator(cryptoTransform);
121+
#else
122+
aes.Padding = PaddingMode.ISO10126; // here we will use such padding
123+
#endif
124+
aes.Padding = PaddingMode.PKCS7; // here we will use such padding
125+
return new CryptoStream(inner, cryptoTransform, CryptoStreamMode.Read);
119126
}
120127
}
121128
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Security.Cryptography;
3+
4+
namespace Force.Blazer.Encyption
5+
{
6+
/// <summary>
7+
/// This class emulates ISO10126 AES transform for .NET Core
8+
/// </summary>
9+
public class Iso10126TransformEmulator : ICryptoTransform
10+
{
11+
private readonly ICryptoTransform _origTransofrm;
12+
13+
/// <summary>
14+
/// Constructor. Origignal transform should be aes with PKCS7 padding
15+
/// </summary>
16+
/// <param name="origTransofrm"></param>
17+
public Iso10126TransformEmulator(ICryptoTransform origTransofrm)
18+
{
19+
_origTransofrm = origTransofrm;
20+
}
21+
22+
/// <summary>
23+
/// Dispose current transform
24+
/// </summary>
25+
public void Dispose()
26+
{
27+
_origTransofrm.Dispose();
28+
}
29+
30+
int ICryptoTransform.TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
31+
{
32+
return _origTransofrm.TransformBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
33+
}
34+
35+
byte[] ICryptoTransform.TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
36+
{
37+
var outBuffer = new byte[_origTransofrm.OutputBlockSize + inputCount];
38+
var dummyBuffer = new byte[_origTransofrm.InputBlockSize + inputCount];
39+
Buffer.BlockCopy(inputBuffer, inputOffset, dummyBuffer, 0, inputCount);
40+
_origTransofrm.TransformBlock(dummyBuffer, 0, dummyBuffer.Length, outBuffer, 0);
41+
Array.Resize(ref outBuffer, outBuffer.Length - outBuffer[outBuffer.Length - 1]);
42+
return outBuffer;
43+
}
44+
45+
int ICryptoTransform.InputBlockSize
46+
{
47+
get
48+
{
49+
return _origTransofrm.InputBlockSize;
50+
}
51+
}
52+
53+
int ICryptoTransform.OutputBlockSize
54+
{
55+
get
56+
{
57+
return _origTransofrm.OutputBlockSize;
58+
}
59+
}
60+
61+
bool ICryptoTransform.CanTransformMultipleBlocks
62+
{
63+
get
64+
{
65+
return _origTransofrm.CanTransformMultipleBlocks;
66+
}
67+
}
68+
69+
bool ICryptoTransform.CanReuseTransform
70+
{
71+
get
72+
{
73+
return _origTransofrm.CanReuseTransform;
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)