Skip to content

Commit 54c32cb

Browse files
authored
Merge pull request #7 from mehyaa/codex/find-and-fix-a-bug-in-codebase
Fix AES decryption
2 parents 836edaf + 9aba7a1 commit 54c32cb

2 files changed

Lines changed: 10 additions & 65 deletions

File tree

src/Convey.Security/src/Convey.Security/IEncryptor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ namespace Convey.Security;
33
// AES-256
44
public interface IEncryptor
55
{
6-
string Encrypt(string data, string key);
7-
string Decrypt(string data, string key);
86
byte[] Encrypt(byte[] data, byte[] iv, byte[] key);
97
byte[] Decrypt(byte[] data, byte[] iv, byte[] key);
108
}

src/Convey.Security/src/Convey.Security/Internals/Encryptor.cs

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,24 @@
11
using System;
22
using System.IO;
3-
using System.Linq;
43
using System.Security.Cryptography;
5-
using System.Text;
64

75
namespace Convey.Security.Internals;
86

97
internal sealed class Encryptor : IEncryptor
108
{
11-
public string Encrypt(string data, string key)
12-
{
13-
if (string.IsNullOrWhiteSpace(data))
14-
{
15-
throw new ArgumentException("Data to be encrypted cannot be empty.", nameof(data));
16-
}
17-
18-
if (string.IsNullOrWhiteSpace(key))
19-
{
20-
throw new ArgumentException("Encryption key cannot be empty.", nameof(key));
21-
}
22-
23-
using var aes = Aes.Create();
24-
aes.Key = Encoding.UTF8.GetBytes(key);
25-
var iv = Convert.ToBase64String(aes.IV);
26-
var transform = aes.CreateEncryptor(aes.Key, aes.IV);
27-
using var memoryStream = new MemoryStream();
28-
using var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
29-
using (var streamWriter = new StreamWriter(cryptoStream))
30-
{
31-
streamWriter.Write(data);
32-
}
33-
34-
return iv + Convert.ToBase64String(memoryStream.ToArray());
35-
}
36-
37-
public string Decrypt(string data, string key)
38-
{
39-
if (string.IsNullOrWhiteSpace(data))
40-
{
41-
throw new ArgumentException("Data to be decrypted cannot be empty.", nameof(data));
42-
}
43-
44-
if (string.IsNullOrWhiteSpace(key))
45-
{
46-
throw new ArgumentException("Encryption key cannot be empty.", nameof(key));
47-
}
48-
49-
using var aes = Aes.Create();
50-
aes.Key = Encoding.UTF8.GetBytes(key);
51-
aes.IV = Convert.FromBase64String(data.Substring(0, 24));
52-
var transform = aes.CreateDecryptor(aes.Key, aes.IV);
53-
using var memoryStream = new MemoryStream(Convert.FromBase64String(data.Substring(24)));
54-
using var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read);
55-
using var streamReader = new StreamReader(cryptoStream);
56-
57-
return streamReader.ReadToEnd();
58-
}
59-
609
public byte[] Encrypt(byte[] data, byte[] iv, byte[] key)
6110
{
62-
if (data is null || !data.Any())
11+
if (data is null || data.Length == 0)
6312
{
6413
throw new ArgumentException("Data to be encrypted cannot be empty.", nameof(data));
6514
}
6615

67-
if (iv is null || !iv.Any())
16+
if (iv is null || iv.Length == 0)
6817
{
6918
throw new ArgumentException("Initialization vector cannot be empty.", nameof(iv));
7019
}
7120

72-
if (key is null || !key.Any())
21+
if (key is null || key.Length == 0)
7322
{
7423
throw new ArgumentException("Encryption key cannot be empty.", nameof(key));
7524
}
@@ -80,27 +29,25 @@ public byte[] Encrypt(byte[] data, byte[] iv, byte[] key)
8029
var transform = aes.CreateEncryptor(aes.Key, aes.IV);
8130
using var memoryStream = new MemoryStream();
8231
using var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
83-
using (var streamWriter = new StreamWriter(cryptoStream))
84-
{
85-
streamWriter.Write(data);
86-
}
32+
cryptoStream.Write(data, 0, data.Length);
33+
cryptoStream.FlushFinalBlock();
8734

8835
return memoryStream.ToArray();
8936
}
9037

9138
public byte[] Decrypt(byte[] data, byte[] iv, byte[] key)
9239
{
93-
if (data is null || !data.Any())
40+
if (data is null || data.Length == 0)
9441
{
9542
throw new ArgumentException("Data to be decrypted cannot be empty.", nameof(data));
9643
}
9744

98-
if (iv is null || !iv.Any())
45+
if (iv is null || iv.Length == 0)
9946
{
10047
throw new ArgumentException("Initialization vector cannot be empty.", nameof(iv));
10148
}
10249

103-
if (key is null || !key.Any())
50+
if (key is null || key.Length == 0)
10451
{
10552
throw new ArgumentException("Encryption key cannot be empty.", nameof(key));
10653
}
@@ -109,8 +56,8 @@ public byte[] Decrypt(byte[] data, byte[] iv, byte[] key)
10956
aes.Key = key;
11057
aes.IV = iv;
11158
var transform = aes.CreateDecryptor(aes.Key, aes.IV);
112-
using var memoryStream = new MemoryStream(data);
113-
using var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read);
59+
using var memoryStream = new MemoryStream();
60+
using var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
11461
cryptoStream.Write(data, 0, data.Length);
11562
cryptoStream.FlushFinalBlock();
11663

0 commit comments

Comments
 (0)