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
74 changes: 74 additions & 0 deletions src/SafeCrypt.Lib/Encryption/Blowfish/Blowfish.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Text;
using System;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;

namespace SafeCrypt.Encryption.BlowfishEncryption
{
public class Blowfish
{
private const int BlockSize = 8; // Blowfish block size is fixed at 8 bytes

/// <summary>
/// Encrypts a plaintext string using the Blowfish algorithm.
/// </summary>
/// <param name="plainText">The text to encrypt.</param>
/// <param name="key">The encryption key (up to 448 bits).</param>
/// <returns>Base64-encoded encrypted text.</returns>
public static string Encrypt(string plainText, byte[] key)
{
var cipher = GeneratePaddedBufferedBlockCipher();

cipher.Init(true, new KeyParameter(key));

byte[] inputBytes = Encoding.UTF8.GetBytes(plainText);
byte[] encryptedBytes = ProcessCipher(cipher, inputBytes);

return Convert.ToBase64String(encryptedBytes);
}

/// <summary>
/// Decrypts an encrypted Base64 string using the Blowfish algorithm.
/// </summary>
/// <param name="cipherText">The Base64-encoded encrypted text.</param>
/// <param name="key">The decryption key (same key used for encryption).</param>
/// <returns>The decrypted plaintext string.</returns>
public static string Decrypt(string cipherText, byte[] key)
{
var cipher = GeneratePaddedBufferedBlockCipher();

cipher.Init(false, new KeyParameter(key));

byte[] encryptedBytes = Convert.FromBase64String(cipherText);
byte[] decryptedBytes = ProcessCipher(cipher, encryptedBytes);

return Encoding.UTF8.GetString(decryptedBytes);
}

/// <summary>
/// Processes the encryption or decryption using the cipher.
/// </summary>
/// <param name="cipher">The cipher to process the data.</param>
/// <param name="input">The input data to process.</param>
/// <returns>The processed data.</returns>
private static byte[] ProcessCipher(IBufferedCipher cipher, byte[] input)
{
try
{
return cipher.DoFinal(input);
}
catch (CryptoException ex)
{
throw new InvalidOperationException("Error during cipher processing", ex);
}
}

private static PaddedBufferedBlockCipher GeneratePaddedBufferedBlockCipher()
{
var engine = new BlowfishEngine();
return new PaddedBufferedBlockCipher(engine);
}
}
}
22 changes: 21 additions & 1 deletion src/SafeCrypt.Lib/Helpers/KeyGenerators.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Org.BouncyCastle.Security;
using System;
using System.Security.Cryptography;

namespace SafeCrypt.Helpers
Expand Down Expand Up @@ -101,5 +102,24 @@ public static Tuple<string, string> GenerateRsaKeys(int keySize)
return new Tuple<string, string>(publicKey, privateKey);
}
}

/// <summary>
/// Generates a random key of the specified length.
/// </summary>
/// <param name="keySizeBits">Key size in bits (32 to 448).</param>
/// <returns>The generated key as a byte array.</returns>
public static byte[] GenerateBlowfishKey(int keySizeBits)
{
if (keySizeBits < 32 || keySizeBits > 448 || keySizeBits % 8 != 0)
{
throw new ArgumentException("Key size must be between 32 and 448 bits, in multiples of 8.");
}

var random = new SecureRandom();
byte[] key = new byte[keySizeBits / 8];
random.NextBytes(key);

return key;
}
}
}
12 changes: 7 additions & 5 deletions src/SafeCrypt.Lib/SafeCrypt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageLicenseFile>MitLicense.txt</PackageLicenseFile>
<PackageReleaseNotes>Release Note - Version 1.2.2
<PackageReleaseNotes>Release Note - Version 1.3.0

We are excited to announce the latest version of SafeCrypt (v1.3.0), bringing a new feature to enhance your experience with our encryption library.

What's New:
We updated some code comments, typo and repo url

What's coming:
More easy to use encryption methods
Addithing of Blow fish algorithm

Feedback and Contributions:
We appreciate your feedback and contributions! If you encounter any issues or have suggestions, please create an issue on GitHub: https://github.com/selfmadecode/SafeKrypt.Data.Security/issues

Thank you for using the Library!</PackageReleaseNotes>
<Version>1.2.2</Version>
Thank you for using the SafeCrypt Library!</PackageReleaseNotes>
<Version>1.3.0</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -46,6 +47,7 @@ Thank you for using the Library!</PackageReleaseNotes>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

Expand Down
3 changes: 3 additions & 0 deletions src/SafeCrypt.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// See https://aka.ms/new-console-template for more information
using SafeCrypt.App.Usage;
using safecrypt_testapp.Usage;

BlowfishTest.Test();

await RsaUsage.Execute();

Expand Down
27 changes: 27 additions & 0 deletions src/SafeCrypt.Test/Usage/Blowfish.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using SafeCrypt.Encryption;
using SafeCrypt.Helpers;
using SafeCrypt.Encryption.BlowfishEncryption;

namespace safecrypt_testapp.Usage
{
public static class BlowfishTest
{
public static void Test()
{
// Generate a random key (128 bits in this example)
int keySize = 128; // Todo: validate keysize, it can be 32, 64, ..., 448
byte[] key = KeyGenerators.GenerateBlowfishKey(keySize);

string plainText = "Hello, Blowfish!";
Console.WriteLine($"Original Text: {plainText}");

// Encrypt
string encryptedText = Blowfish.Encrypt(plainText, key);
Console.WriteLine($"Encrypted Text (Base64): {encryptedText}");

// Decrypt
string decryptedText = Blowfish.Decrypt(encryptedText, key);
Console.WriteLine($"Decrypted Text: {decryptedText}");
}
}
}