Skip to content

Commit 7114bdf

Browse files
authored
Merge pull request #17 from selfmadecode/dev_raphael_aes
include decrypting class
2 parents 338bb63 + aa6cf91 commit 7114bdf

4 files changed

Lines changed: 136 additions & 124 deletions

File tree

src/Encryption/AesEncryption/AesEncryption.cs

Lines changed: 0 additions & 124 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using SafeCrypt.src.Encrypt.AesEncryption;
2+
using SafeCrypt.src.Helpers;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace SafeCrypt.src.Encryption.AesEncryption
8+
{
9+
public class Decrypting : BaseAesEncryption
10+
{
11+
public byte[] AesDecrypt(string data, string secretKey, string iv)
12+
{
13+
NullChecks(data, secretKey, iv);
14+
15+
var (aesKey, aesIv) = ConvertKeysToBytesAndGetKeys(secretKey, iv);
16+
17+
var aesData = data.HexadecimalStringToByteArray();
18+
return DecryptAES(aesData, aesKey, aesIv);
19+
}
20+
21+
public byte[] Decrypt(byte[] data, byte[] secretKey, byte[] iv)
22+
=> DecryptAES(data, secretKey, iv);
23+
24+
25+
private void NullChecks(string data, string secretKey, string iv)
26+
{
27+
if (data == null || data.Length <= 0)
28+
throw new ArgumentNullException(nameof(data));
29+
30+
if (secretKey == null || secretKey.Length <= 0)
31+
throw new ArgumentNullException(nameof(secretKey));
32+
33+
if (iv == null || iv.Length <= 0)
34+
throw new ArgumentNullException(nameof(iv));
35+
}
36+
37+
private (byte[], byte[]) ConvertKeysToBytesAndGetKeys(string secretKey, string iv)
38+
{
39+
40+
return (secretKey.ConvertKeysToBytes(), iv.ConvertKeysToBytes());
41+
}
42+
}
43+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Text;
3+
using System.Security.Cryptography;
4+
using SafeCrypt.src.Helpers;
5+
using SafeCrypt.src.Encryption.AesEncryption.Models;
6+
7+
namespace SafeCrypt.src.Encrypt.AesEncryption
8+
{
9+
public class Encrypting : BaseAesEncryption
10+
{
11+
public byte[] Encrypt(byte[] data, byte[] secretKey, byte[] iv)
12+
=> EncryptAES(data, secretKey, iv);
13+
14+
15+
public byte[] Encrypt(string data, string secretKey, string iv)
16+
{
17+
NullChecks(data, secretKey, iv);
18+
19+
var aesKey = secretKey.ConvertKeysToBytes();
20+
var aesIv = iv.ConvertKeysToBytes();
21+
22+
var aesData = data.HexadecimalStringToByteArray();
23+
return EncryptAES(aesData, aesKey, aesIv);
24+
}
25+
26+
27+
28+
public AesEncryptionData Encrypt(string data, string secretKey)
29+
{
30+
NullChecks(data, secretKey);
31+
32+
var aesIv = GenerateRandomBytes(16);
33+
34+
var aesKey = Encoding.UTF8.GetBytes(secretKey);
35+
var aesData = data.HexadecimalStringToByteArray();
36+
37+
var response = EncryptAES(aesData, aesKey, aesIv);
38+
39+
var responseData = new AesEncryptionData
40+
{
41+
Data = response,
42+
Iv = aesIv
43+
};
44+
45+
return responseData;
46+
}
47+
48+
public string EncryptByteToHexString(byte[] data, byte[] secretKey, byte[] iv)
49+
{
50+
var cipherText = EncryptAES(data, secretKey, iv);
51+
52+
return cipherText.ByteArrayToHexString();
53+
}
54+
55+
public string EncryptByteToBase64String(byte[] data, byte[] secretKey, byte[] iv)
56+
{
57+
var cipherText = EncryptAES(data, secretKey, iv);
58+
59+
return Convert.ToBase64String(cipherText);
60+
}
61+
62+
public string EncryptByteToString(byte[] data, byte[] secretKey, byte[] iv)
63+
{
64+
var cipherText = EncryptAES(data, secretKey, iv);
65+
66+
return cipherText.BytesToString();
67+
}
68+
69+
// needs methods that would accept aes mode
70+
//public byte[] AesEncrypt(byte[] data, byte[] key, byte[] iv, ReturnType returnType)
71+
//{
72+
73+
//}
74+
75+
private void NullChecks(string data, string secretKey, string iv = "")
76+
{
77+
if (data == null || data.Length <= 0)
78+
throw new ArgumentNullException(nameof(data));
79+
80+
if (secretKey == null || secretKey.Length <= 0)
81+
throw new ArgumentNullException(nameof(secretKey));
82+
83+
if (iv == null || iv.Length <= 0)
84+
throw new ArgumentNullException(nameof(iv));
85+
}
86+
}
87+
88+
}

src/Helpers/Converters.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@ public static byte[] HexadecimalStringToByteArray(this string input)
3838
}
3939
return output;
4040
}
41+
42+
public static byte[] ConvertKeysToBytes(this string data)
43+
{
44+
return Encoding.UTF8.GetBytes(data);
45+
}
4146
}
4247
}

0 commit comments

Comments
 (0)