Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@

namespace SafeCrypt
{
public class Encrypt : BaseAesEncryption
public class AesEncryption : BaseAesEncryption
{
public byte[] AesEncrypt(byte[] data, byte[] secretKey, byte[] iv)
=> EncryptAES(data, secretKey, iv);

public byte[] AesDecrypt(byte[] data, byte[] secretKey, byte[] iv)
=> DecryptAES(data, secretKey, iv);

public byte[] AesEncrypt(string data, string secretKey, string iv)
{
NullChecks(data, secretKey, iv);
var convertedKeys = ConvertKeysToBytes(secretKey, iv);

var aesKey = Encoding.UTF8.GetBytes(secretKey);
var aesIv = Encoding.UTF8.GetBytes(iv);
var aesData = data.HexadecimalStringToByteArray();
var aesKey = convertedKeys.Item1;
var aesIv = convertedKeys.Item2;

var aesData = data.HexadecimalStringToByteArray();
return EncryptAES(aesData, aesKey, aesIv);
}

public AesData AesEncrypt(string data, string secretKey)
public AesEncryptionData AesEncrypt(string data, string secretKey)
{
NullChecks(data, secretKey);

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

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

var responseData = new AesData
var responseData = new AesEncryptionData
{
Data = response,
Iv = aesIv
Expand Down Expand Up @@ -60,34 +64,42 @@ public string AesEncryptByteToString(byte[] data, byte[] secretKey, byte[] iv)
return cipherText.BytesToString();
}

// needs methods that would accept aes mode
//public byte[] AesEncrypt(byte[] data, byte[] key, byte[] iv, ReturnType returnType)
//{

//}

private void NullChecks(string data, string secretKey, string iv)
{
if (data == null || data.Length <= 0)
throw new ArgumentNullException("plainText");
throw new ArgumentNullException(nameof(data));

if (secretKey == null || secretKey.Length <= 0)
throw new ArgumentNullException("Key");
throw new ArgumentNullException(nameof(secretKey));

if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("IV");
throw new ArgumentNullException(nameof(iv));
}

private (byte[], byte[]) ConvertKeysToBytes(string secretKey, string ivKey)
{
var secret = Encoding.UTF8.GetBytes(secretKey);
var iv = Encoding.UTF8.GetBytes(ivKey);

return (secret, iv);
}
private void NullChecks(string data, string secretKey)
{
if (data == null || data.Length <= 0)
throw new ArgumentNullException("plainText");
throw new ArgumentNullException(nameof(data));

if (secretKey == null || secretKey.Length <= 0)
throw new ArgumentNullException("Key");
}

//public byte[] AesEncrypt(byte[] data, byte[] key, byte[] iv, ReturnType returnType)
//{

//}
throw new ArgumentNullException(nameof(secretKey));
}
}

public class AesData
public class AesEncryptionData
{
public byte[] Data { get; set; }
public byte[] Iv { get; set; }
Expand Down
30 changes: 26 additions & 4 deletions Encrypt/AesEncryption/BaseAesEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,32 @@ public virtual byte[] EncryptAES(byte[] data, byte[] key, byte[] iv)
}
}

// Method to generate a random byte array of given length
// Used to get the IV
// Generate a random 16-byte IV for AES in CBC mode
public static byte[] GenerateRandomBytes(int length)
// Method to decrypt data using AES algorithm
public static byte[] DecryptAES(byte[] encryptedData, byte[] key, byte[] iv)
{
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
using (MemoryStream decryptedStream = new MemoryStream())
{
cryptoStream.CopyTo(decryptedStream);
return decryptedStream.ToArray();
}
}
}
}
}

// Method to generate a random byte array of given length
// Used to get the IV
// Generate a random 16-byte IV for AES in CBC mode
public static byte[] GenerateRandomBytes(int length)
{
byte[] randomBytes = new byte[length];
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
Expand Down