Skip to content

Commit a226583

Browse files
committed
Fix wrong stream R/W reposition.
1 parent 27060da commit a226583

2 files changed

Lines changed: 60 additions & 11 deletions

File tree

CryptoMemoryStreamTests/IO/CryptoMemoryStreamTests.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using CryptoMemoryStream.IO;
32
using System;
43
using System.Collections.Generic;
54
using System.Linq;
65
using System.Text;
76
using System.Threading.Tasks;
7+
using System.Security.Cryptography;
88

9-
namespace CryptoMemoryStream.IO.Tests
9+
using CryptoStream;
10+
11+
namespace CryptoStream.IO.Tests
1012
{
1113
[TestClass()]
1214
public class CryptoMemoryStreamTests
@@ -19,7 +21,7 @@ public void WriteTest()
1921
{
2022
CryptoMemoryStream memoryStream = new CryptoMemoryStream(1024, plainKey, plainKey);
2123

22-
memoryStream.Write(data, 0, data.Length);
24+
memoryStream.Encrypt(data, 0, data.Length);
2325
}
2426

2527
[TestMethod()]
@@ -28,7 +30,7 @@ public void ReadTest()
2830
CryptoMemoryStream memoryStream = new CryptoMemoryStream(1024, plainKey, plainKey);
2931
byte[] buffer = new byte[16];
3032

31-
memoryStream.Write(data, 0, data.Length);
33+
memoryStream.Encrypt(data, 0, data.Length);
3234
Console.WriteLine(memoryStream.Read(buffer, 0, buffer.Length));
3335
Console.WriteLine(string.Join(" ", buffer));
3436
}
@@ -41,10 +43,27 @@ public void DuplexRWTest()
4143

4244
for (int i = 0; i < 2; i++)
4345
{
44-
memoryStream.Write(data, 0, data.Length);
46+
memoryStream.Encrypt(data, 0, data.Length);
4547
Console.WriteLine(memoryStream.Read(buffer, 0, buffer.Length));
4648
Console.WriteLine(string.Join(" ", buffer));
4749
}
4850
}
51+
52+
[TestMethod()]
53+
public void AESIVSizeTest()
54+
{
55+
AesCryptoServiceProvider aesManaged = new AesCryptoServiceProvider
56+
{
57+
Padding = PaddingMode.None,
58+
Mode = CipherMode.ECB
59+
};
60+
61+
aesManaged.KeySize = 256;
62+
aesManaged.GenerateIV();
63+
aesManaged.GenerateKey();
64+
65+
Console.WriteLine("IV Length : " + aesManaged.IV.Length);
66+
Console.WriteLine("Key Length : " + aesManaged.Key.Length);
67+
}
4968
}
5069
}

CryptoStream/CryptoMemoryStream.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ namespace CryptoStream
1414
/// </summary>
1515
public class CryptoMemoryStream : MemoryStream
1616
{
17+
/// <summary>
18+
/// 내부 암호화 알고리즘에 사용되는 초기화 벡터(Initialization vector) 의 크기를 반환합니다.
19+
/// </summary>
20+
public int IVSize => aesManaged.BlockSize / 8;
21+
1722
private ICryptoTransform cryptor;
1823

1924
private readonly AesCryptoServiceProvider aesManaged = new AesCryptoServiceProvider
@@ -81,10 +86,19 @@ private void InitializeCryptor(byte[] key, byte[] initializeVector)
8186
/// <param name="count">쓸 최대 바이트 수입니다.</param>
8287
public override void Write(byte[] buffer, int offset, int count)
8388
{
84-
byte[] encryptBytes = (byte[])buffer.Clone();
89+
base.Write(buffer, offset, count);
90+
}
8591

86-
CTRCryptor(encryptBytes, count);
87-
base.Write(encryptBytes, offset, count);
92+
/// <summary>
93+
/// 현재 스트림에서 바이트 블록을 읽어서 버퍼에 쓰고 읽어들인 부분을 0으로 채웁니다.
94+
/// </summary>
95+
/// <param name="buffer">이 메서드는 지정된 바이트 배열의 값이 offset과 (offset + count - 1) 사이에서 현재 원본으로부터 읽어온 바이트로 교체된 상태로 반환됩니다.</param>
96+
/// <param name="offset">현재 스트림으로 바이트를 복사하기 시작할 buffer의 바이트 오프셋(0부터 시작)입니다.</param>
97+
/// <param name="count">쓸 최대 바이트 수입니다.</param>
98+
/// <returns>버퍼로 쓴 총 바이트 수입니다. 해당 바이트 수를 현재 사용할 수 없는 경우 이 수는 요청된 바이트 수보다 작을 수 있으며 바이트를 읽기 전에 스트림의 끝에 도달한 경우에는 0이 될 수도 있습니다.</returns>
99+
public override int Read(byte[] buffer, int offset, int count)
100+
{
101+
return Read(buffer, offset, count, true);
88102
}
89103

90104
/// <summary>
@@ -93,13 +107,26 @@ public override void Write(byte[] buffer, int offset, int count)
93107
/// <param name="buffer">이 메서드는 지정된 바이트 배열의 값이 offset과 (offset + count - 1) 사이에서 현재 원본으로부터 읽어온 바이트로 교체된 상태로 반환됩니다.</param>
94108
/// <param name="offset">현재 스트림으로 바이트를 복사하기 시작할 buffer의 바이트 오프셋(0부터 시작)입니다.</param>
95109
/// <param name="count">쓸 최대 바이트 수입니다.</param>
110+
/// <param name="erase">읽어들인 스트림 위치의 데이터를 0으로 채웁니다.</param>
96111
/// <returns>버퍼로 쓴 총 바이트 수입니다. 해당 바이트 수를 현재 사용할 수 없는 경우 이 수는 요청된 바이트 수보다 작을 수 있으며 바이트를 읽기 전에 스트림의 끝에 도달한 경우에는 0이 될 수도 있습니다.</returns>
97-
public override int Read(byte[] buffer, int offset, int count)
112+
public int Read(byte[] buffer, int offset, int count, bool erase)
98113
{
99-
Position = Math.Max(Position - count, 0);
114+
int streamPosition = (int)Math.Max(Position - count, 0);
115+
116+
Position = streamPosition;
100117
int readedSize = base.Read(buffer, offset, count);
118+
119+
Position = streamPosition;
101120
SetLength(Position);
102121

122+
if (erase)
123+
{
124+
for (int i = 0; i < readedSize; i++)
125+
WriteByte(0);
126+
}
127+
128+
Position = streamPosition;
129+
SetLength(Position);
103130
return readedSize;
104131
}
105132

@@ -111,7 +138,10 @@ public override int Read(byte[] buffer, int offset, int count)
111138
/// <param name="count">쓸 최대 바이트 수입니다.</param>
112139
public void Encrypt(byte[] buffer, int offset, int count)
113140
{
114-
base.Write(buffer, offset, count);
141+
byte[] encryptBytes = (byte[])buffer.Clone();
142+
143+
CTRCryptor(encryptBytes, count);
144+
base.Write(encryptBytes, offset, count);
115145
}
116146

117147
/// <summary>

0 commit comments

Comments
 (0)