Skip to content

Commit b2d2414

Browse files
authored
Merge pull request #11 from selfmadecode/dev
merge Dev into master
2 parents 84d8cac + a6a8cc9 commit b2d2414

2 files changed

Lines changed: 56 additions & 22 deletions

File tree

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44

55
namespace SafeCrypt
66
{
7-
public class Encrypt : BaseAesEncryption
7+
public class AesEncryption : BaseAesEncryption
88
{
99
public byte[] AesEncrypt(byte[] data, byte[] secretKey, byte[] iv)
1010
=> EncryptAES(data, secretKey, iv);
1111

12+
public byte[] AesDecrypt(byte[] data, byte[] secretKey, byte[] iv)
13+
=> DecryptAES(data, secretKey, iv);
14+
1215
public byte[] AesEncrypt(string data, string secretKey, string iv)
1316
{
1417
NullChecks(data, secretKey, iv);
18+
var convertedKeys = ConvertKeysToBytes(secretKey, iv);
1519

16-
var aesKey = Encoding.UTF8.GetBytes(secretKey);
17-
var aesIv = Encoding.UTF8.GetBytes(iv);
18-
var aesData = data.HexadecimalStringToByteArray();
20+
var aesKey = convertedKeys.Item1;
21+
var aesIv = convertedKeys.Item2;
1922

23+
var aesData = data.HexadecimalStringToByteArray();
2024
return EncryptAES(aesData, aesKey, aesIv);
2125
}
2226

23-
public AesData AesEncrypt(string data, string secretKey)
27+
public AesEncryptionData AesEncrypt(string data, string secretKey)
2428
{
2529
NullChecks(data, secretKey);
2630

@@ -30,7 +34,7 @@ public AesData AesEncrypt(string data, string secretKey)
3034

3135
var response = EncryptAES(aesData, aesKey, aesIv);
3236

33-
var responseData = new AesData
37+
var responseData = new AesEncryptionData
3438
{
3539
Data = response,
3640
Iv = aesIv
@@ -60,34 +64,42 @@ public string AesEncryptByteToString(byte[] data, byte[] secretKey, byte[] iv)
6064
return cipherText.BytesToString();
6165
}
6266

67+
// needs methods that would accept aes mode
68+
//public byte[] AesEncrypt(byte[] data, byte[] key, byte[] iv, ReturnType returnType)
69+
//{
70+
71+
//}
72+
6373
private void NullChecks(string data, string secretKey, string iv)
6474
{
6575
if (data == null || data.Length <= 0)
66-
throw new ArgumentNullException("plainText");
76+
throw new ArgumentNullException(nameof(data));
6777

6878
if (secretKey == null || secretKey.Length <= 0)
69-
throw new ArgumentNullException("Key");
79+
throw new ArgumentNullException(nameof(secretKey));
7080

7181
if (iv == null || iv.Length <= 0)
72-
throw new ArgumentNullException("IV");
82+
throw new ArgumentNullException(nameof(iv));
7383
}
7484

85+
private (byte[], byte[]) ConvertKeysToBytes(string secretKey, string ivKey)
86+
{
87+
var secret = Encoding.UTF8.GetBytes(secretKey);
88+
var iv = Encoding.UTF8.GetBytes(ivKey);
89+
90+
return (secret, iv);
91+
}
7592
private void NullChecks(string data, string secretKey)
7693
{
7794
if (data == null || data.Length <= 0)
78-
throw new ArgumentNullException("plainText");
95+
throw new ArgumentNullException(nameof(data));
7996

8097
if (secretKey == null || secretKey.Length <= 0)
81-
throw new ArgumentNullException("Key");
82-
}
83-
84-
//public byte[] AesEncrypt(byte[] data, byte[] key, byte[] iv, ReturnType returnType)
85-
//{
86-
87-
//}
98+
throw new ArgumentNullException(nameof(secretKey));
99+
}
88100
}
89101

90-
public class AesData
102+
public class AesEncryptionData
91103
{
92104
public byte[] Data { get; set; }
93105
public byte[] Iv { get; set; }

Encrypt/AesEncryption/BaseAesEncryption.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,32 @@ public virtual byte[] EncryptAES(byte[] data, byte[] key, byte[] iv)
3434
}
3535
}
3636

37-
// Method to generate a random byte array of given length
38-
// Used to get the IV
39-
// Generate a random 16-byte IV for AES in CBC mode
40-
public static byte[] GenerateRandomBytes(int length)
37+
// Method to decrypt data using AES algorithm
38+
public static byte[] DecryptAES(byte[] encryptedData, byte[] key, byte[] iv)
39+
{
40+
using (Aes aes = Aes.Create())
41+
{
42+
aes.Key = key;
43+
aes.IV = iv;
44+
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
45+
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
46+
{
47+
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
48+
{
49+
using (MemoryStream decryptedStream = new MemoryStream())
50+
{
51+
cryptoStream.CopyTo(decryptedStream);
52+
return decryptedStream.ToArray();
53+
}
54+
}
55+
}
56+
}
57+
}
58+
59+
// Method to generate a random byte array of given length
60+
// Used to get the IV
61+
// Generate a random 16-byte IV for AES in CBC mode
62+
public static byte[] GenerateRandomBytes(int length)
4163
{
4264
byte[] randomBytes = new byte[length];
4365
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())

0 commit comments

Comments
 (0)