Skip to content

Commit

Permalink
feat - Support for byte encryption and decryption on AES
Browse files Browse the repository at this point in the history
fix - Bug fixes and updates
  • Loading branch information
Sangeeth Nandakumar committed Aug 12, 2023
1 parent ac8c4ba commit 1b3dedf
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 42 deletions.
31 changes: 24 additions & 7 deletions Twileloop.Security.Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Cryptography;
using System;
using System.Security.Cryptography;
using Twileloop.Security.Encoding;
using Twileloop.Security.Encryption;
using Twileloop.Security.Hashing;
Expand Down Expand Up @@ -35,17 +36,33 @@ static void Main(string[] args)


// Encryptions
//-------------------------------

var text = "Sangeeth Nandakumar";
var textAsBytes = System.Text.Encoding.UTF8.GetBytes(text);


//1 - AES (Advanced Encryption Standard)
var aesEncryptedData = AESAlgorithm.EncryptText("Twileloop", key: "1234", iv: "1234567890123456");
var aesDecryptedData = AESAlgorithm.DecryptText(aesEncryptedData, key: "1234", iv: "1234567890123456");
//--------------------------------------
//Raw Bytes
var aesEncryptedBytes = AESAlgorithm.EncryptBytes(textAsBytes, key: "1234", iv: "1234567890123456");
var aesDecryptedBytes = AESAlgorithm.DecryptBytes(aesEncryptedBytes, key: "1234", iv: "1234567890123456");
//Text
var aesEncryptedString = AESAlgorithm.EncryptText("Twileloop", key: "1234", iv: "1234567890123456");
var aesDecryptedString = AESAlgorithm.DecryptText(aesEncryptedString, key: "1234", iv: "1234567890123456");
//File
AESAlgorithm.EncryptFile(@"D:\data.txt", @"D:\data_aes_encrypted.aes", key: "1234", iv: "1234567890123456");
AESAlgorithm.DecryptFile(@"D:\data_aes_encrypted.aes", @"D:\data_aes_decrypted.txt", key: "1234", iv: "1234567890123456");

//2 - RSA
//2 - RSA (Rivest-Shamir-Adleman)
//--------------------------------------
RSAAlgorithm.MakeRSAKeyPairs(out RSAParameters publicKey, out RSAParameters privateKey);
var rsaEncryptedData = RSAAlgorithm.EncryptText("Twileloop", publicKey);
var rsaDecryptedData = RSAAlgorithm.DecryptText(rsaEncryptedData, privateKey);
//Raw Bytes
var rsaEncryptedBytes = RSAAlgorithm.EncryptBytes(textAsBytes, publicKey);
var rsaDecryptedBytes = RSAAlgorithm.DecryptBytes(rsaEncryptedBytes, privateKey);
//Text
var rsaEncryptedText = RSAAlgorithm.EncryptText("Twileloop", publicKey);
var rsaDecryptedText = RSAAlgorithm.DecryptText(rsaEncryptedText, privateKey);
//File
RSAAlgorithm.EncryptFile(@"D:\data.txt", @"D:\data_rsa_encrypted.rsa", publicKey);
RSAAlgorithm.DecryptFile(@"D:\data_rsa_encrypted.rsa", @"D:\data_rsa_decrypted.txt", privateKey);

Expand Down
1 change: 0 additions & 1 deletion Twileloop.Security/Encoding/UTF8Encoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ public static string Decode(string encodedText)
return System.Text.Encoding.UTF8.GetString(encodedBytes);
}
}

}
51 changes: 28 additions & 23 deletions Twileloop.Security/Encryption/AESAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,63 @@ namespace Twileloop.Security.Encryption
{
public static class AESAlgorithm
{
public static string EncryptText(string plainText, string key, string iv)
public static byte[] EncryptBytes(byte[] rawData, string key, string iv)
{
using (Aes aes = Aes.Create())
using (var aes = Aes.Create())
{
byte[] keyBytes = DeriveKeyFromPassphrase(key, aes.KeySize / 8);
byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);

aes.Key = keyBytes;
aes.IV = ivBytes;

ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

byte[] encryptedBytes;
using (var ms = new MemoryStream())
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
byte[] plaintextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
cs.Write(plaintextBytes, 0, plaintextBytes.Length);
cs.Write(rawData, 0, rawData.Length);
cs.FlushFinalBlock();
encryptedBytes = ms.ToArray();
}

return Convert.ToBase64String(encryptedBytes);
return encryptedBytes;
}
}

public static string DecryptText(string encryptedText, string key, string iv)
public static byte[] DecryptBytes(byte[] encryptedData, string key, string iv)
{
using (Aes aes = Aes.Create())
using (var aes = Aes.Create())
{
byte[] keyBytes = DeriveKeyFromPassphrase(key, aes.KeySize / 8);
byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);

aes.Key = keyBytes;
aes.IV = ivBytes;

ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
byte[] decryptedBytes;
using (var ms = new MemoryStream())
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
using (var ms = new MemoryStream(encryptedData))
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
cs.Write(encryptedBytes, 0, encryptedBytes.Length);
cs.FlushFinalBlock();
decryptedBytes = ms.ToArray();
using (var decryptedMs = new MemoryStream())
{
cs.CopyTo(decryptedMs);
return decryptedMs.ToArray();
}
}

return System.Text.Encoding.UTF8.GetString(decryptedBytes);
}
}

public static string EncryptText(string plainText, string key, string iv)
{
var inputAsBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
var outputAsBytes = EncryptBytes(inputAsBytes, key, iv);
return Convert.ToBase64String(outputAsBytes);
}

public static string DecryptText(string encryptedText, string key, string iv)
{
var inputAsBytes = Convert.FromBase64String(encryptedText);
var outputAsBytes = DecryptBytes(inputAsBytes, key, iv);
return System.Text.Encoding.UTF8.GetString(outputAsBytes);
}


private static byte[] DeriveKeyFromPassphrase(string passphrase, int keySize)
{
byte[] salt = System.Text.Encoding.UTF8.GetBytes("SomeSaltValue"); // Provide a unique salt value
Expand Down
28 changes: 17 additions & 11 deletions Twileloop.Security/Encryption/RSAAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,38 @@ namespace Twileloop.Security.Encryption
{
public class RSAAlgorithm
{
public static string EncryptText(string plaintext, RSAParameters publicKey)
public static byte[] EncryptBytes(byte[] rawBytes, RSAParameters publicKey)
{
byte[] plaintextBytes = System.Text.Encoding.UTF8.GetBytes(plaintext);

using (RSA rsa = RSA.Create())
{
rsa.ImportParameters(publicKey);
byte[] encryptedBytes = rsa.Encrypt(plaintextBytes, RSAEncryptionPadding.OaepSHA256);

return Convert.ToBase64String(encryptedBytes);
byte[] encryptedBytes = rsa.Encrypt(rawBytes, RSAEncryptionPadding.OaepSHA256);
return encryptedBytes;
}
}

public static string DecryptText(string encryptedText, RSAParameters privateKey)
public static byte[] DecryptBytes(byte[] encryptedBytes, RSAParameters privateKey)
{
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);

using (RSA rsa = RSA.Create())
{
rsa.ImportParameters(privateKey);
byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, RSAEncryptionPadding.OaepSHA256);

return System.Text.Encoding.UTF8.GetString(decryptedBytes);
return decryptedBytes;
}
}

public static string EncryptText(string plaintext, RSAParameters publicKey)
{
byte[] plaintextBytes = System.Text.Encoding.UTF8.GetBytes(plaintext);
return Convert.ToBase64String(EncryptBytes(plaintextBytes, publicKey));
}

public static string DecryptText(string encryptedText, RSAParameters privateKey)
{
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
return System.Text.Encoding.UTF8.GetString(DecryptBytes(encryptedBytes, privateKey));
}

public static void EncryptFile(string inputFile, string outputFile, RSAParameters publicKey)
{
using (RSA rsa = RSA.Create())
Expand Down

0 comments on commit 1b3dedf

Please sign in to comment.