diff --git a/README.md b/README.md index 1d71a14..70a53ed 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,21 @@ -# SafeCrypt Library - -A C# library for encryption and decryption. +## SafeKrypt Library +[![NuGet Version](https://img.shields.io/nuget/v/SafeCrypt.Data.Security.svg?style=flat&logo=nuget)](https://www.nuget.org/packages/SafeCrypt.Data.Security) +[![NuGet Downloads](https://img.shields.io/nuget/dt/SafeCrypt.Data.Security.svg?style=flat&logo=nuget)](https://www.nuget.org/packages/SafeCrypt.Data.Security) +[![License](https://img.shields.io/github/license/selfmadecode/SafeKrypt.Data.Security.svg?style=flat)](LICENSE) +[![GitHub Stars](https://img.shields.io/github/stars/selfmadecode/SafeKrypt.Data.Security?style=flat&logo=github)](https://github.com/selfmadecode/SafeKrypt.Data.Security/stargazers) +[![GitHub Issues](https://img.shields.io/github/issues/selfmadecode/SafeKrypt.Data.Security.svg?style=flat)](https://github.com/selfmadecode/SafeKrypt.Data.Security/issues) +[![Open PRs](https://img.shields.io/github/issues-pr/selfmadecode/SafeKrypt.Data.Security.svg?style=flat)](https://github.com/selfmadecode/SafeKrypt.Data.Security/pulls) +[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat)](https://github.com/selfmadecode/SafeKrypt.Data.Security/pulls) +![.NET Standard 2.0](https://img.shields.io/badge/.NET-Standard%202.0-512BD4?style=flat&logo=dot-net) + +A C# utility library for encryption and decryption. ## Overview -The SafeCrypt library provides a set of methods for encrypting and decrypting data using various encryption algorithms, +This library provides a set of methods for encrypting and decrypting data using various encryption algorithms, including the Advanced Encryption Standard (AES) and RSA (Rivest–Shamir–Adleman). It is designed to be easy to use and can be integrated into C# applications that require secure data transmission or storage. + ## Table of Contents - [Installation](#installation) @@ -17,13 +26,12 @@ It is designed to be easy to use and can be integrated into C# applications that ## Installation -To use the SafeCrypt library in your C# project, follow these steps: +To use this library in your C# project, follow these steps: 1. Clone the repository: ```bash - git clone https://github.com/selfmadecode/SafeCrypt - cd SafeCrypt + git clone https://github.com/selfmadecode/SafeKrypt.Data.Security ``` 2. Build the project: @@ -32,7 +40,7 @@ To use the SafeCrypt library in your C# project, follow these steps: dotnet build ``` -Now, you can reference the SafeCrypt library in your C# project. +Now, you can reference the library in your C# project. ## Aes To use AES encryption in your C# application, access the static Aes class directly. @@ -49,7 +57,7 @@ For more details on RSA Encryption, check the [Rsa.md](doc/Rsa.md) document. ## Contributing -If you would like to contribute to the development of the SafeCrypt library, follow these steps: +If you would like to contribute to the development of the library, follow these steps: 1. Create an issue to discuss the proposed changes or bug fixes. 2. Fork the repository and create a new branch for your work: @@ -65,4 +73,4 @@ If you would like to contribute to the development of the SafeCrypt library, fol ## License -This project is licensed under the MIT License - see the [LICENSE](https://github.com/selfmadecode/SafeCrypt/tree/master?tab=MIT-1-ov-file) file for details. +This project is licensed under the MIT License - see the [LICENSE](https://github.com/selfmadecode/SafeKrypt.Data.Security/tree/master?tab=MIT-1-ov-file) file for details. diff --git a/src/SafeCrypt.Lib/Encryption/Blowfish/Blowfish.cs b/src/SafeCrypt.Lib/Encryption/Blowfish/Blowfish.cs new file mode 100644 index 0000000..dbbd4dd --- /dev/null +++ b/src/SafeCrypt.Lib/Encryption/Blowfish/Blowfish.cs @@ -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 + + /// + /// Encrypts a plaintext string using the Blowfish algorithm. + /// + /// The text to encrypt. + /// The encryption key (up to 448 bits). + /// Base64-encoded encrypted text. + 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); + } + + /// + /// Decrypts an encrypted Base64 string using the Blowfish algorithm. + /// + /// The Base64-encoded encrypted text. + /// The decryption key (same key used for encryption). + /// The decrypted plaintext string. + 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); + } + + /// + /// Processes the encryption or decryption using the cipher. + /// + /// The cipher to process the data. + /// The input data to process. + /// The processed data. + 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); + } + } +} diff --git a/src/SafeCrypt.Lib/Helpers/KeyGenerators.cs b/src/SafeCrypt.Lib/Helpers/KeyGenerators.cs index ef1d5f6..e7aaf90 100644 --- a/src/SafeCrypt.Lib/Helpers/KeyGenerators.cs +++ b/src/SafeCrypt.Lib/Helpers/KeyGenerators.cs @@ -1,4 +1,5 @@ -using System; +using Org.BouncyCastle.Security; +using System; using System.Security.Cryptography; namespace SafeCrypt.Helpers @@ -101,5 +102,24 @@ public static Tuple GenerateRsaKeys(int keySize) return new Tuple(publicKey, privateKey); } } + + /// + /// Generates a random key of the specified length. + /// + /// Key size in bits (32 to 448). + /// The generated key as a byte array. + 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; + } } } diff --git a/src/SafeCrypt.Lib/SafeCrypt.csproj b/src/SafeCrypt.Lib/SafeCrypt.csproj index 9908d08..5bec7b0 100644 --- a/src/SafeCrypt.Lib/SafeCrypt.csproj +++ b/src/SafeCrypt.Lib/SafeCrypt.csproj @@ -3,41 +3,32 @@ netstandard2.0 $(AssemblyName).Data.Security - SafeCrypt - Simple Encryption Library for C# + Simple encryption utility library for C# selfmade NextGen - The SafeCrypt library provides a set of methods for encrypting and decrypting data using various encryption algorithms, including the Advanced Encryption Standard (AES) and RSA (Rivest–Shamir–Adleman). It is designed to be easy to use and can be integrated into C# applications that require secure data transmission or storage. + This library provides a set of methods for encrypting and decrypting data using various encryption algorithms, including the Advanced Encryption Standard (AES) and RSA (Rivest–Shamir–Adleman). It is designed to be easy to use and can be integrated into C# applications that require secure data transmission or storage. Raphael Anyanwu - https://github.com/selfmadecode/SafeCrypt - https://github.com/selfmadecode/SafeCrypt - encryption;aes;cryptography;base64;aes + https://github.com/selfmadecode/SafeKrypt.Data.Security + https://github.com/selfmadecode/SafeKrypt.Data.Security + encryption;aes;cryptography;base64;algorithm;rsa;decryption safecrypticon.jpg README.md True MitLicense.txt - Release Note - Version 1.2.0 + Release Note - Version 1.3.0 -We are excited to announce the latest version of SafeCrypt (v1.2.0), bringing a new feature, improvements, and bug fixes to enhance your experience with our encryption library. +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 -Aes Class Renamed: -We renamed the AesEncryption and AesDecryption class to Aes, and consolidate all encrypting and decrypting methods within this single static class. This means you can now directly access the class without the need for instantiation. - -Introduction of RSACryptoServiceProvider Algorithm: -We have introduced the RSACryptoServiceProvider algorithm, allowing users to choose the encryption method that best suits their specific requirements. - -Bug Fixes for Aes: -We have addressed and resolved several bugs in the Aes implementation. - -Upgrade Command: -dotnet add package SafeCrypt --version 1.2.0 +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/SafeCrypt/issues +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 SafeCrypt Library! - 1.2.0 + 1.3.0 @@ -56,6 +47,7 @@ Thank you for using the SafeCrypt Library! + diff --git a/src/SafeCrypt.Test/Program.cs b/src/SafeCrypt.Test/Program.cs index e9abc73..8f57e6f 100644 --- a/src/SafeCrypt.Test/Program.cs +++ b/src/SafeCrypt.Test/Program.cs @@ -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(); diff --git a/src/SafeCrypt.Test/Usage/Blowfish.cs b/src/SafeCrypt.Test/Usage/Blowfish.cs new file mode 100644 index 0000000..97645a0 --- /dev/null +++ b/src/SafeCrypt.Test/Usage/Blowfish.cs @@ -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}"); + } + } +}