Skip to content

Commit 930f3ac

Browse files
authored
Merge pull request #3 from selfmadecode/dev_raphael_aes
Adding encryption overload methods - merge dev_raphael_aes branch into dev
2 parents ca35140 + 44644bd commit 930f3ac

5 files changed

Lines changed: 152 additions & 4 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.IO;
3+
using System.Security.Cryptography;
4+
5+
namespace SafeCrypt
6+
{
7+
public class BaseAesEncryption
8+
{
9+
// Method to encrypt data using AES algorithm
10+
public virtual byte[] EncryptAES(byte[] data, byte[] key, byte[] iv)
11+
{
12+
try
13+
{
14+
using (Aes aes = Aes.Create())
15+
{
16+
aes.Key = key;
17+
aes.IV = iv;
18+
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
19+
using (MemoryStream memoryStream = new MemoryStream())
20+
{
21+
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
22+
{
23+
cryptoStream.Write(data, 0, data.Length);
24+
cryptoStream.FlushFinalBlock();
25+
26+
return memoryStream.ToArray();
27+
}
28+
}
29+
}
30+
}
31+
catch (Exception ex)
32+
{
33+
throw;
34+
}
35+
}
36+
}
37+
}

Encrypt/AesEncryption/Encrypt.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Text;
3+
using System.Security.Cryptography;
4+
5+
namespace SafeCrypt
6+
{
7+
public class Encrypt : BaseAesEncryption
8+
{
9+
public byte[] AesEncrypt(byte[] data, byte[] secretKey, byte[] iv)
10+
=> EncryptAES(data, secretKey, iv);
11+
12+
public byte[] AesEncrypt(string data, string secretKey, string iv)
13+
{
14+
NullChecks(data, secretKey, iv);
15+
16+
var aesKey = Encoding.UTF8.GetBytes(secretKey);
17+
var aesIv = Encoding.UTF8.GetBytes(iv);
18+
var aesData = data.HexadecimalStringToByteArray();
19+
20+
return EncryptAES(aesData, aesKey, aesIv);
21+
}
22+
23+
public string AesEncryptByteToHexString(byte[] data, byte[] secretKey, byte[] iv)
24+
{
25+
var cipherText = EncryptAES(data, secretKey, iv);
26+
27+
return cipherText.ByteArrayToHexString();
28+
}
29+
30+
public string AesEncryptByteToBase64String(byte[] data, byte[] secretKey, byte[] iv)
31+
{
32+
var cipherText = EncryptAES(data, secretKey, iv);
33+
34+
return Convert.ToBase64String(cipherText);
35+
}
36+
37+
public string AesEncryptByteToString(byte[] data, byte[] secretKey, byte[] iv)
38+
{
39+
var cipherText = EncryptAES(data, secretKey, iv);
40+
41+
return cipherText.BytesToString();
42+
}
43+
44+
private void NullChecks(string data, string secretKey, string iv)
45+
{
46+
if (data == null || data.Length <= 0)
47+
throw new ArgumentNullException("plainText");
48+
49+
if (secretKey == null || secretKey.Length <= 0)
50+
throw new ArgumentNullException("Key");
51+
52+
if (iv == null || iv.Length <= 0)
53+
throw new ArgumentNullException("IV");
54+
}
55+
56+
//public byte[] AesEncrypt(byte[] data, byte[] key, byte[] iv, ReturnType returnType)
57+
//{
58+
59+
//}
60+
}
61+
}

Enums/ReturnType.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SafeCrypt
6+
{
7+
public enum ReturnType
8+
{
9+
Hex = 1,
10+
Bytes
11+
}
12+
}

Helpers/Converters.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
6+
namespace SafeCrypt
7+
{
8+
public static class Converters
9+
{
10+
public static string ByteArrayToHexString(this byte[] bytes)
11+
{
12+
StringBuilder hex = new StringBuilder(bytes.Length * 2);
13+
foreach (byte b in bytes)
14+
hex.AppendFormat("{0:x2}", b);
15+
return hex.ToString();
16+
}
17+
18+
public static string BytesToString(this byte[] bytes)
19+
{
20+
using (MemoryStream stream = new MemoryStream(bytes))
21+
{
22+
using (StreamReader streamReader = new StreamReader(stream))
23+
{
24+
return streamReader.ReadToEnd();
25+
}
26+
}
27+
}
28+
29+
public static byte[] HexadecimalStringToByteArray(this string input)
30+
{
31+
var outputLength = input.Length / 2;
32+
var output = new byte[outputLength];
33+
using (var sr = new StringReader(input))
34+
{
35+
for (var i = 0; i < outputLength; i++)
36+
output[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(),
37+
(char)sr.Read() }), 16);
38+
}
39+
return output;
40+
}
41+
}
42+
}

SafeCrypt.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7-
<ItemGroup>
8-
<Folder Include="AES\" />
9-
</ItemGroup>
10-
117
</Project>

0 commit comments

Comments
 (0)