├── .github └── FUNDING.yml ├── .gitignore ├── DevsDaddy └── Shared │ └── CryptoLibrary │ ├── Core.meta │ ├── Core │ ├── ICryptoProvider.cs │ ├── ICryptoProvider.cs.meta │ ├── IProviderOptions.cs │ └── IProviderOptions.cs.meta │ ├── CryptoController.cs │ ├── CryptoController.cs.meta │ ├── CryptoFile.cs │ ├── CryptoFile.cs.meta │ ├── CryptoLibrary.asmdef │ ├── CryptoLibrary.asmdef.meta │ ├── CryptoModule.cs │ ├── CryptoModule.cs.meta │ ├── Modules.meta │ ├── Modules │ ├── AES.meta │ ├── AES │ │ ├── AESEncryptionOptions.cs │ │ ├── AESEncryptionOptions.cs.meta │ │ ├── AESProvider.cs │ │ └── AESProvider.cs.meta │ ├── Base64.meta │ ├── Base64 │ │ ├── Base64EncryptionOptions.cs │ │ ├── Base64EncryptionOptions.cs.meta │ │ ├── Base64Provider.cs │ │ └── Base64Provider.cs.meta │ ├── BlowFish.meta │ ├── BlowFish │ │ ├── BlowfishEncryptionOptions.cs │ │ ├── BlowfishEncryptionOptions.cs.meta │ │ ├── BlowfishProvider.cs │ │ └── BlowfishProvider.cs.meta │ ├── Crc32.meta │ ├── Crc32 │ │ ├── CRC32EncryptionOptions.cs │ │ ├── CRC32EncryptionOptions.cs.meta │ │ ├── CRC32Provider.cs │ │ ├── CRC32Provider.cs.meta │ │ ├── Crc32.cs │ │ └── Crc32.cs.meta │ ├── DES.meta │ ├── DES │ │ ├── DESEncryptionOptions.cs │ │ ├── DESEncryptionOptions.cs.meta │ │ ├── DESProvider.cs │ │ └── DESProvider.cs.meta │ ├── MD5.meta │ ├── MD5 │ │ ├── MD5EncryptionOptions.cs │ │ ├── MD5EncryptionOptions.cs.meta │ │ ├── MD5Provider.cs │ │ └── MD5Provider.cs.meta │ ├── PBKDF2.meta │ ├── PBKDF2 │ │ ├── PBKDF2EncryptionOptions.cs │ │ ├── PBKDF2EncryptionOptions.cs.meta │ │ ├── PBKDF2Provider.cs │ │ └── PBKDF2Provider.cs.meta │ ├── RIPEMD-160.meta │ ├── RIPEMD-160 │ │ ├── RIPEMD160.cs │ │ ├── RIPEMD160.cs.meta │ │ ├── RIPEMD160Managed.cs │ │ ├── RIPEMD160Managed.cs.meta │ │ ├── RipemdEncryptionOptions.cs │ │ ├── RipemdEncryptionOptions.cs.meta │ │ ├── RipemdProvider.cs │ │ └── RipemdProvider.cs.meta │ ├── RSA.meta │ ├── RSA │ │ ├── RSAEncryptionOptions.cs │ │ ├── RSAEncryptionOptions.cs.meta │ │ ├── RSAProvider.cs │ │ └── RSAProvider.cs.meta │ ├── SHA.meta │ ├── SHA │ │ ├── SHA1Provider.cs │ │ ├── SHA1Provider.cs.meta │ │ ├── SHA256Provider.cs │ │ ├── SHA256Provider.cs.meta │ │ ├── SHA512Provider.cs │ │ ├── SHA512Provider.cs.meta │ │ ├── SHAEncryptionOptions.cs │ │ └── SHAEncryptionOptions.cs.meta │ ├── TripleDES.meta │ ├── TripleDES │ │ ├── TripleDESEncryptionOptions.cs │ │ ├── TripleDESEncryptionOptions.cs.meta │ │ ├── TripleDESProvider.cs │ │ └── TripleDESProvider.cs.meta │ ├── Twofish.meta │ ├── Twofish │ │ ├── Twofish.cs │ │ ├── Twofish.cs.meta │ │ ├── TwofishEncryptionOptions.cs │ │ ├── TwofishEncryptionOptions.cs.meta │ │ ├── TwofishProvider.cs │ │ └── TwofishProvider.cs.meta │ ├── XOR.meta │ ├── XOR │ │ ├── XOREncryptionOptions.cs │ │ ├── XOREncryptionOptions.cs.meta │ │ ├── XORProvider.cs │ │ └── XORProvider.cs.meta │ ├── xxHash.meta │ └── xxHash │ │ ├── xxHashEncryptionOptions.cs │ │ ├── xxHashEncryptionOptions.cs.meta │ │ ├── xxHashProvider.cs │ │ └── xxHashProvider.cs.meta │ ├── Preview.png │ ├── Preview.png.meta │ ├── package.json │ └── package.json.meta ├── LICENSE └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 12 | polar: # Replace with a single Polar username 13 | custom: https://boosty.to/devsdaddy 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # This .gitignore file should be placed at the root of your Unity project directory 2 | # 3 | # Get latest from https://github.com/github/gitignore/blob/main/Unity.gitignore 4 | # 5 | /[Ll]ibrary/ 6 | /[Tt]emp/ 7 | /[Oo]bj/ 8 | /[Bb]uild/ 9 | /[Bb]uilds/ 10 | /[Ll]ogs/ 11 | /[Uu]ser[Ss]ettings/ 12 | 13 | # MemoryCaptures can get excessive in size. 14 | # They also could contain extremely sensitive data 15 | /[Mm]emoryCaptures/ 16 | 17 | # Recordings can get excessive in size 18 | /[Rr]ecordings/ 19 | 20 | # Uncomment this line if you wish to ignore the asset store tools plugin 21 | # /[Aa]ssets/AssetStoreTools* 22 | 23 | # Autogenerated Jetbrains Rider plugin 24 | /[Aa]ssets/Plugins/Editor/JetBrains* 25 | 26 | # Visual Studio cache directory 27 | .vs/ 28 | 29 | # Gradle cache directory 30 | .gradle/ 31 | 32 | # Autogenerated VS/MD/Consulo solution and project files 33 | ExportedObj/ 34 | .consulo/ 35 | *.csproj 36 | *.unityproj 37 | *.sln 38 | *.suo 39 | *.tmp 40 | *.user 41 | *.userprefs 42 | *.pidb 43 | *.booproj 44 | *.svd 45 | *.pdb 46 | *.mdb 47 | *.opendb 48 | *.VC.db 49 | 50 | # Unity3D generated meta files 51 | *.pidb.meta 52 | *.pdb.meta 53 | *.mdb.meta 54 | 55 | # Unity3D generated file on crash reports 56 | sysinfo.txt 57 | 58 | # Builds 59 | *.apk 60 | *.aab 61 | *.unitypackage 62 | *.app 63 | 64 | # Crashlytics generated file 65 | crashlytics-build.properties 66 | 67 | # Packed Addressables 68 | /[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin* 69 | 70 | # Temporary auto-generated Android Assets 71 | /[Aa]ssets/[Ss]treamingAssets/aa.meta 72 | /[Aa]ssets/[Ss]treamingAssets/aa/* 73 | -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Core.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e79982a065cc4bc4943d1db4ba896ae3 3 | timeCreated: 1709889672 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Core/ICryptoProvider.cs: -------------------------------------------------------------------------------- 1 | namespace DevsDaddy.Shared.CryptoLibrary.Core 2 | { 3 | /// 4 | /// Base Crypto-Provider Interface 5 | /// 6 | public interface ICryptoProvider 7 | { 8 | /// 9 | /// Decrypt String Data 10 | /// 11 | /// 12 | /// 13 | string DecryptString(string encryptedString); 14 | 15 | /// 16 | /// Decrypt Binary Data 17 | /// 18 | /// 19 | /// 20 | byte[] DecryptBinary(byte[] encryptedBinary); 21 | 22 | /// 23 | /// Encrypt String 24 | /// 25 | /// 26 | /// 27 | string EncryptString(string decryptedString); 28 | 29 | /// 30 | /// Encrypt Binary 31 | /// 32 | /// 33 | /// 34 | byte[] EncryptBinary(byte[] decryptedBinary); 35 | } 36 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Core/ICryptoProvider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b2dfd16ad84e46dcb349978d4d7332c3 3 | timeCreated: 1709889459 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Core/IProviderOptions.cs: -------------------------------------------------------------------------------- 1 | namespace DevsDaddy.Shared.CryptoLibrary.Core 2 | { 3 | /// 4 | /// Provider Options Interface 5 | /// 6 | public interface IProviderOptions { } 7 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Core/IProviderOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8334da39a9004bf5811d44dc29eaf43c 3 | timeCreated: 1709889484 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/CryptoController.cs: -------------------------------------------------------------------------------- 1 | using DevsDaddy.Shared.CryptoLibrary.Core; 2 | using DevsDaddy.Shared.CryptoLibrary.Modules.AES; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary 5 | { 6 | public static class CryptoController 7 | { 8 | // Default Provider 9 | private static ICryptoProvider defaultProvider = new AESProvider(); 10 | 11 | /// 12 | /// Set Default Crypto Provider 13 | /// 14 | /// 15 | public static void SetDefaultProvider(ICryptoProvider provider) { 16 | defaultProvider = provider; 17 | } 18 | 19 | /// 20 | /// Decrypt Encrypted Plain-Text using provided Crypto-provider or Default Crypto-provider 21 | /// 22 | /// 23 | /// 24 | /// 25 | public static string Decrypt(string encryptedString, ICryptoProvider provider = null) { 26 | if (provider == null) provider = defaultProvider; 27 | return provider.DecryptString(encryptedString); 28 | } 29 | 30 | /// 31 | /// Decrypt encrypted byte array using provided Crypto-provider or Default Crypto-provider 32 | /// 33 | /// 34 | /// 35 | /// 36 | public static byte[] Decrypt(byte[] encryptedArray, ICryptoProvider provider) { 37 | if (provider == null) provider = defaultProvider; 38 | return provider.DecryptBinary(encryptedArray); 39 | } 40 | 41 | /// 42 | /// Encrypt plain-text using provided Crypto-provider or Default Crypto-provider 43 | /// 44 | /// 45 | /// 46 | /// 47 | public static string Encrypt(string plainText, ICryptoProvider provider = null) { 48 | if (provider == null) provider = defaultProvider; 49 | return provider.EncryptString(plainText); 50 | } 51 | 52 | /// 53 | /// Encrypt binary array using provided Crypto-provider or Default Crypto-provider 54 | /// 55 | /// 56 | /// 57 | /// 58 | public static byte[] Encrypt(byte[] decryptedArray, ICryptoProvider provider = null) { 59 | if (provider == null) provider = defaultProvider; 60 | return provider.EncryptBinary(decryptedArray); 61 | } 62 | 63 | public static void Test() { 64 | CryptoFile.SetDefaultProvider(new AESProvider(new AESEncryptionOptions { 65 | cryptoKey = "key" 66 | })); 67 | 68 | string decryptedText = CryptoFile.ReadText("path_to_encrypted_file"); 69 | bool writtenFile = CryptoFile.WriteText("path_to_encrypted_file", decryptedText); 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/CryptoController.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 586014d07f2f4e3ab3881a37a1ee4e47 3 | timeCreated: 1709889696 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/CryptoFile.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using DevsDaddy.Shared.CryptoLibrary.Core; 6 | using DevsDaddy.Shared.CryptoLibrary.Modules.AES; 7 | using UnityEngine; 8 | 9 | namespace DevsDaddy.Shared.CryptoLibrary 10 | { 11 | public static class CryptoFile 12 | { 13 | // Default Provider 14 | private static ICryptoProvider defaultProvider = new AESProvider(); 15 | 16 | /// 17 | /// Set Default Crypto Provider 18 | /// 19 | /// 20 | public static void SetDefaultProvider(ICryptoProvider provider) { 21 | defaultProvider = provider; 22 | } 23 | 24 | /// 25 | /// Write Text file to path with encrypted plain-text using provided / default crypto-provider 26 | /// 27 | /// 28 | /// 29 | /// 30 | /// 31 | public static bool WriteText(string path, string plainText, ICryptoProvider provider = null) { 32 | try { 33 | if (provider == null) provider = defaultProvider; 34 | string encryptedText = provider.EncryptString(plainText); 35 | File.WriteAllText(path, encryptedText); 36 | return true; 37 | } 38 | catch (Exception ex) { 39 | Debug.LogError(ex); 40 | return false; 41 | } 42 | } 43 | 44 | /// 45 | /// Write Binary File to path encrypted byte array using provided / default crypto-provider 46 | /// 47 | /// 48 | /// 49 | /// 50 | /// 51 | public static bool WriteBinary(string path, byte[] byteArray, ICryptoProvider provider = null) { 52 | try { 53 | if (provider == null) provider = defaultProvider; 54 | byte[] encryptedText = provider.EncryptBinary(byteArray); 55 | File.WriteAllBytes(path, encryptedText); 56 | return true; 57 | } 58 | catch (Exception ex) { 59 | Debug.LogError(ex); 60 | return false; 61 | } 62 | } 63 | 64 | /// 65 | /// Read encrypted file and returns decrypted plain-text using provided / default crypto-provider 66 | /// 67 | /// 68 | /// 69 | /// 70 | public static async Task ReadTextAsync(string path, ICryptoProvider provider = null) { 71 | try { 72 | if (provider == null) provider = defaultProvider; 73 | string encryptedText = await File.ReadAllTextAsync(path); 74 | string decryptedText = provider.DecryptString(encryptedText); 75 | return decryptedText; 76 | } 77 | catch (Exception ex) { 78 | Debug.LogError(ex); 79 | return null; 80 | } 81 | } 82 | 83 | /// 84 | /// Read encrypted binary file and returns decrypted byte array using provided / default crypto-provider 85 | /// 86 | /// 87 | /// 88 | /// 89 | public static async Task ReadBinaryAsync(string path, ICryptoProvider provider = null) { 90 | try { 91 | if (provider == null) provider = defaultProvider; 92 | byte[] encryptedData = await File.ReadAllBytesAsync(path); 93 | byte[] decryptedData = provider.DecryptBinary(encryptedData); 94 | return decryptedData; 95 | } 96 | catch (Exception ex) { 97 | Debug.LogError(ex); 98 | return null; 99 | } 100 | } 101 | 102 | /// 103 | /// Write Text file to path with encrypted plain-text using provided / default crypto-provider 104 | /// 105 | /// 106 | /// 107 | /// 108 | /// 109 | public static async Task WriteTextAsync(string path, string plainText, ICryptoProvider provider = null) { 110 | try { 111 | if (provider == null) provider = defaultProvider; 112 | string encryptedText = provider.EncryptString(plainText); 113 | await File.WriteAllTextAsync(path, encryptedText); 114 | return true; 115 | } 116 | catch (Exception ex) { 117 | Debug.LogError(ex); 118 | return false; 119 | } 120 | } 121 | 122 | /// 123 | /// Write Binary File to path encrypted byte array using provided / default crypto-provider 124 | /// 125 | /// 126 | /// 127 | /// 128 | /// 129 | public static async Task WriteBinaryAsync(string path, byte[] byteArray, ICryptoProvider provider = null) { 130 | try { 131 | if (provider == null) provider = defaultProvider; 132 | byte[] encryptedText = provider.EncryptBinary(byteArray); 133 | await File.WriteAllBytesAsync(path, encryptedText); 134 | return true; 135 | } 136 | catch (Exception ex) { 137 | Debug.LogError(ex); 138 | return false; 139 | } 140 | } 141 | 142 | /// 143 | /// Read encrypted file and returns decrypted plain-text using provided / default crypto-provider 144 | /// 145 | /// 146 | /// 147 | /// 148 | public static string ReadText(string path, ICryptoProvider provider = null) { 149 | try { 150 | if (provider == null) provider = defaultProvider; 151 | string encryptedText = File.ReadAllText(path); 152 | string decryptedText = provider.DecryptString(encryptedText); 153 | return decryptedText; 154 | } 155 | catch (Exception ex) { 156 | Debug.LogError(ex); 157 | return null; 158 | } 159 | } 160 | 161 | /// 162 | /// Read encrypted binary file and returns decrypted byte array using provided / default crypto-provider 163 | /// 164 | /// 165 | /// 166 | /// 167 | public static byte[] ReadBinary(string path, ICryptoProvider provider = null) { 168 | try { 169 | if (provider == null) provider = defaultProvider; 170 | byte[] encryptedData = File.ReadAllBytes(path); 171 | byte[] decryptedData = provider.DecryptBinary(encryptedData); 172 | return decryptedData; 173 | } 174 | catch (Exception ex) { 175 | Debug.LogError(ex); 176 | return null; 177 | } 178 | } 179 | } 180 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/CryptoFile.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3b53aedd0664478da9df35f3cf683c18 3 | timeCreated: 1709889703 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/CryptoLibrary.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "CryptoLibrary" 3 | } 4 | -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/CryptoLibrary.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2576790177f6d514d97155b443dfc070 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/CryptoModule.cs: -------------------------------------------------------------------------------- 1 | namespace DevsDaddy.Shared.CryptoLibrary 2 | { 3 | public enum CryptoModule 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/CryptoModule.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b0c71a10e117439f93a15473c317dc03 3 | timeCreated: 1709889714 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d1f1668f543df7e4eae8debf0d7dcfba 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/AES.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2efc1c50b6ae4d659b53ee3428cc8c93 3 | timeCreated: 1709889626 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/AES/AESEncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | using System.Text; 3 | using DevsDaddy.Shared.CryptoLibrary.Core; 4 | 5 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.AES 6 | { 7 | [System.Serializable] 8 | public class AESEncryptionOptions : IProviderOptions 9 | { 10 | public int bufferKeySize = 32; 11 | public int blockSize = 256; 12 | public int keySize = 256; 13 | 14 | public string cryptoKey = "AESPassword"; 15 | public Encoding encoding = Encoding.UTF8; 16 | 17 | public CipherMode cipherMode = CipherMode.CBC; 18 | public PaddingMode paddingMode = PaddingMode.PKCS7; 19 | } 20 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/AES/AESEncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a102000f019449dfaf93ff3b1485a75a 3 | timeCreated: 1709890300 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/AES/AESProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Security.Cryptography; 4 | using DevsDaddy.Shared.CryptoLibrary.Core; 5 | 6 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.AES 7 | { 8 | /// 9 | /// AES Encryption Provider. 10 | /// Using for encrypt/decrypt plain text or byte arrays 11 | /// 12 | public class AESProvider : ICryptoProvider 13 | { 14 | private AESEncryptionOptions _options; 15 | 16 | /// 17 | /// AES Cryptography Provider 18 | /// 19 | /// 20 | public AESProvider(AESEncryptionOptions options = null) { 21 | _options = options ?? new AESEncryptionOptions(); 22 | } 23 | 24 | /// 25 | /// Decrypt AES-encrypted base-64 string to plain-text 26 | /// 27 | /// 28 | /// 29 | public string DecryptString(string encryptedString) { 30 | byte[] decrypted = DecryptBinary(Convert.FromBase64String(encryptedString)); 31 | return _options.encoding.GetString(decrypted); 32 | } 33 | 34 | /// 35 | /// Decrypt AES-encrypted binary 36 | /// 37 | /// 38 | /// 39 | public byte[] DecryptBinary(byte[] encryptedBinary) { 40 | RijndaelManaged rij = setupRijndaelManaged; 41 | 42 | List compile = new List(encryptedBinary); 43 | 44 | // First 32 bytes are salt. 45 | List salt = compile.GetRange(0, _options.bufferKeySize); 46 | // Second 32 bytes are IV. 47 | List iv = compile.GetRange(_options.bufferKeySize, _options.bufferKeySize); 48 | rij.IV = iv.ToArray(); 49 | 50 | Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(_options.cryptoKey, salt.ToArray()); 51 | byte[] bufferKey = deriveBytes.GetBytes(_options.bufferKeySize); // Convert 32 bytes of salt to password 52 | rij.Key = bufferKey; 53 | 54 | byte[] plain = compile.GetRange(_options.bufferKeySize * 2, compile.Count - (_options.bufferKeySize * 2)).ToArray(); 55 | 56 | using (ICryptoTransform decrypt = rij.CreateDecryptor(rij.Key, rij.IV)) 57 | { 58 | byte[] dest = decrypt.TransformFinalBlock(plain, 0, plain.Length); 59 | return dest; 60 | } 61 | } 62 | 63 | /// 64 | /// Encrypt plain-text to AES-encrypted base-64 string 65 | /// 66 | /// 67 | /// 68 | public string EncryptString(string decryptedString) { 69 | byte[] encrypted = EncryptBinary(_options.encoding.GetBytes(decryptedString)); 70 | return Convert.ToBase64String(encrypted); 71 | } 72 | 73 | /// 74 | /// Encrypt byte array to AES-encrypted byte array 75 | /// 76 | /// 77 | /// 78 | public byte[] EncryptBinary(byte[] decryptedBinary) { 79 | RijndaelManaged rij = setupRijndaelManaged; 80 | 81 | // A pseudorandom number is newly generated based on the inputted password 82 | Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(_options.cryptoKey, _options.bufferKeySize); 83 | // The missing parts are specified in advance to fill in 0 length 84 | byte[] salt = new byte[_options.bufferKeySize]; 85 | // Rfc2898DeriveBytes gets an internally generated salt 86 | salt = deriveBytes.Salt; 87 | // The 32-byte data extracted from the generated pseudorandom number is used as a password 88 | byte[] bufferKey = deriveBytes.GetBytes(_options.bufferKeySize); 89 | 90 | rij.Key = bufferKey; 91 | rij.GenerateIV(); 92 | 93 | using (ICryptoTransform encrypt = rij.CreateEncryptor(rij.Key, rij.IV)) 94 | { 95 | byte[] dest = encrypt.TransformFinalBlock(decryptedBinary, 0, decryptedBinary.Length); 96 | // first 32 bytes of salt and second 32 bytes of IV for the first 64 bytes 97 | List compile = new List(salt); 98 | compile.AddRange(rij.IV); 99 | compile.AddRange(dest); 100 | return compile.ToArray(); 101 | } 102 | } 103 | 104 | /// 105 | /// Setup Manager for AES 106 | /// 107 | private RijndaelManaged setupRijndaelManaged 108 | { 109 | get 110 | { 111 | RijndaelManaged rij = new RijndaelManaged(); 112 | rij.BlockSize = _options.blockSize; 113 | rij.KeySize = _options.keySize; 114 | rij.Mode = _options.cipherMode; 115 | rij.Padding = _options.paddingMode; 116 | return rij; 117 | } 118 | } 119 | } 120 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/AES/AESProvider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1f5f2cf3f4814af28f6d8b51353d90b6 3 | timeCreated: 1709889798 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Base64.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6a9b5fa32f0f4013b1d98a5a89d4b7c7 3 | timeCreated: 1709889648 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Base64/Base64EncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.Base64 5 | { 6 | [System.Serializable] 7 | public class Base64EncryptionOptions : IProviderOptions 8 | { 9 | public Encoding encoding = Encoding.UTF8; 10 | } 11 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Base64/Base64EncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c3484dc302e545a993986d2e3fe21c31 3 | timeCreated: 1709890394 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Base64/Base64Provider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.Base64 5 | { 6 | /// 7 | /// Base64 Encryption Provider. 8 | /// Using for encrypt/decrypt plain text or byte arrays 9 | /// 10 | public class Base64Provider : ICryptoProvider 11 | { 12 | private Base64EncryptionOptions _options; 13 | 14 | /// 15 | /// Base64 Encryption Provider 16 | /// 17 | /// 18 | public Base64Provider(Base64EncryptionOptions options = null) { 19 | _options = options ?? new Base64EncryptionOptions(); 20 | } 21 | 22 | /// 23 | /// Decrypt Base64 string to plain text 24 | /// 25 | /// 26 | /// 27 | public string DecryptString(string encryptedString) { 28 | byte[] decodedBytes = Convert.FromBase64String (encryptedString); 29 | string decodedText = _options.encoding.GetString (decodedBytes); 30 | return decodedText; 31 | } 32 | 33 | /// 34 | /// Decrypt Base64 byte array to decrypted byte array 35 | /// 36 | /// 37 | /// 38 | public byte[] DecryptBinary(byte[] encryptedBinary) { 39 | string encoded = _options.encoding.GetString(encryptedBinary); 40 | byte[] decodedBytes = Convert.FromBase64String(encoded); 41 | return decodedBytes; 42 | } 43 | 44 | /// 45 | /// Encrypt plain-text to base64 string 46 | /// 47 | /// 48 | /// 49 | public string EncryptString(string decryptedString) { 50 | byte[] bytesToEncode = _options.encoding.GetBytes (decryptedString); 51 | string encodedText = Convert.ToBase64String (bytesToEncode); 52 | return encodedText; 53 | } 54 | 55 | /// 56 | /// Encrypt byte array to Base64 binary 57 | /// 58 | /// 59 | /// 60 | public byte[] EncryptBinary(byte[] decryptedBinary) { 61 | string encodedText = Convert.ToBase64String(decryptedBinary); 62 | byte[] encodedBytes = _options.encoding.GetBytes (encodedText); 63 | return encodedBytes; 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Base64/Base64Provider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e306f37400714c7aad8a32136af895eb 3 | timeCreated: 1709890384 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/BlowFish.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d79d2dc5556347c094e744ba1e21c395 3 | timeCreated: 1709897312 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/BlowFish/BlowfishEncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.BlowFish 5 | { 6 | [System.Serializable] 7 | public class BlowfishEncryptionOptions : IProviderOptions 8 | { 9 | public Encoding encoding = Encoding.UTF8; 10 | public string cryptoKey = "MyCryptoKey"; 11 | } 12 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/BlowFish/BlowfishEncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 970075a8bcf84b42849dec0e3b47117e 3 | timeCreated: 1709901465 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/BlowFish/BlowfishProvider.cs: -------------------------------------------------------------------------------- 1 | using DevsDaddy.Shared.CryptoLibrary.Core; 2 | using System; 3 | using System.IO; 4 | using System.Text; 5 | 6 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.BlowFish 7 | { 8 | /// 9 | /// Blowfish Crypto Provider 10 | /// 11 | public class BlowfishProvider : ICryptoProvider 12 | { 13 | private BlowfishEncryptionOptions _options; 14 | 15 | const int N = 16; 16 | const int KEYBYTES = 8; 17 | 18 | static uint[] _P = 19 | { 20 | 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 21 | 0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 22 | 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b 23 | }; 24 | static uint[,] _S = 25 | { 26 | { 27 | 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, 28 | 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 29 | 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658, 30 | 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, 31 | 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e, 32 | 0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, 33 | 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6, 34 | 0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, 35 | 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c, 36 | 0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, 37 | 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1, 38 | 0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, 39 | 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a, 40 | 0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, 41 | 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, 0xa1f1651d, 0x39af0176, 42 | 0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, 43 | 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706, 44 | 0x1bfedf72, 0x429b023d, 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, 45 | 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b, 46 | 0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, 47 | 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c, 48 | 0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, 49 | 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a, 50 | 0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, 51 | 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760, 52 | 0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, 53 | 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, 0x695b27b0, 0xbbca58c8, 54 | 0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, 55 | 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33, 56 | 0x62fb1341, 0xcee4c6e8, 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, 57 | 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0, 58 | 0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, 59 | 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777, 60 | 0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, 61 | 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705, 62 | 0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, 63 | 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e, 64 | 0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, 65 | 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, 0x83260376, 0x6295cfa9, 66 | 0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, 67 | 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f, 68 | 0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, 69 | 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a 70 | }, 71 | { 72 | 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, 73 | 0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, 74 | 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65, 75 | 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, 76 | 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9, 77 | 0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, 78 | 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d, 79 | 0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, 80 | 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc, 81 | 0xc8b57634, 0x9af3dda7, 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, 82 | 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38, 0x4f6db908, 83 | 0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, 84 | 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124, 85 | 0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, 86 | 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908, 87 | 0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, 88 | 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b, 89 | 0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, 90 | 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa, 91 | 0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, 92 | 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d, 93 | 0x1939260f, 0x19c27960, 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, 94 | 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef, 0xbe6c5aa5, 95 | 0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, 96 | 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96, 97 | 0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, 98 | 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca, 99 | 0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, 100 | 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77, 101 | 0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, 102 | 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054, 103 | 0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, 104 | 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea, 105 | 0xdb6c4f15, 0xfacb4fd0, 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, 106 | 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2, 0x5b8d2646, 107 | 0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, 108 | 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea, 109 | 0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, 110 | 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e, 111 | 0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, 112 | 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd, 113 | 0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, 114 | 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7 115 | }, 116 | { 117 | 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, 118 | 0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, 119 | 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af, 120 | 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, 121 | 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4, 122 | 0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, 123 | 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec, 124 | 0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, 125 | 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332, 126 | 0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, 127 | 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58, 128 | 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, 129 | 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22, 130 | 0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, 131 | 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60, 132 | 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, 133 | 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99, 134 | 0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, 135 | 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74, 136 | 0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, 137 | 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3, 138 | 0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, 139 | 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979, 140 | 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, 141 | 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa, 142 | 0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, 143 | 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086, 144 | 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, 145 | 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24, 146 | 0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, 147 | 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84, 148 | 0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, 149 | 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09, 150 | 0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, 151 | 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe, 152 | 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, 153 | 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0, 154 | 0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, 155 | 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188, 156 | 0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, 157 | 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8, 158 | 0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, 159 | 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0 160 | }, 161 | { 162 | 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, 163 | 0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, 164 | 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79, 165 | 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, 166 | 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a, 167 | 0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, 168 | 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1, 169 | 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, 170 | 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797, 171 | 0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, 172 | 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6, 173 | 0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, 174 | 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba, 175 | 0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, 176 | 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5, 177 | 0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, 178 | 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce, 179 | 0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, 180 | 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd, 181 | 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, 182 | 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, 183 | 0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, 184 | 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc, 185 | 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, 186 | 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc, 187 | 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, 188 | 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a, 189 | 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, 190 | 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a, 191 | 0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, 192 | 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b, 193 | 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, 194 | 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, 195 | 0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, 196 | 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623, 197 | 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, 198 | 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a, 199 | 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, 200 | 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3, 201 | 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, 202 | 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c, 203 | 0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, 204 | 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6 205 | } 206 | }; 207 | 208 | uint[] P; 209 | uint[,] S; 210 | 211 | /// 212 | /// Blowfish Provider 213 | /// 214 | /// 215 | public BlowfishProvider(BlowfishEncryptionOptions options) { 216 | _options = options ?? new BlowfishEncryptionOptions(); 217 | 218 | byte[] key = _options.encoding.GetBytes(_options.cryptoKey); 219 | 220 | short i; 221 | short j; 222 | short k; 223 | uint data; 224 | uint datal; 225 | uint datar; 226 | 227 | P = _P.Clone() as uint[]; 228 | S = _S.Clone() as uint[,]; 229 | 230 | j = 0; 231 | for (i = 0; i < N + 2; ++i) 232 | { 233 | data = 0x00000000; 234 | for (k = 0; k < 4; ++k) 235 | { 236 | data = (data << 8) | key[j]; 237 | j++; 238 | if (j >= key.Length) 239 | { 240 | j = 0; 241 | } 242 | } 243 | P[i] = P[i] ^ data; 244 | } 245 | 246 | datal = 0x00000000; 247 | datar = 0x00000000; 248 | 249 | for (i = 0; i < N + 2; i += 2) 250 | { 251 | Encipher(ref datal, ref datar); 252 | P[i] = datal; 253 | P[i + 1] = datar; 254 | } 255 | 256 | for (i = 0; i < 4; ++i) 257 | { 258 | for (j = 0; j < 256; j += 2) 259 | { 260 | Encipher(ref datal, ref datar); 261 | 262 | S[i, j] = datal; 263 | S[i, j + 1] = datar; 264 | } 265 | } 266 | } 267 | 268 | private uint F(uint x) 269 | { 270 | ushort a; 271 | ushort b; 272 | ushort c; 273 | ushort d; 274 | uint y; 275 | 276 | d = (ushort)(x & 0x00FF); 277 | x >>= 8; 278 | c = (ushort)(x & 0x00FF); 279 | x >>= 8; 280 | b = (ushort)(x & 0x00FF); 281 | x >>= 8; 282 | a = (ushort)(x & 0x00FF); 283 | //y = ((S[0][a] + S[1][b]) ^ S[2][c]) + S[3][d]; 284 | y = S[0, a] + S[1, b]; 285 | y = y ^ S[2, c]; 286 | y = y + S[3, d]; 287 | 288 | return y; 289 | } 290 | 291 | private void Encipher(byte[] data, int length) 292 | { 293 | uint xl, xr; 294 | if ((length % 8) != 0) 295 | throw new Exception("Invalid Length"); 296 | for (int i = 0; i < length; i += 8) 297 | { 298 | // Encode the data in 8 byte blocks. 299 | xl = (uint)((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]); 300 | xr = (uint)((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]); 301 | Encipher(ref xl, ref xr); 302 | // Now Replace the data. 303 | data[i] = (byte)(xl >> 24); 304 | data[i + 1] = (byte)(xl >> 16); 305 | data[i + 2] = (byte)(xl >> 8); 306 | data[i + 3] = (byte)(xl); 307 | data[i + 4] = (byte)(xr >> 24); 308 | data[i + 5] = (byte)(xr >> 16); 309 | data[i + 6] = (byte)(xr >> 8); 310 | data[i + 7] = (byte)(xr); 311 | } 312 | } 313 | 314 | private void Encipher(ref uint xl, ref uint xr) 315 | { 316 | uint Xl; 317 | uint Xr; 318 | uint temp; 319 | short i; 320 | 321 | Xl = xl; 322 | Xr = xr; 323 | 324 | for (i = 0; i < N; ++i) 325 | { 326 | Xl = Xl ^ P[i]; 327 | Xr = F(Xl) ^ Xr; 328 | 329 | temp = Xl; 330 | Xl = Xr; 331 | Xr = temp; 332 | } 333 | 334 | temp = Xl; 335 | Xl = Xr; 336 | Xr = temp; 337 | 338 | Xr = Xr ^ P[N]; 339 | Xl = Xl ^ P[N + 1]; 340 | 341 | xl = Xl; 342 | xr = Xr; 343 | } 344 | 345 | private String Encipher(String data) 346 | { 347 | byte[] b = _options.encoding.GetBytes(data); 348 | Encipher(b, b.Length); 349 | return Convert.ToBase64String(b); 350 | } 351 | 352 | private void Decipher(byte[] data, int length) 353 | { 354 | uint xl, xr; 355 | if ((length % 8) != 0) 356 | throw new Exception("Invalid Length"); 357 | for (int i = 0; i < length; i += 8) 358 | { 359 | // Encode the data in 8 byte blocks. 360 | xl = (uint)((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]); 361 | xr = (uint)((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]); 362 | Decipher(ref xl, ref xr); 363 | // Now Replace the data. 364 | data[i] = (byte)(xl >> 24); 365 | data[i + 1] = (byte)(xl >> 16); 366 | data[i + 2] = (byte)(xl >> 8); 367 | data[i + 3] = (byte)(xl); 368 | data[i + 4] = (byte)(xr >> 24); 369 | data[i + 5] = (byte)(xr >> 16); 370 | data[i + 6] = (byte)(xr >> 8); 371 | data[i + 7] = (byte)(xr); 372 | } 373 | } 374 | 375 | private void Decipher(ref uint xl, ref uint xr) 376 | { 377 | uint Xl; 378 | uint Xr; 379 | uint temp; 380 | short i; 381 | 382 | Xl = xl; 383 | Xr = xr; 384 | 385 | for (i = N + 1; i > 1; --i) 386 | { 387 | Xl = Xl ^ P[i]; 388 | Xr = F(Xl) ^ Xr; 389 | 390 | /* Exchange Xl and Xr */ 391 | temp = Xl; 392 | Xl = Xr; 393 | Xr = temp; 394 | } 395 | 396 | /* Exchange Xl and Xr */ 397 | temp = Xl; 398 | Xl = Xr; 399 | Xr = temp; 400 | 401 | Xr = Xr ^ P[1]; 402 | Xl = Xl ^ P[0]; 403 | 404 | xl = Xl; 405 | xr = Xr; 406 | } 407 | 408 | private String Decipher(String data) 409 | { 410 | byte[] b = Convert.FromBase64String(data); 411 | Decipher(b, b.Length); 412 | 413 | return _options.encoding.GetString(b); 414 | } 415 | 416 | /// 417 | /// Decrypt text using Blowfish Provider 418 | /// 419 | /// 420 | /// 421 | public string DecryptString(string encryptedString) { 422 | return Decipher(encryptedString); 423 | } 424 | 425 | /// 426 | /// Decrypt byte array using Blowfish Provider 427 | /// 428 | /// 429 | /// 430 | public byte[] DecryptBinary(byte[] encryptedBinary) { 431 | byte[] decrypted = new byte[encryptedBinary.Length]; 432 | Array.Copy(encryptedBinary, decrypted, encryptedBinary.Length); 433 | Decipher(decrypted, decrypted.Length); 434 | return decrypted; 435 | } 436 | 437 | /// 438 | /// Encode plain-text using Blowfish Provider 439 | /// 440 | /// 441 | /// 442 | public string EncryptString(string decryptedString) { 443 | return Encipher(decryptedString); 444 | } 445 | 446 | /// 447 | /// Encrypt Binary using Blowfish Provider 448 | /// 449 | /// 450 | /// 451 | public byte[] EncryptBinary(byte[] decryptedBinary) { 452 | byte[] encrypted = new byte[decryptedBinary.Length]; 453 | Array.Copy(decryptedBinary, encrypted, decryptedBinary.Length); 454 | Decipher(encrypted, encrypted.Length); 455 | return encrypted; 456 | } 457 | } 458 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/BlowFish/BlowfishProvider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c50802061af74e20ab21681d6addd0c1 3 | timeCreated: 1709901452 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Crc32.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7a00b48dbb944ac0a2d68a7beb1f4d74 3 | timeCreated: 1709901092 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Crc32/CRC32EncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.Crc32 5 | { 6 | [System.Serializable] 7 | public class CRC32EncryptionOptions : IProviderOptions 8 | { 9 | public Encoding encoding = Encoding.UTF8; 10 | } 11 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Crc32/CRC32EncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c88999e508ec47fda502137b21747ede 3 | timeCreated: 1709901131 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Crc32/CRC32Provider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.Crc32 5 | { 6 | /// 7 | /// CRC32 Hash Provider 8 | /// 9 | public class CRC32Provider : ICryptoProvider 10 | { 11 | // Provider Options 12 | private CRC32EncryptionOptions _options; 13 | 14 | /// 15 | /// CRC32 Hash Provider 16 | /// 17 | /// 18 | public CRC32Provider(CRC32EncryptionOptions options = null) { 19 | _options = options ?? new CRC32EncryptionOptions(); 20 | } 21 | 22 | /// 23 | /// Decrypt String (Not Supported for hashing) 24 | /// 25 | /// 26 | /// 27 | /// 28 | public string DecryptString(string encryptedString) { 29 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 30 | } 31 | 32 | /// 33 | /// Decrypt Binary (Not Supported for hashing) 34 | /// 35 | /// 36 | /// 37 | /// 38 | public byte[] DecryptBinary(byte[] encryptedBinary) { 39 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 40 | } 41 | 42 | /// 43 | /// Get CRC32 of Plain Text 44 | /// 45 | /// 46 | /// 47 | public string EncryptString(string decryptedString) { 48 | byte[] encrypted = EncryptBinary(_options.encoding.GetBytes(decryptedString)); 49 | return Convert.ToBase64String(encrypted); 50 | } 51 | 52 | /// 53 | /// Encrypt Binary 54 | /// 55 | /// 56 | /// 57 | public byte[] EncryptBinary(byte[] decryptedBinary) { 58 | using (Crc32 crc = new Crc32()) 59 | { 60 | byte[] hashBytes = crc.ComputeHash(decryptedBinary); 61 | return hashBytes; 62 | } 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Crc32/CRC32Provider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 32e98cf987fa4d4ba5950846eccf09d8 3 | timeCreated: 1709901123 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Crc32/Crc32.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Security.Cryptography; 3 | using System.IO; 4 | 5 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.Crc32 6 | { 7 | public class Crc32 : HashAlgorithm 8 | { 9 | public const UInt32 DefaultPolynomial = 0xedb88320; 10 | public const UInt32 DefaultSeed = 0xffffffff; 11 | 12 | private UInt32 hash; 13 | private UInt32 seed; 14 | private UInt32[] table; 15 | private static UInt32[] defaultTable; 16 | 17 | public Crc32() { 18 | table = InitializeTable(DefaultPolynomial); 19 | seed = DefaultSeed; 20 | Initialize(); 21 | } 22 | 23 | public Crc32(UInt32 polynomial, UInt32 seed) { 24 | table = InitializeTable(polynomial); 25 | this.seed = seed; 26 | Initialize(); 27 | } 28 | 29 | public override void Initialize() { 30 | hash = seed; 31 | } 32 | 33 | protected override void HashCore(byte[] buffer, int start, int length) { 34 | hash = CalculateHash(table, hash, buffer, start, length); 35 | } 36 | 37 | protected override byte[] HashFinal() { 38 | byte[] hashBuffer = UInt32ToBigEndianBytes(~hash); 39 | this.HashValue = hashBuffer; 40 | return hashBuffer; 41 | } 42 | 43 | public override int HashSize { 44 | get { return 32; } 45 | } 46 | 47 | public static UInt32 Compute(byte[] buffer) { 48 | return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length); 49 | } 50 | 51 | public static UInt32 Compute(UInt32 seed, byte[] buffer) { 52 | return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length); 53 | } 54 | 55 | public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer) { 56 | return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length); 57 | } 58 | 59 | private static UInt32[] InitializeTable(UInt32 polynomial) { 60 | if (polynomial == DefaultPolynomial && defaultTable != null) 61 | return defaultTable; 62 | 63 | UInt32[] createTable = new UInt32[256]; 64 | for (int i = 0; i < 256; i++) 65 | { 66 | UInt32 entry = (UInt32)i; 67 | for (int j = 0; j < 8; j++) 68 | if ((entry & 1) == 1) 69 | entry = (entry >> 1) ^ polynomial; 70 | else 71 | entry = entry >> 1; 72 | createTable[i] = entry; 73 | } 74 | 75 | if (polynomial == DefaultPolynomial) 76 | defaultTable = createTable; 77 | 78 | return createTable; 79 | } 80 | 81 | private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size) { 82 | UInt32 crc = seed; 83 | for (int i = start; i < size; i++) 84 | unchecked 85 | { 86 | crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff]; 87 | } 88 | return crc; 89 | } 90 | 91 | private byte[] UInt32ToBigEndianBytes(UInt32 x) { 92 | return new byte[] { 93 | (byte)((x >> 24) & 0xff), 94 | (byte)((x >> 16) & 0xff), 95 | (byte)((x >> 8) & 0xff), 96 | (byte)(x & 0xff) 97 | }; 98 | } 99 | 100 | public string Get(string FilePath) { 101 | Crc32 crc32 = new Crc32(); 102 | String hash = String.Empty; 103 | 104 | using (FileStream fs = File.Open(FilePath, FileMode.Open)) 105 | foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower(); 106 | 107 | return hash; 108 | } 109 | } 110 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Crc32/Crc32.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5b306395bd914c1ea1be614cf0fffbe6 3 | timeCreated: 1709901203 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/DES.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7331c9adf98140a99fa8d992ae7f851c 3 | timeCreated: 1709891836 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/DES/DESEncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.DES 5 | { 6 | [System.Serializable] 7 | public class DESEncryptionOptions : IProviderOptions 8 | { 9 | public string cryptoKey = "ABCDEFGH"; 10 | public Encoding encoding = Encoding.UTF8; 11 | } 12 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/DES/DESEncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7d4e5d2be20b40d284b39199847a26f0 3 | timeCreated: 1709891855 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/DES/DESProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Security.Cryptography; 4 | using DevsDaddy.Shared.CryptoLibrary.Core; 5 | 6 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.DES 7 | { 8 | /// 9 | /// DES Encryption Provider. 10 | /// Using for encrypt/decrypt plain text or byte arrays 11 | /// 12 | public class DESProvider : ICryptoProvider 13 | { 14 | // Encryption Options 15 | private DESEncryptionOptions _options; 16 | 17 | /// 18 | /// DES Encryption Provider 19 | /// 20 | /// 21 | public DESProvider(DESEncryptionOptions options = null) { 22 | _options = options ?? new DESEncryptionOptions(); 23 | } 24 | 25 | /// 26 | /// Decrypt encrypted string to plain text using DES 27 | /// 28 | /// 29 | /// 30 | public string DecryptString(string encryptedString) { 31 | byte[] txtByteData = Convert.FromBase64String(encryptedString); 32 | byte[] decoded = DecryptBinary(txtByteData); 33 | return _options.encoding.GetString(decoded); 34 | } 35 | 36 | /// 37 | /// Decrypt encrypted byte array to decrypted byte array using DES 38 | /// 39 | /// 40 | /// 41 | public byte[] DecryptBinary(byte[] encryptedBinary) { 42 | byte[] keyByteData = _options.encoding.GetBytes(_options.cryptoKey); 43 | 44 | DESCryptoServiceProvider DEScryptoProvider = new DESCryptoServiceProvider(); 45 | ICryptoTransform trnsfrm = DEScryptoProvider.CreateDecryptor(keyByteData, keyByteData); 46 | CryptoStreamMode mode = CryptoStreamMode.Write; 47 | 48 | //Set up Stream & Write Encript data 49 | MemoryStream mStream = new MemoryStream(); 50 | CryptoStream cStream = new CryptoStream(mStream, trnsfrm, mode); 51 | cStream.Write(encryptedBinary, 0, encryptedBinary.Length); 52 | cStream.FlushFinalBlock(); 53 | 54 | //Read Ecncrypted Data From Memory Stream 55 | byte[] result = new byte[mStream.Length]; 56 | mStream.Position = 0; 57 | mStream.Read(result, 0, result.Length); 58 | 59 | return result; 60 | } 61 | 62 | /// 63 | /// Encrypt plain-text using DES 64 | /// 65 | /// 66 | /// 67 | public string EncryptString(string decryptedString) { 68 | byte[] txtByteData = _options.encoding.GetBytes(decryptedString); 69 | byte[] encoded = EncryptBinary(txtByteData); 70 | return Convert.ToBase64String(encoded); 71 | } 72 | 73 | /// 74 | /// Encrypt byte-array using DES 75 | /// 76 | /// 77 | /// 78 | public byte[] EncryptBinary(byte[] decryptedBinary) { 79 | byte[] keyByteData = _options.encoding.GetBytes(_options.cryptoKey); 80 | DESCryptoServiceProvider DEScryptoProvider = new DESCryptoServiceProvider(); 81 | ICryptoTransform trnsfrm = DEScryptoProvider.CreateEncryptor(keyByteData, keyByteData); 82 | CryptoStreamMode mode = CryptoStreamMode.Write; 83 | 84 | //Set up Stream & Write Encript data 85 | MemoryStream mStream = new MemoryStream(); 86 | CryptoStream cStream = new CryptoStream(mStream, trnsfrm, mode); 87 | cStream.Write(decryptedBinary, 0, decryptedBinary.Length); 88 | cStream.FlushFinalBlock(); 89 | 90 | //Read Ecncrypted Data From Memory Stream 91 | byte[] result = new byte[mStream.Length]; 92 | mStream.Position = 0; 93 | mStream.Read(result, 0, result.Length); 94 | 95 | return result; 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/DES/DESProvider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f97ecf2e80fd4695be93a4faf86a6b0f 3 | timeCreated: 1709891843 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/MD5.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 709b5e3fc5ab4a628f158df4ea242698 3 | timeCreated: 1709889633 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/MD5/MD5EncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.MD5 5 | { 6 | [System.Serializable] 7 | public class MD5EncryptionOptions : IProviderOptions 8 | { 9 | public Encoding encoding = Encoding.UTF8; 10 | } 11 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/MD5/MD5EncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8de0da8afd9b4da5b2d8c8bd995ce70f 3 | timeCreated: 1709894165 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/MD5/MD5Provider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using DevsDaddy.Shared.CryptoLibrary.Core; 4 | 5 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.MD5 6 | { 7 | /// 8 | /// MD5 Hash Provider (No Decryption, Only Encryption) 9 | /// 10 | public class MD5Provider : ICryptoProvider 11 | { 12 | // Provider Options 13 | private MD5EncryptionOptions _options; 14 | 15 | /// 16 | /// MD5 Hashing Provider 17 | /// 18 | /// 19 | public MD5Provider(MD5EncryptionOptions options = null) { 20 | _options = options ?? new MD5EncryptionOptions(); 21 | } 22 | 23 | /// 24 | /// Decrypt String (Not Supported for hashing) 25 | /// 26 | /// 27 | /// 28 | /// 29 | public string DecryptString(string encryptedString) { 30 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 31 | } 32 | 33 | /// 34 | /// Decrypt Binary (Not Supported for hashing) 35 | /// 36 | /// 37 | /// 38 | /// 39 | public byte[] DecryptBinary(byte[] encryptedBinary) { 40 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 41 | } 42 | 43 | /// 44 | /// Encrypt plain-text and returns MD5 hash 45 | /// 46 | /// 47 | /// 48 | public string EncryptString(string decryptedString) { 49 | using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) 50 | { 51 | byte[] inputBytes = _options.encoding.GetBytes(decryptedString); 52 | byte[] hashBytes = md5.ComputeHash(inputBytes); 53 | 54 | // Convert the byte array to hexadecimal string 55 | StringBuilder sb = new StringBuilder(); 56 | for (int i = 0; i < hashBytes.Length; i++) 57 | { 58 | sb.Append(hashBytes[i].ToString("X2")); 59 | } 60 | return sb.ToString(); 61 | } 62 | } 63 | 64 | /// 65 | /// Encrypt byte array and return MD5 hashed byte array 66 | /// 67 | /// 68 | /// 69 | public byte[] EncryptBinary(byte[] decryptedBinary) { 70 | using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) 71 | { 72 | byte[] hashBytes = md5.ComputeHash(decryptedBinary); 73 | return hashBytes; 74 | } 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/MD5/MD5Provider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ee97b4162c6c4e1cb68e88ab196c2028 3 | timeCreated: 1709894172 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/PBKDF2.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 34c2bd6fc94d4b1c9f8d55f1e3ea6f93 3 | timeCreated: 1709898865 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/PBKDF2/PBKDF2EncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.PBKDF2 5 | { 6 | [System.Serializable] 7 | public class PBKDF2EncryptionOptions : IProviderOptions 8 | { 9 | public bool autoRegenerateSalt = true; 10 | public Encoding encoding = Encoding.UTF8; 11 | public int hashIterations = 100000; 12 | public int saltSize = 34; 13 | } 14 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/PBKDF2/PBKDF2EncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5913aff3da074d73bf59d501328a333e 3 | timeCreated: 1709898878 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/PBKDF2/PBKDF2Provider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Security.Cryptography; 3 | using DevsDaddy.Shared.CryptoLibrary.Core; 4 | 5 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.PBKDF2 6 | { 7 | /// 8 | /// PBKDF2 Hashing Provider 9 | /// 10 | public class PBKDF2Provider : ICryptoProvider 11 | { 12 | // Provider Options 13 | private PBKDF2EncryptionOptions _options; 14 | private string currentSalt; 15 | 16 | /// 17 | /// PBKDF2 Provider 18 | /// 19 | /// 20 | public PBKDF2Provider(PBKDF2EncryptionOptions options = null) { 21 | _options = options ?? new PBKDF2EncryptionOptions(); 22 | } 23 | 24 | /// 25 | /// Decrypt String (Not Supported for hashing) 26 | /// 27 | /// 28 | /// 29 | /// 30 | public string DecryptString(string encryptedString) { 31 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 32 | } 33 | 34 | /// 35 | /// Decrypt Binary (Not Supported for hashing) 36 | /// 37 | /// 38 | /// 39 | /// 40 | public byte[] DecryptBinary(byte[] encryptedBinary) { 41 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 42 | } 43 | 44 | /// 45 | /// Get PBKDF2 Hash from plain-text 46 | /// 47 | /// 48 | /// 49 | public string EncryptString(string decryptedString) { 50 | if (string.IsNullOrEmpty(currentSalt) || _options.autoRegenerateSalt) GenerateSalt(); 51 | return calculateHash(_options.hashIterations, decryptedString); 52 | } 53 | 54 | /// 55 | /// Get PBKDF2 Hash from byte array 56 | /// 57 | /// 58 | /// 59 | public byte[] EncryptBinary(byte[] decryptedBinary) { 60 | if (string.IsNullOrEmpty(currentSalt) || _options.autoRegenerateSalt) GenerateSalt(); 61 | return calculateHash(_options.hashIterations, decryptedBinary); 62 | } 63 | 64 | /// 65 | /// Generate Hash Salt 66 | /// 67 | /// 68 | /// 69 | public string GenerateSalt() 70 | { 71 | if (_options.saltSize < 1) 72 | throw new InvalidOperationException(string.Format("Cannot generate a salt of size {0}, use a value greater than 1, recommended: 16", _options.saltSize)); 73 | 74 | var rand = RandomNumberGenerator.Create(); 75 | var ret = new byte[_options.saltSize]; 76 | rand.GetBytes(ret); 77 | 78 | currentSalt = string.Format("{0}.{1}", _options.hashIterations, Convert.ToBase64String(ret)); 79 | return currentSalt; 80 | } 81 | 82 | /// 83 | /// Calculate Text Hash 84 | /// 85 | /// 86 | /// 87 | /// 88 | private string calculateHash(int iteration, string text) 89 | { 90 | byte[] saltBytes = _options.encoding.GetBytes(currentSalt); 91 | var pbkdf2 = new Rfc2898DeriveBytes(text, saltBytes, iteration); 92 | var key = pbkdf2.GetBytes(64); 93 | return Convert.ToBase64String(key); 94 | } 95 | 96 | /// 97 | /// Calculate Binary Hash 98 | /// 99 | /// 100 | /// 101 | /// 102 | private byte[] calculateHash(int iteration, byte[] data) { 103 | byte[] saltBytes = _options.encoding.GetBytes(currentSalt); 104 | var pbkdf2 = new Rfc2898DeriveBytes(data, saltBytes, iteration); 105 | var key = pbkdf2.GetBytes(64); 106 | return key; 107 | } 108 | 109 | /// 110 | /// Expand Salt 111 | /// 112 | /// 113 | private void expandSalt() 114 | { 115 | try 116 | { 117 | var i = currentSalt.IndexOf('.'); 118 | _options.hashIterations = int.Parse(currentSalt.Substring(0, i), System.Globalization.NumberStyles.Number); 119 | } 120 | catch (Exception) 121 | { 122 | throw new FormatException("The salt was not in an expected format of {int}.{string}"); 123 | } 124 | } 125 | } 126 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/PBKDF2/PBKDF2Provider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b5d0e6204e3048ee9720ecfc78f8e958 3 | timeCreated: 1709898883 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RIPEMD-160.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9592fbdc5acf4a61b4e6717e1596b69b 3 | timeCreated: 1709897338 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RIPEMD-160/RIPEMD160.cs: -------------------------------------------------------------------------------- 1 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.RIPEMD_160 2 | { 3 | public abstract class RIPEMD160 : System.Security.Cryptography.HashAlgorithm 4 | { 5 | public RIPEMD160() { } 6 | 7 | public new static RIPEMD160 Create() 8 | { 9 | return new RIPEMD160Managed(); 10 | } 11 | 12 | public new static RIPEMD160 Create(string hashname) 13 | { 14 | return new RIPEMD160Managed(); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RIPEMD-160/RIPEMD160.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c31d9ebcea784ab18e1443e8656abe00 3 | timeCreated: 1709898451 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RIPEMD-160/RIPEMD160Managed.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.RIPEMD_160 6 | { 7 | public class RIPEMD160Managed : RIPEMD160 8 | { 9 | static public UInt32 ReadUInt32(byte[] buffer, long offset) 10 | { 11 | return 12 | (Convert.ToUInt32(buffer[3 + offset]) << 24) | 13 | (Convert.ToUInt32(buffer[2 + offset]) << 16) | 14 | (Convert.ToUInt32(buffer[1 + offset]) << 8) | 15 | (Convert.ToUInt32(buffer[0 + offset])); 16 | } 17 | 18 | static UInt32 RotateLeft(UInt32 value, int bits) 19 | { 20 | return (value << bits) | (value >> (32 - bits)); 21 | } 22 | 23 | static UInt32 F(UInt32 x, UInt32 y, UInt32 z) 24 | { 25 | return x ^ y ^ z; 26 | } 27 | 28 | static UInt32 G(UInt32 x, UInt32 y, UInt32 z) 29 | { 30 | return (x & y) | (~x & z); 31 | } 32 | 33 | static UInt32 H(UInt32 x, UInt32 y, UInt32 z) 34 | { 35 | return (x | ~y) ^ z; 36 | } 37 | 38 | static UInt32 I(UInt32 x, UInt32 y, UInt32 z) 39 | { 40 | return (x & z) | (y & ~z); 41 | } 42 | 43 | static UInt32 J(UInt32 x, UInt32 y, UInt32 z) 44 | { 45 | return x ^ (y | ~z); 46 | } 47 | 48 | static void FF(ref UInt32 a, UInt32 b, ref UInt32 c, UInt32 d, UInt32 e, UInt32 x, int s) 49 | { 50 | a += F(b, c, d) + x; 51 | a = RotateLeft(a, s) + e; 52 | c = RotateLeft(c, 10); 53 | } 54 | 55 | static void GG(ref UInt32 a, UInt32 b, ref UInt32 c, UInt32 d, UInt32 e, UInt32 x, int s) 56 | { 57 | a += G(b, c, d) + x + (UInt32)0x5a827999; 58 | a = RotateLeft(a, s) + e; 59 | c = RotateLeft(c, 10); 60 | } 61 | 62 | static void HH(ref UInt32 a, UInt32 b, ref UInt32 c, UInt32 d, UInt32 e, UInt32 x, int s) 63 | { 64 | a += H(b, c, d) + x + (UInt32)0x6ed9eba1; 65 | a = RotateLeft(a, s) + e; 66 | c = RotateLeft(c, 10); 67 | } 68 | 69 | static void II(ref UInt32 a, UInt32 b, ref UInt32 c, UInt32 d, UInt32 e, UInt32 x, int s) 70 | { 71 | a += I(b, c, d) + x + (UInt32)0x8f1bbcdc; 72 | a = RotateLeft(a, s) + e; 73 | c = RotateLeft(c, 10); 74 | } 75 | 76 | static void JJ(ref UInt32 a, UInt32 b, ref UInt32 c, UInt32 d, UInt32 e, UInt32 x, int s) 77 | { 78 | a += J(b, c, d) + x + (UInt32)0xa953fd4e; 79 | a = RotateLeft(a, s) + e; 80 | c = RotateLeft(c, 10); 81 | } 82 | 83 | static void FFF(ref UInt32 a, UInt32 b, ref UInt32 c, UInt32 d, UInt32 e, UInt32 x, int s) 84 | { 85 | a += F(b, c, d) + x; 86 | a = RotateLeft(a, s) + e; 87 | c = RotateLeft(c, 10); 88 | } 89 | 90 | static void GGG(ref UInt32 a, UInt32 b, ref UInt32 c, UInt32 d, UInt32 e, UInt32 x, int s) 91 | { 92 | a += G(b, c, d) + x + (UInt32)0x7a6d76e9; 93 | a = RotateLeft(a, s) + e; 94 | c = RotateLeft(c, 10); 95 | } 96 | 97 | static void HHH(ref UInt32 a, UInt32 b, ref UInt32 c, UInt32 d, UInt32 e, UInt32 x, int s) 98 | { 99 | a += H(b, c, d) + x + (UInt32)0x6d703ef3; 100 | a = RotateLeft(a, s) + e; 101 | c = RotateLeft(c, 10); 102 | } 103 | 104 | static void III(ref UInt32 a, UInt32 b, ref UInt32 c, UInt32 d, UInt32 e, UInt32 x, int s) 105 | { 106 | a += I(b, c, d) + x + (UInt32)0x5c4dd124; 107 | a = RotateLeft(a, s) + e; 108 | c = RotateLeft(c, 10); 109 | } 110 | 111 | static void JJJ(ref UInt32 a, UInt32 b, ref UInt32 c, UInt32 d, UInt32 e, UInt32 x, int s) 112 | { 113 | a += J(b, c, d) + x + (UInt32)0x50a28be6; 114 | a = RotateLeft(a, s) + e; 115 | c = RotateLeft(c, 10); 116 | } 117 | 118 | static public void MDinit(ref UInt32[] MDbuf) 119 | { 120 | MDbuf[0] = (UInt32)0x67452301; 121 | MDbuf[1] = (UInt32)0xefcdab89; 122 | MDbuf[2] = (UInt32)0x98badcfe; 123 | MDbuf[3] = (UInt32)0x10325476; 124 | MDbuf[4] = (UInt32)0xc3d2e1f0; 125 | } 126 | 127 | static public void compress(ref UInt32[] MDbuf, UInt32[] X) 128 | { 129 | UInt32 aa = MDbuf[0]; 130 | UInt32 bb = MDbuf[1]; 131 | UInt32 cc = MDbuf[2]; 132 | UInt32 dd = MDbuf[3]; 133 | UInt32 ee = MDbuf[4]; 134 | UInt32 aaa = MDbuf[0]; 135 | UInt32 bbb = MDbuf[1]; 136 | UInt32 ccc = MDbuf[2]; 137 | UInt32 ddd = MDbuf[3]; 138 | UInt32 eee = MDbuf[4]; 139 | 140 | /* round 1 */ 141 | FF(ref aa, bb, ref cc, dd, ee, X[0], 11); 142 | FF(ref ee, aa, ref bb, cc, dd, X[1], 14); 143 | FF(ref dd, ee, ref aa, bb, cc, X[2], 15); 144 | FF(ref cc, dd, ref ee, aa, bb, X[3], 12); 145 | FF(ref bb, cc, ref dd, ee, aa, X[4], 5); 146 | FF(ref aa, bb, ref cc, dd, ee, X[5], 8); 147 | FF(ref ee, aa, ref bb, cc, dd, X[6], 7); 148 | FF(ref dd, ee, ref aa, bb, cc, X[7], 9); 149 | FF(ref cc, dd, ref ee, aa, bb, X[8], 11); 150 | FF(ref bb, cc, ref dd, ee, aa, X[9], 13); 151 | FF(ref aa, bb, ref cc, dd, ee, X[10], 14); 152 | FF(ref ee, aa, ref bb, cc, dd, X[11], 15); 153 | FF(ref dd, ee, ref aa, bb, cc, X[12], 6); 154 | FF(ref cc, dd, ref ee, aa, bb, X[13], 7); 155 | FF(ref bb, cc, ref dd, ee, aa, X[14], 9); 156 | FF(ref aa, bb, ref cc, dd, ee, X[15], 8); 157 | 158 | /* round 2 */ 159 | GG(ref ee, aa, ref bb, cc, dd, X[7], 7); 160 | GG(ref dd, ee, ref aa, bb, cc, X[4], 6); 161 | GG(ref cc, dd, ref ee, aa, bb, X[13], 8); 162 | GG(ref bb, cc, ref dd, ee, aa, X[1], 13); 163 | GG(ref aa, bb, ref cc, dd, ee, X[10], 11); 164 | GG(ref ee, aa, ref bb, cc, dd, X[6], 9); 165 | GG(ref dd, ee, ref aa, bb, cc, X[15], 7); 166 | GG(ref cc, dd, ref ee, aa, bb, X[3], 15); 167 | GG(ref bb, cc, ref dd, ee, aa, X[12], 7); 168 | GG(ref aa, bb, ref cc, dd, ee, X[0], 12); 169 | GG(ref ee, aa, ref bb, cc, dd, X[9], 15); 170 | GG(ref dd, ee, ref aa, bb, cc, X[5], 9); 171 | GG(ref cc, dd, ref ee, aa, bb, X[2], 11); 172 | GG(ref bb, cc, ref dd, ee, aa, X[14], 7); 173 | GG(ref aa, bb, ref cc, dd, ee, X[11], 13); 174 | GG(ref ee, aa, ref bb, cc, dd, X[8], 12); 175 | 176 | /* round 3 */ 177 | HH(ref dd, ee, ref aa, bb, cc, X[3], 11); 178 | HH(ref cc, dd, ref ee, aa, bb, X[10], 13); 179 | HH(ref bb, cc, ref dd, ee, aa, X[14], 6); 180 | HH(ref aa, bb, ref cc, dd, ee, X[4], 7); 181 | HH(ref ee, aa, ref bb, cc, dd, X[9], 14); 182 | HH(ref dd, ee, ref aa, bb, cc, X[15], 9); 183 | HH(ref cc, dd, ref ee, aa, bb, X[8], 13); 184 | HH(ref bb, cc, ref dd, ee, aa, X[1], 15); 185 | HH(ref aa, bb, ref cc, dd, ee, X[2], 14); 186 | HH(ref ee, aa, ref bb, cc, dd, X[7], 8); 187 | HH(ref dd, ee, ref aa, bb, cc, X[0], 13); 188 | HH(ref cc, dd, ref ee, aa, bb, X[6], 6); 189 | HH(ref bb, cc, ref dd, ee, aa, X[13], 5); 190 | HH(ref aa, bb, ref cc, dd, ee, X[11], 12); 191 | HH(ref ee, aa, ref bb, cc, dd, X[5], 7); 192 | HH(ref dd, ee, ref aa, bb, cc, X[12], 5); 193 | 194 | /* round 4 */ 195 | II(ref cc, dd, ref ee, aa, bb, X[1], 11); 196 | II(ref bb, cc, ref dd, ee, aa, X[9], 12); 197 | II(ref aa, bb, ref cc, dd, ee, X[11], 14); 198 | II(ref ee, aa, ref bb, cc, dd, X[10], 15); 199 | II(ref dd, ee, ref aa, bb, cc, X[0], 14); 200 | II(ref cc, dd, ref ee, aa, bb, X[8], 15); 201 | II(ref bb, cc, ref dd, ee, aa, X[12], 9); 202 | II(ref aa, bb, ref cc, dd, ee, X[4], 8); 203 | II(ref ee, aa, ref bb, cc, dd, X[13], 9); 204 | II(ref dd, ee, ref aa, bb, cc, X[3], 14); 205 | II(ref cc, dd, ref ee, aa, bb, X[7], 5); 206 | II(ref bb, cc, ref dd, ee, aa, X[15], 6); 207 | II(ref aa, bb, ref cc, dd, ee, X[14], 8); 208 | II(ref ee, aa, ref bb, cc, dd, X[5], 6); 209 | II(ref dd, ee, ref aa, bb, cc, X[6], 5); 210 | II(ref cc, dd, ref ee, aa, bb, X[2], 12); 211 | 212 | /* round 5 */ 213 | JJ(ref bb, cc, ref dd, ee, aa, X[4], 9); 214 | JJ(ref aa, bb, ref cc, dd, ee, X[0], 15); 215 | JJ(ref ee, aa, ref bb, cc, dd, X[5], 5); 216 | JJ(ref dd, ee, ref aa, bb, cc, X[9], 11); 217 | JJ(ref cc, dd, ref ee, aa, bb, X[7], 6); 218 | JJ(ref bb, cc, ref dd, ee, aa, X[12], 8); 219 | JJ(ref aa, bb, ref cc, dd, ee, X[2], 13); 220 | JJ(ref ee, aa, ref bb, cc, dd, X[10], 12); 221 | JJ(ref dd, ee, ref aa, bb, cc, X[14], 5); 222 | JJ(ref cc, dd, ref ee, aa, bb, X[1], 12); 223 | JJ(ref bb, cc, ref dd, ee, aa, X[3], 13); 224 | JJ(ref aa, bb, ref cc, dd, ee, X[8], 14); 225 | JJ(ref ee, aa, ref bb, cc, dd, X[11], 11); 226 | JJ(ref dd, ee, ref aa, bb, cc, X[6], 8); 227 | JJ(ref cc, dd, ref ee, aa, bb, X[15], 5); 228 | JJ(ref bb, cc, ref dd, ee, aa, X[13], 6); 229 | 230 | /* parallel round 1 */ 231 | JJJ(ref aaa, bbb, ref ccc, ddd, eee, X[5], 8); 232 | JJJ(ref eee, aaa, ref bbb, ccc, ddd, X[14], 9); 233 | JJJ(ref ddd, eee, ref aaa, bbb, ccc, X[7], 9); 234 | JJJ(ref ccc, ddd, ref eee, aaa, bbb, X[0], 11); 235 | JJJ(ref bbb, ccc, ref ddd, eee, aaa, X[9], 13); 236 | JJJ(ref aaa, bbb, ref ccc, ddd, eee, X[2], 15); 237 | JJJ(ref eee, aaa, ref bbb, ccc, ddd, X[11], 15); 238 | JJJ(ref ddd, eee, ref aaa, bbb, ccc, X[4], 5); 239 | JJJ(ref ccc, ddd, ref eee, aaa, bbb, X[13], 7); 240 | JJJ(ref bbb, ccc, ref ddd, eee, aaa, X[6], 7); 241 | JJJ(ref aaa, bbb, ref ccc, ddd, eee, X[15], 8); 242 | JJJ(ref eee, aaa, ref bbb, ccc, ddd, X[8], 11); 243 | JJJ(ref ddd, eee, ref aaa, bbb, ccc, X[1], 14); 244 | JJJ(ref ccc, ddd, ref eee, aaa, bbb, X[10], 14); 245 | JJJ(ref bbb, ccc, ref ddd, eee, aaa, X[3], 12); 246 | JJJ(ref aaa, bbb, ref ccc, ddd, eee, X[12], 6); 247 | 248 | /* parallel round 2 */ 249 | III(ref eee, aaa, ref bbb, ccc, ddd, X[6], 9); 250 | III(ref ddd, eee, ref aaa, bbb, ccc, X[11], 13); 251 | III(ref ccc, ddd, ref eee, aaa, bbb, X[3], 15); 252 | III(ref bbb, ccc, ref ddd, eee, aaa, X[7], 7); 253 | III(ref aaa, bbb, ref ccc, ddd, eee, X[0], 12); 254 | III(ref eee, aaa, ref bbb, ccc, ddd, X[13], 8); 255 | III(ref ddd, eee, ref aaa, bbb, ccc, X[5], 9); 256 | III(ref ccc, ddd, ref eee, aaa, bbb, X[10], 11); 257 | III(ref bbb, ccc, ref ddd, eee, aaa, X[14], 7); 258 | III(ref aaa, bbb, ref ccc, ddd, eee, X[15], 7); 259 | III(ref eee, aaa, ref bbb, ccc, ddd, X[8], 12); 260 | III(ref ddd, eee, ref aaa, bbb, ccc, X[12], 7); 261 | III(ref ccc, ddd, ref eee, aaa, bbb, X[4], 6); 262 | III(ref bbb, ccc, ref ddd, eee, aaa, X[9], 15); 263 | III(ref aaa, bbb, ref ccc, ddd, eee, X[1], 13); 264 | III(ref eee, aaa, ref bbb, ccc, ddd, X[2], 11); 265 | 266 | /* parallel round 3 */ 267 | HHH(ref ddd, eee, ref aaa, bbb, ccc, X[15], 9); 268 | HHH(ref ccc, ddd, ref eee, aaa, bbb, X[5], 7); 269 | HHH(ref bbb, ccc, ref ddd, eee, aaa, X[1], 15); 270 | HHH(ref aaa, bbb, ref ccc, ddd, eee, X[3], 11); 271 | HHH(ref eee, aaa, ref bbb, ccc, ddd, X[7], 8); 272 | HHH(ref ddd, eee, ref aaa, bbb, ccc, X[14], 6); 273 | HHH(ref ccc, ddd, ref eee, aaa, bbb, X[6], 6); 274 | HHH(ref bbb, ccc, ref ddd, eee, aaa, X[9], 14); 275 | HHH(ref aaa, bbb, ref ccc, ddd, eee, X[11], 12); 276 | HHH(ref eee, aaa, ref bbb, ccc, ddd, X[8], 13); 277 | HHH(ref ddd, eee, ref aaa, bbb, ccc, X[12], 5); 278 | HHH(ref ccc, ddd, ref eee, aaa, bbb, X[2], 14); 279 | HHH(ref bbb, ccc, ref ddd, eee, aaa, X[10], 13); 280 | HHH(ref aaa, bbb, ref ccc, ddd, eee, X[0], 13); 281 | HHH(ref eee, aaa, ref bbb, ccc, ddd, X[4], 7); 282 | HHH(ref ddd, eee, ref aaa, bbb, ccc, X[13], 5); 283 | 284 | /* parallel round 4 */ 285 | GGG(ref ccc, ddd, ref eee, aaa, bbb, X[8], 15); 286 | GGG(ref bbb, ccc, ref ddd, eee, aaa, X[6], 5); 287 | GGG(ref aaa, bbb, ref ccc, ddd, eee, X[4], 8); 288 | GGG(ref eee, aaa, ref bbb, ccc, ddd, X[1], 11); 289 | GGG(ref ddd, eee, ref aaa, bbb, ccc, X[3], 14); 290 | GGG(ref ccc, ddd, ref eee, aaa, bbb, X[11], 14); 291 | GGG(ref bbb, ccc, ref ddd, eee, aaa, X[15], 6); 292 | GGG(ref aaa, bbb, ref ccc, ddd, eee, X[0], 14); 293 | GGG(ref eee, aaa, ref bbb, ccc, ddd, X[5], 6); 294 | GGG(ref ddd, eee, ref aaa, bbb, ccc, X[12], 9); 295 | GGG(ref ccc, ddd, ref eee, aaa, bbb, X[2], 12); 296 | GGG(ref bbb, ccc, ref ddd, eee, aaa, X[13], 9); 297 | GGG(ref aaa, bbb, ref ccc, ddd, eee, X[9], 12); 298 | GGG(ref eee, aaa, ref bbb, ccc, ddd, X[7], 5); 299 | GGG(ref ddd, eee, ref aaa, bbb, ccc, X[10], 15); 300 | GGG(ref ccc, ddd, ref eee, aaa, bbb, X[14], 8); 301 | 302 | /* parallel round 5 */ 303 | FFF(ref bbb, ccc, ref ddd, eee, aaa, X[12], 8); 304 | FFF(ref aaa, bbb, ref ccc, ddd, eee, X[15], 5); 305 | FFF(ref eee, aaa, ref bbb, ccc, ddd, X[10], 12); 306 | FFF(ref ddd, eee, ref aaa, bbb, ccc, X[4], 9); 307 | FFF(ref ccc, ddd, ref eee, aaa, bbb, X[1], 12); 308 | FFF(ref bbb, ccc, ref ddd, eee, aaa, X[5], 5); 309 | FFF(ref aaa, bbb, ref ccc, ddd, eee, X[8], 14); 310 | FFF(ref eee, aaa, ref bbb, ccc, ddd, X[7], 6); 311 | FFF(ref ddd, eee, ref aaa, bbb, ccc, X[6], 8); 312 | FFF(ref ccc, ddd, ref eee, aaa, bbb, X[2], 13); 313 | FFF(ref bbb, ccc, ref ddd, eee, aaa, X[13], 6); 314 | FFF(ref aaa, bbb, ref ccc, ddd, eee, X[14], 5); 315 | FFF(ref eee, aaa, ref bbb, ccc, ddd, X[0], 15); 316 | FFF(ref ddd, eee, ref aaa, bbb, ccc, X[3], 13); 317 | FFF(ref ccc, ddd, ref eee, aaa, bbb, X[9], 11); 318 | FFF(ref bbb, ccc, ref ddd, eee, aaa, X[11], 11); 319 | 320 | // combine results */ 321 | ddd += cc + MDbuf[1]; /* final result for MDbuf[0] */ 322 | MDbuf[1] = MDbuf[2] + dd + eee; 323 | MDbuf[2] = MDbuf[3] + ee + aaa; 324 | MDbuf[3] = MDbuf[4] + aa + bbb; 325 | MDbuf[4] = MDbuf[0] + bb + ccc; 326 | MDbuf[0] = ddd; 327 | } 328 | 329 | static public void MDfinish(ref UInt32[] MDbuf, byte[] strptr, long index, UInt32 lswlen, UInt32 mswlen) 330 | { 331 | //UInt32 i; /* counter */ 332 | var X = Enumerable.Repeat((UInt32)0, 16).ToArray(); /* message words */ 333 | 334 | 335 | /* put bytes from strptr into X */ 336 | for (var i = 0; i < (lswlen & 63); i++) 337 | { 338 | /* byte i goes into word X[i div 4] at pos. 8*(i mod 4) */ 339 | X[i >> 2] ^= Convert.ToUInt32(strptr[i + index]) << (8 * (i & 3)); 340 | } 341 | 342 | /* append the bit m_n == 1 */ 343 | X[(lswlen >> 2) & 15] ^= (UInt32)1 << Convert.ToInt32(8 * (lswlen & 3) + 7); 344 | 345 | if ((lswlen & 63) > 55) 346 | { 347 | /* length goes to next block */ 348 | compress(ref MDbuf, X); 349 | X = Enumerable.Repeat((UInt32)0, 16).ToArray(); 350 | } 351 | 352 | /* append length in bits*/ 353 | X[14] = lswlen << 3; 354 | X[15] = (lswlen >> 29) | (mswlen << 3); 355 | compress(ref MDbuf, X); 356 | } 357 | 358 | static int RMDsize = 160; 359 | UInt32 [] MDbuf = new UInt32 [RMDsize / 32]; 360 | UInt32 [] X = new UInt32[16]; 361 | byte [] UnhashedBuffer = new byte[64]; 362 | int UnhashedBufferLength = 0; 363 | long HashedLength = 0; 364 | 365 | public RIPEMD160Managed() 366 | { 367 | Initialize(); 368 | } 369 | 370 | protected override void HashCore (byte[] array, int ibStart, int cbSize) 371 | { 372 | var index = 0; 373 | while(index < cbSize) 374 | { 375 | var bytesRemaining = cbSize - index; 376 | if(UnhashedBufferLength > 0) 377 | { 378 | if((bytesRemaining + UnhashedBufferLength) >= (UnhashedBuffer.Length)) 379 | { 380 | Array.Copy(array, ibStart + index, UnhashedBuffer, UnhashedBufferLength, (UnhashedBuffer.Length) - UnhashedBufferLength); 381 | index += (UnhashedBuffer.Length) - UnhashedBufferLength; 382 | UnhashedBufferLength = UnhashedBuffer.Length; 383 | 384 | for (var i = 0; i < 16; i++) 385 | X[i] = ReadUInt32(UnhashedBuffer, i * 4); 386 | 387 | compress(ref MDbuf, X); 388 | UnhashedBufferLength = 0; 389 | } 390 | else 391 | { 392 | Array.Copy(array, ibStart + index, UnhashedBuffer, UnhashedBufferLength, bytesRemaining); 393 | UnhashedBufferLength += bytesRemaining; 394 | index += bytesRemaining; 395 | } 396 | } 397 | else 398 | { 399 | if(bytesRemaining >= (UnhashedBuffer.Length)) 400 | { 401 | for (var i = 0; i < 16; i++) 402 | X[i] = ReadUInt32(array, index + (i * 4)); 403 | index += UnhashedBuffer.Length; 404 | 405 | compress(ref MDbuf, X); 406 | } 407 | else 408 | { 409 | Array.Copy(array, ibStart + index, UnhashedBuffer, 0, bytesRemaining); 410 | UnhashedBufferLength = bytesRemaining; 411 | index += bytesRemaining; 412 | } 413 | } 414 | } 415 | 416 | HashedLength += cbSize; 417 | } 418 | 419 | protected override byte[] HashFinal () 420 | { 421 | MDfinish(ref MDbuf, UnhashedBuffer, 0, Convert.ToUInt32(HashedLength), 0); 422 | 423 | var result = new byte [RMDsize / 8]; 424 | 425 | for (var i = 0; i < RMDsize / 8; i += 4) 426 | { 427 | result[i] = Convert.ToByte(MDbuf[i >> 2] & 0xFF); /* implicit cast to byte */ 428 | result[i + 1] = Convert.ToByte((MDbuf[i >> 2] >> 8) & 0xFF); /* extracts the 8 least */ 429 | result[i + 2] = Convert.ToByte((MDbuf[i >> 2] >> 16) & 0xFF); /* significant bits. */ 430 | result[i + 3] = Convert.ToByte((MDbuf[i >> 2] >> 24) & 0xFF); 431 | } 432 | 433 | return result; 434 | } 435 | 436 | public override void Initialize () 437 | { 438 | MDinit(ref MDbuf); 439 | X = Enumerable.Repeat((UInt32)0, 16).ToArray(); 440 | HashedLength = 0; 441 | UnhashedBufferLength = 0; 442 | } 443 | } 444 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RIPEMD-160/RIPEMD160Managed.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 26bcc4f1c6c841829281644666d97924 3 | timeCreated: 1709898527 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RIPEMD-160/RipemdEncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.RIPEMD_160 5 | { 6 | [System.Serializable] 7 | public class RipemdEncryptionOptions : IProviderOptions 8 | { 9 | public Encoding encoding = Encoding.UTF8; 10 | } 11 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RIPEMD-160/RipemdEncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 74488339489a42cebc569ec7b0afb0ef 3 | timeCreated: 1709897381 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RIPEMD-160/RipemdProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.RIPEMD_160 5 | { 6 | /// 7 | /// Old RIPEMD-160 Hashing Provider 8 | /// 9 | public class RipemdProvider : ICryptoProvider 10 | { 11 | // Provider Options 12 | private RipemdEncryptionOptions _options; 13 | 14 | /// 15 | /// RIPEMD-160 Provider 16 | /// 17 | /// 18 | public RipemdProvider(RipemdEncryptionOptions options = null) { 19 | _options = options ?? new RipemdEncryptionOptions(); 20 | } 21 | 22 | /// 23 | /// Decrypt String (Not Supported for hashing) 24 | /// 25 | /// 26 | /// 27 | /// 28 | public string DecryptString(string encryptedString) { 29 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 30 | } 31 | 32 | /// 33 | /// Decrypt Binary (Not Supported for hashing) 34 | /// 35 | /// 36 | /// 37 | /// 38 | public byte[] DecryptBinary(byte[] encryptedBinary) { 39 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 40 | } 41 | 42 | /// 43 | /// Get RIPEMD-160 Hash from String 44 | /// 45 | /// 46 | /// 47 | public string EncryptString(string decryptedString) { 48 | byte[] encrypted = EncryptBinary(_options.encoding.GetBytes(decryptedString)); 49 | return Convert.ToBase64String(encrypted); 50 | } 51 | 52 | /// 53 | /// Get RIPEMD-160 Hash from Byte Array 54 | /// 55 | /// 56 | /// 57 | public byte[] EncryptBinary(byte[] decryptedBinary) { 58 | using (RIPEMD160 r160 = RIPEMD160.Create()) 59 | { 60 | byte[] hashBytes = r160.ComputeHash(decryptedBinary); 61 | return hashBytes; 62 | } 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RIPEMD-160/RipemdProvider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 471108039962485781b7fb6befbd1009 3 | timeCreated: 1709897391 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RSA.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 866a055b07f84eacaee1ae7b40aa148e 3 | timeCreated: 1709889644 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RSA/RSAEncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.RSA 5 | { 6 | [System.Serializable] 7 | public class RSAEncryptionOptions : IProviderOptions 8 | { 9 | public string publicKey = "RSAPublicKey"; 10 | public string privateKey = "RSAPrivateKey"; 11 | 12 | public Encoding encoding = Encoding.UTF8; 13 | } 14 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RSA/RSAEncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ee37ae4cb385462d96af708da83eadfa 3 | timeCreated: 1709894551 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RSA/RSAProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Security.Cryptography; 4 | using DevsDaddy.Shared.CryptoLibrary.Core; 5 | 6 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.RSA 7 | { 8 | /// 9 | /// RSA Encryption Provider 10 | /// Using for encrypt/decrypt plain text or byte arrays 11 | /// 12 | public class RSAProvider : ICryptoProvider 13 | { 14 | // Provider Options 15 | private RSAEncryptionOptions _options; 16 | 17 | /// 18 | /// RSA Encryption Provider 19 | /// 20 | /// 21 | public RSAProvider(RSAEncryptionOptions options = null) { 22 | _options = options ?? new RSAEncryptionOptions(); 23 | } 24 | 25 | /// 26 | /// Decrypt RSA-encrypted string 27 | /// 28 | /// 29 | /// 30 | public string DecryptString(string encryptedString) { 31 | byte[] decrypted = DecryptBinary(Convert.FromBase64String(encryptedString)); 32 | return _options.encoding.GetString(decrypted); 33 | } 34 | 35 | /// 36 | /// Decrypt RSA-encrypted byte array 37 | /// 38 | /// 39 | /// 40 | public byte[] DecryptBinary(byte[] encryptedBinary) { 41 | using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) 42 | { 43 | rsa.FromXmlString(_options.privateKey); 44 | byte[] decrypted = rsa.Decrypt(encryptedBinary, false); 45 | return decrypted; 46 | } 47 | } 48 | 49 | /// 50 | /// Encrypt plain-text to RSA-encrypted string 51 | /// 52 | /// 53 | /// 54 | public string EncryptString(string decryptedString) { 55 | byte[] encrypted = EncryptBinary(_options.encoding.GetBytes(decryptedString)); 56 | return Convert.ToBase64String(encrypted); 57 | } 58 | 59 | /// 60 | /// Encrypt byte array to RSA-encrypted byte array 61 | /// 62 | /// 63 | /// 64 | public byte[] EncryptBinary(byte[] decryptedBinary) { 65 | using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) 66 | { 67 | rsa.FromXmlString(_options.publicKey); 68 | byte[] encrypted = rsa.Encrypt(decryptedBinary, false); 69 | return encrypted; 70 | } 71 | } 72 | 73 | /// 74 | /// Generate Key Pair for RSA Provider 75 | /// 76 | /// 77 | /// 78 | public static KeyValuePair GenerateKeyPair(int keySize) 79 | { 80 | RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize); 81 | string publicKey = rsa.ToXmlString(false); 82 | string privateKey = rsa.ToXmlString(true); 83 | return new KeyValuePair(publicKey, privateKey); 84 | } 85 | } 86 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/RSA/RSAProvider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5d97d6eae2424e81b81c16663bc8b7c0 3 | timeCreated: 1709894581 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/SHA.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 04b40f7dc243443aa26ffe21731de7f3 3 | timeCreated: 1709889629 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/SHA/SHA1Provider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Security.Cryptography; 3 | using System.Text; 4 | using DevsDaddy.Shared.CryptoLibrary.Core; 5 | 6 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.SHA 7 | { 8 | /// 9 | /// SHA1 Hashing Provider 10 | /// 11 | public class SHA1Provider : ICryptoProvider 12 | { 13 | private SHAEncryptionOptions _options; 14 | 15 | /// 16 | /// SHA1 Provider 17 | /// 18 | /// 19 | public SHA1Provider(SHAEncryptionOptions options = null) { 20 | _options = options ?? new SHAEncryptionOptions(); 21 | } 22 | 23 | /// 24 | /// Decrypt plain-text (Not Supported for Hashing) 25 | /// 26 | /// 27 | /// 28 | /// 29 | public string DecryptString(string encryptedString) { 30 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 31 | } 32 | 33 | /// 34 | /// Decrypt Binary Data (Not Supported for Hashing) 35 | /// 36 | /// 37 | /// 38 | /// 39 | public byte[] DecryptBinary(byte[] encryptedBinary) { 40 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 41 | } 42 | 43 | /// 44 | /// Get SHA1 Hash for Plain-text 45 | /// 46 | /// 47 | /// 48 | public string EncryptString(string decryptedString) { 49 | using (SHA1Managed sha1 = new SHA1Managed()) 50 | { 51 | var hash = sha1.ComputeHash(_options.encoding.GetBytes(decryptedString)); 52 | var sb = new StringBuilder(hash.Length * 2); 53 | 54 | foreach (byte b in hash) 55 | { 56 | // can be "x2" if you want lowercase 57 | sb.Append(b.ToString("X2")); 58 | } 59 | 60 | return sb.ToString(); 61 | } 62 | } 63 | 64 | /// 65 | /// Get SHA1 Hash for Byte array 66 | /// 67 | /// 68 | /// 69 | public byte[] EncryptBinary(byte[] decryptedBinary) { 70 | using (SHA1Managed sha1 = new SHA1Managed()) 71 | { 72 | var hash = sha1.ComputeHash(decryptedBinary); 73 | return hash; 74 | } 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/SHA/SHA1Provider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6d4c9e5dd260446583ebdff39bc31329 3 | timeCreated: 1709895806 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/SHA/SHA256Provider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Security.Cryptography; 3 | using System.Text; 4 | using DevsDaddy.Shared.CryptoLibrary.Core; 5 | 6 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.SHA 7 | { 8 | /// 9 | /// SHA1 Hashing Provider 10 | /// 11 | public class SHA256Provider : ICryptoProvider 12 | { 13 | private SHAEncryptionOptions _options; 14 | 15 | /// 16 | /// SHA256 Provider 17 | /// 18 | /// 19 | public SHA256Provider(SHAEncryptionOptions options = null) { 20 | _options = options ?? new SHAEncryptionOptions(); 21 | } 22 | 23 | /// 24 | /// Decrypt plain-text (Not Supported for Hashing) 25 | /// 26 | /// 27 | /// 28 | /// 29 | public string DecryptString(string encryptedString) { 30 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 31 | } 32 | 33 | /// 34 | /// Decrypt Binary Data (Not Supported for Hashing) 35 | /// 36 | /// 37 | /// 38 | /// 39 | public byte[] DecryptBinary(byte[] encryptedBinary) { 40 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 41 | } 42 | 43 | /// 44 | /// Get SHA256 Hash for Plain-text 45 | /// 46 | /// 47 | /// 48 | public string EncryptString(string decryptedString) { 49 | using (SHA256Managed sha256 = new SHA256Managed()) 50 | { 51 | var hash = sha256.ComputeHash(_options.encoding.GetBytes(decryptedString)); 52 | var sb = new StringBuilder(hash.Length * 2); 53 | 54 | foreach (byte b in hash) 55 | { 56 | // can be "x2" if you want lowercase 57 | sb.Append(b.ToString("X2")); 58 | } 59 | 60 | return sb.ToString(); 61 | } 62 | } 63 | 64 | /// 65 | /// Get SHA256 Hash for Byte array 66 | /// 67 | /// 68 | /// 69 | public byte[] EncryptBinary(byte[] decryptedBinary) { 70 | using (SHA256Managed sha256 = new SHA256Managed()) 71 | { 72 | var hash = sha256.ComputeHash(decryptedBinary); 73 | return hash; 74 | } 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/SHA/SHA256Provider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cb92a95f816c49599c5f2b4ee4d38422 3 | timeCreated: 1709895861 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/SHA/SHA512Provider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Security.Cryptography; 3 | using System.Text; 4 | using DevsDaddy.Shared.CryptoLibrary.Core; 5 | 6 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.SHA 7 | { 8 | /// 9 | /// SHA512 Hashing Provider 10 | /// 11 | public class SHA512Provider : ICryptoProvider 12 | { 13 | private SHAEncryptionOptions _options; 14 | 15 | /// 16 | /// SHA512 Provider 17 | /// 18 | /// 19 | public SHA512Provider(SHAEncryptionOptions options = null) { 20 | _options = options ?? new SHAEncryptionOptions(); 21 | } 22 | 23 | /// 24 | /// Decrypt plain-text (Not Supported for Hashing) 25 | /// 26 | /// 27 | /// 28 | /// 29 | public string DecryptString(string encryptedString) { 30 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 31 | } 32 | 33 | /// 34 | /// Decrypt Binary Data (Not Supported for Hashing) 35 | /// 36 | /// 37 | /// 38 | /// 39 | public byte[] DecryptBinary(byte[] encryptedBinary) { 40 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 41 | } 42 | 43 | /// 44 | /// Get SHA512 Hash for Plain-text 45 | /// 46 | /// 47 | /// 48 | public string EncryptString(string decryptedString) { 49 | using (SHA512Managed sha256 = new SHA512Managed()) 50 | { 51 | var hash = sha256.ComputeHash(_options.encoding.GetBytes(decryptedString)); 52 | var sb = new StringBuilder(hash.Length * 2); 53 | 54 | foreach (byte b in hash) 55 | { 56 | // can be "x2" if you want lowercase 57 | sb.Append(b.ToString("X2")); 58 | } 59 | 60 | return sb.ToString(); 61 | } 62 | } 63 | 64 | /// 65 | /// Get SHA512 Hash for Byte array 66 | /// 67 | /// 68 | /// 69 | public byte[] EncryptBinary(byte[] decryptedBinary) { 70 | using (SHA512Managed sha512 = new SHA512Managed()) 71 | { 72 | var hash = sha512.ComputeHash(decryptedBinary); 73 | return hash; 74 | } 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/SHA/SHA512Provider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2164e47f6c7a4874b422c7789539ec18 3 | timeCreated: 1709895868 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/SHA/SHAEncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.SHA 5 | { 6 | [System.Serializable] 7 | public class SHAEncryptionOptions : IProviderOptions 8 | { 9 | public Encoding encoding = Encoding.UTF8; 10 | } 11 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/SHA/SHAEncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 329c2b00898a419ebd3501427a96764f 3 | timeCreated: 1709895780 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/TripleDES.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: aac9be5c0d2c4e468ce9d2fe00d89460 3 | timeCreated: 1709896341 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/TripleDES/TripleDESEncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | using System.Text; 3 | using DevsDaddy.Shared.CryptoLibrary.Core; 4 | 5 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.TripleDES 6 | { 7 | [System.Serializable] 8 | public class TripleDESEncryptionOptions : IProviderOptions 9 | { 10 | public string cryptoKey = "ABCDEFGH"; 11 | public Encoding encoding = Encoding.UTF8; 12 | 13 | public CipherMode mode = CipherMode.ECB; 14 | public PaddingMode padding = PaddingMode.PKCS7; 15 | } 16 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/TripleDES/TripleDESEncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5454f018e8874bc88dae375a8e20f892 3 | timeCreated: 1709896365 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/TripleDES/TripleDESProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Security.Cryptography; 3 | using DevsDaddy.Shared.CryptoLibrary.Core; 4 | 5 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.TripleDES 6 | { 7 | /// 8 | /// Triple DES Modern Crypto Provider 9 | /// 10 | public class TripleDESProvider : ICryptoProvider 11 | { 12 | private TripleDESEncryptionOptions _options; 13 | 14 | /// 15 | /// Triple DES Provider 16 | /// 17 | /// 18 | public TripleDESProvider(TripleDESEncryptionOptions options = null) { 19 | _options = options ?? new TripleDESEncryptionOptions(); 20 | } 21 | 22 | /// 23 | /// Decrypt plain-text using Triple DES 24 | /// 25 | /// 26 | /// 27 | public string DecryptString(string encryptedString) { 28 | byte[] txtByteData = Convert.FromBase64String(encryptedString); 29 | byte[] decoded = DecryptBinary(txtByteData); 30 | return _options.encoding.GetString(decoded); 31 | } 32 | 33 | /// 34 | /// Decrypt byte array using Triple DES 35 | /// 36 | /// 37 | /// 38 | public byte[] DecryptBinary(byte[] encryptedBinary) { 39 | MD5CryptoServiceProvider MD5CryptoService = new MD5CryptoServiceProvider(); 40 | byte[] securityKeyArray = MD5CryptoService.ComputeHash(_options.encoding.GetBytes(_options.cryptoKey)); 41 | MD5CryptoService.Clear(); 42 | 43 | var tripleDESCryptoService = new TripleDESCryptoServiceProvider(); 44 | tripleDESCryptoService.Key = securityKeyArray; 45 | tripleDESCryptoService.Mode = _options.mode; 46 | tripleDESCryptoService.Padding = _options.padding; 47 | 48 | var crytpoTransform = tripleDESCryptoService.CreateDecryptor(); 49 | byte[] resultArray = crytpoTransform.TransformFinalBlock(encryptedBinary, 0, encryptedBinary.Length); 50 | tripleDESCryptoService.Clear(); 51 | return resultArray; 52 | } 53 | 54 | /// 55 | /// Encrypt Plain-Text using Triple DES 56 | /// 57 | /// 58 | /// 59 | public string EncryptString(string decryptedString) { 60 | byte[] txtByteData = _options.encoding.GetBytes(decryptedString); 61 | byte[] encoded = EncryptBinary(txtByteData); 62 | return Convert.ToBase64String(encoded); 63 | } 64 | 65 | /// 66 | /// Encrypt Byte Array using Triple DES 67 | /// 68 | /// 69 | /// 70 | public byte[] EncryptBinary(byte[] decryptedBinary) { 71 | MD5CryptoServiceProvider MD5CryptoService = new MD5CryptoServiceProvider(); 72 | byte[] securityKeyArray = MD5CryptoService.ComputeHash(_options.encoding.GetBytes(_options.cryptoKey)); 73 | MD5CryptoService.Clear(); 74 | 75 | var tripleDESCryptoService = new TripleDESCryptoServiceProvider(); 76 | tripleDESCryptoService.Key = securityKeyArray; 77 | tripleDESCryptoService.Mode = _options.mode; 78 | tripleDESCryptoService.Padding = _options.padding; 79 | 80 | var crytpoTransform = tripleDESCryptoService.CreateEncryptor(); 81 | byte[] resultArray = crytpoTransform.TransformFinalBlock(decryptedBinary, 0, decryptedBinary.Length); 82 | 83 | tripleDESCryptoService.Clear(); 84 | return resultArray; 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/TripleDES/TripleDESProvider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d899fe1793c34e3984a69625da66e8be 3 | timeCreated: 1709896382 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Twofish.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 533b53f6351c40b0a209b008dc6facaf 3 | timeCreated: 1709897320 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Twofish/Twofish.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.CompilerServices; 3 | using System.Security.Cryptography; 4 | 5 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.Twofish 6 | { 7 | public sealed class Twofish : SymmetricAlgorithm 8 | { 9 | public Twofish() : base() { 10 | KeySizeValue = KeySizeInBits; 11 | BlockSizeValue = BlockSizeInBits; 12 | FeedbackSizeValue = BlockSizeValue; 13 | LegalBlockSizesValue = new KeySizes[] { new KeySizes(BlockSizeInBits, BlockSizeInBits, 0) }; 14 | LegalKeySizesValue = new KeySizes[] { new KeySizes(128, 256, 64) }; // 128, 192, or 256 15 | 16 | base.Mode = CipherMode.CBC; // same as default 17 | base.Padding = PaddingMode.PKCS7; 18 | } 19 | 20 | public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV) { 21 | if (rgbKey == null) { throw new ArgumentNullException(nameof(rgbKey), "Key cannot be null."); } 22 | if (rgbKey.Length != KeySize / 8) { throw new ArgumentOutOfRangeException(nameof(rgbKey), "Key size mismatch."); } 23 | if (Mode == CipherMode.CBC) { 24 | if (rgbIV == null) { throw new ArgumentNullException(nameof(rgbIV), "IV cannot be null."); } 25 | if (rgbIV.Length != 16) { throw new ArgumentOutOfRangeException(nameof(rgbIV), "Invalid IV size."); } 26 | } 27 | 28 | return new TwofishTransform(rgbKey, rgbIV, TwofishTransformMode.Decrypt, Mode, Padding); 29 | } 30 | 31 | public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV) { 32 | if (rgbKey == null) { throw new ArgumentNullException(nameof(rgbKey), "Key cannot be null."); } 33 | if (rgbKey.Length != KeySize / 8) { throw new ArgumentOutOfRangeException(nameof(rgbKey), "Key size mismatch."); } 34 | if (Mode == CipherMode.CBC) { 35 | if (rgbIV == null) { throw new ArgumentNullException(nameof(rgbIV), "IV cannot be null."); } 36 | if (rgbIV.Length != 16) { throw new ArgumentOutOfRangeException(nameof(rgbIV), "Invalid IV size."); } 37 | } 38 | 39 | return new TwofishTransform(rgbKey, rgbIV, TwofishTransformMode.Encrypt, Mode, Padding); 40 | } 41 | 42 | public override void GenerateIV() { 43 | byte[] rndKey = new byte[FeedbackSizeValue / 8]; 44 | RandomNumberGenerator generator = RandomNumberGenerator.Create(); 45 | generator.GetBytes(rndKey); 46 | IVValue = rndKey; 47 | } 48 | 49 | public override void GenerateKey() { 50 | byte[] rndKey = new byte[KeySizeValue / 8]; 51 | RandomNumberGenerator generator = RandomNumberGenerator.Create(); 52 | generator.GetBytes(rndKey); 53 | KeyValue = rndKey; 54 | } 55 | 56 | public override CipherMode Mode { 57 | get { return base.Mode; } 58 | set { 59 | switch (value) { 60 | case CipherMode.CBC: break; 61 | #pragma warning disable CA5358 // While using ECB is not recommended, it's still supported 62 | case CipherMode.ECB: break; 63 | #pragma warning restore CA5358 // Review cipher mode usage with cryptography experts 64 | default: throw new CryptographicException("Cipher mode is not supported."); 65 | } 66 | base.Mode = value; 67 | } 68 | } 69 | 70 | public override PaddingMode Padding { 71 | get { return base.Padding; } 72 | set { 73 | base.Padding = value switch { 74 | PaddingMode.None => value, 75 | PaddingMode.PKCS7 => value, 76 | PaddingMode.Zeros => value, 77 | PaddingMode.ANSIX923 => value, 78 | PaddingMode.ISO10126 => value, 79 | _ => throw new CryptographicException("Padding mode is not supported."), 80 | }; 81 | } 82 | } 83 | 84 | private const int KeySizeInBits = 256; 85 | private const int BlockSizeInBits = 128; 86 | } 87 | 88 | enum TwofishTransformMode { 89 | Encrypt = 0, 90 | Decrypt = 1 91 | } 92 | 93 | sealed class TwofishTransform : ICryptoTransform 94 | { 95 | internal TwofishTransform(byte[] rgbKey, byte[]? rgbIV, TwofishTransformMode transformMode, CipherMode cipherMode, PaddingMode paddingMode) { 96 | if (rgbKey == null) { throw new ArgumentNullException(nameof(rgbKey), "Key cannot be null."); } 97 | if (rgbKey.Length is not 16 and not 24 and not 32) { throw new ArgumentOutOfRangeException(nameof(rgbKey), "Key must be 128, 192, or 256 bits."); } 98 | if ((rgbIV is not null) && (rgbIV.Length != 16)) { throw new ArgumentOutOfRangeException(nameof(rgbKey), "IV must be 128 bits."); } 99 | CipherMode = cipherMode; 100 | TransformMode = transformMode; 101 | PaddingMode = paddingMode; 102 | 103 | DecryptionBuffer = new byte[16]; 104 | 105 | Key = new DWord[rgbKey.Length / 4]; 106 | SBoxKeys = new DWord[MaxKeyBits / 64]; 107 | SubKeys = new DWord[TotalSubkeys]; 108 | 109 | var key32 = new uint[Key.Length]; 110 | Buffer.BlockCopy(rgbKey, 0, key32, 0, rgbKey.Length); 111 | for (var i = 0; i < Key.Length; i++) { Key[i] = (DWord)key32[i]; } 112 | Array.Clear(key32, 0, key32.Length); 113 | 114 | if (rgbIV != null) { 115 | IV = new DWord[rgbIV.Length]; 116 | var iv32 = new uint[IV.Length]; 117 | Buffer.BlockCopy(rgbIV, 0, iv32, 0, rgbIV.Length); 118 | for (var i = 0; i < IV.Length; i++) { IV[i] = (DWord)iv32[i]; } 119 | Array.Clear(iv32, 0, iv32.Length); 120 | } 121 | 122 | ReKey(); 123 | } 124 | 125 | private readonly TwofishTransformMode TransformMode; 126 | private readonly CipherMode CipherMode; 127 | private readonly PaddingMode PaddingMode; 128 | 129 | /// 130 | /// Gets a value indicating whether the current transform can be reused. 131 | /// 132 | public bool CanReuseTransform { get { return false; } } 133 | 134 | /// 135 | /// Gets a value indicating whether multiple blocks can be transformed. 136 | /// 137 | public bool CanTransformMultipleBlocks { get { return true; } } 138 | 139 | /// 140 | /// Gets the input block size (in bytes). 141 | /// 142 | public int InputBlockSize { get { return 16; } } //block is always 128 bits 143 | 144 | /// 145 | /// Gets the output block size (in bytes). 146 | /// 147 | public int OutputBlockSize { get { return 16; } } //block is always 128 bits 148 | 149 | /// 150 | /// Releases resources. 151 | /// 152 | public void Dispose() { 153 | Dispose(true); 154 | } 155 | 156 | private void Dispose(bool disposing) { 157 | if (disposing) { 158 | Array.Clear(Key, 0, Key.Length); 159 | if (IV != null) { Array.Clear(IV, 0, IV.Length); } 160 | Array.Clear(SBoxKeys, 0, SBoxKeys.Length); 161 | Array.Clear(SubKeys, 0, SubKeys.Length); 162 | Array.Clear(DecryptionBuffer, 0, DecryptionBuffer.Length); 163 | } 164 | } 165 | 166 | public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { 167 | if (inputBuffer == null) { throw new ArgumentNullException(nameof(inputBuffer), "Input buffer cannot be null."); } 168 | if (outputBuffer == null) { throw new ArgumentNullException(nameof(outputBuffer), "Output buffer cannot be null."); } 169 | if (inputOffset < 0) { throw new ArgumentOutOfRangeException(nameof(inputOffset), "Input offset must be non-negative number."); } 170 | if (outputOffset < 0) { throw new ArgumentOutOfRangeException(nameof(outputOffset), "Output offset must be non-negative number."); } 171 | if ((inputCount <= 0) || (inputCount % 16 != 0) || (inputCount > inputBuffer.Length)) { throw new ArgumentOutOfRangeException(nameof(inputCount), "Invalid input count."); } 172 | if ((inputBuffer.Length - inputCount) < inputOffset) { throw new ArgumentOutOfRangeException(nameof(inputCount), "Invalid input length."); } 173 | if (outputOffset + inputCount > outputBuffer.Length) { throw new ArgumentOutOfRangeException(nameof(outputOffset), "Insufficient output buffer."); } 174 | 175 | if (TransformMode == TwofishTransformMode.Encrypt) { 176 | 177 | for (var i = 0; i < inputCount; i += 16) { 178 | BlockEncrypt(inputBuffer, inputOffset + i, outputBuffer, outputOffset + i); 179 | } 180 | return inputCount; 181 | 182 | } else { // Decrypt 183 | 184 | var bytesWritten = 0; 185 | 186 | if (DecryptionBufferInUse) { // process the last block of previous round 187 | BlockDecrypt(DecryptionBuffer, 0, outputBuffer, outputOffset); 188 | DecryptionBufferInUse = false; 189 | outputOffset += 16; 190 | bytesWritten += 16; 191 | } 192 | 193 | for (var i = 0; i < inputCount - 16; i += 16) { 194 | BlockDecrypt(inputBuffer, inputOffset + i, outputBuffer, outputOffset); 195 | outputOffset += 16; 196 | bytesWritten += 16; 197 | } 198 | 199 | if (PaddingMode == PaddingMode.None) { 200 | BlockDecrypt(inputBuffer, inputOffset + inputCount - 16, outputBuffer, outputOffset); 201 | return inputCount; 202 | } else { // save last block without processing because decryption otherwise cannot detect padding in CryptoStream 203 | Buffer.BlockCopy(inputBuffer, inputOffset + inputCount - 16, DecryptionBuffer, 0, 16); 204 | DecryptionBufferInUse = true; 205 | } 206 | 207 | return bytesWritten; 208 | 209 | } 210 | } 211 | 212 | public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { 213 | if (inputBuffer == null) { throw new ArgumentNullException(nameof(inputBuffer), "Input buffer cannot be null."); } 214 | if (inputOffset < 0) { throw new ArgumentOutOfRangeException(nameof(inputOffset), "Input offset must be non-negative number."); } 215 | if ((inputCount < 0) || (inputCount > inputBuffer.Length)) { throw new ArgumentOutOfRangeException(nameof(inputCount), "Invalid input count."); } 216 | if ((PaddingMode == PaddingMode.None) && (inputCount % 16 != 0)) { throw new ArgumentOutOfRangeException(nameof(inputCount), "No padding for final block."); } 217 | if ((inputBuffer.Length - inputCount) < inputOffset) { throw new ArgumentOutOfRangeException(nameof(inputCount), "Invalid input length."); } 218 | 219 | if (TransformMode == TwofishTransformMode.Encrypt) { 220 | 221 | int paddedLength; 222 | byte[] paddedInputBuffer; 223 | int paddedInputOffset; 224 | switch (PaddingMode) { 225 | case PaddingMode.None: 226 | paddedLength = inputCount; 227 | paddedInputBuffer = inputBuffer; 228 | paddedInputOffset = inputOffset; 229 | break; 230 | 231 | case PaddingMode.PKCS7: 232 | paddedLength = inputCount / 16 * 16 + 16; //to round to next whole block 233 | paddedInputBuffer = new byte[paddedLength]; 234 | paddedInputOffset = 0; 235 | Buffer.BlockCopy(inputBuffer, inputOffset, paddedInputBuffer, 0, inputCount); 236 | var added = (byte)(paddedLength - inputCount); 237 | for (var i = inputCount; i < inputCount + added; i++) { 238 | paddedInputBuffer[i] = added; 239 | } 240 | break; 241 | 242 | case PaddingMode.Zeros: 243 | paddedLength = (inputCount + 15) / 16 * 16; //to round to next whole block 244 | paddedInputBuffer = new byte[paddedLength]; 245 | paddedInputOffset = 0; 246 | Buffer.BlockCopy(inputBuffer, inputOffset, paddedInputBuffer, 0, inputCount); 247 | break; 248 | 249 | case PaddingMode.ANSIX923: 250 | paddedLength = inputCount / 16 * 16 + 16; //to round to next whole block 251 | paddedInputBuffer = new byte[paddedLength]; 252 | paddedInputOffset = 0; 253 | Buffer.BlockCopy(inputBuffer, inputOffset, paddedInputBuffer, 0, inputCount); 254 | paddedInputBuffer[^1] = (byte)(paddedLength - inputCount); 255 | break; 256 | 257 | case PaddingMode.ISO10126: 258 | paddedLength = inputCount / 16 * 16 + 16; //to round to next whole block 259 | paddedInputBuffer = new byte[paddedLength]; 260 | RandomNumberGenerator.Fill(paddedInputBuffer.AsSpan(inputCount)); 261 | paddedInputOffset = 0; 262 | Buffer.BlockCopy(inputBuffer, inputOffset, paddedInputBuffer, 0, inputCount); 263 | paddedInputBuffer[^1] = (byte)(paddedLength - inputCount); 264 | break; 265 | 266 | default: throw new CryptographicException("Unsupported padding mode."); 267 | } 268 | 269 | var outputBuffer = new byte[paddedLength]; 270 | 271 | for (var i = 0; i < paddedLength; i += 16) { 272 | BlockEncrypt(paddedInputBuffer, paddedInputOffset + i, outputBuffer, i); 273 | } 274 | 275 | return outputBuffer; 276 | 277 | } else { // Decrypt 278 | 279 | byte[] outputBuffer; 280 | 281 | if (PaddingMode == PaddingMode.None) { 282 | outputBuffer = new byte[inputCount]; 283 | } else if (inputCount % 16 != 0) { 284 | throw new ArgumentOutOfRangeException(nameof(inputCount), "Invalid input count."); 285 | } else { 286 | outputBuffer = new byte[inputCount + (DecryptionBufferInUse ? 16 : 0)]; 287 | } 288 | 289 | var outputOffset = 0; 290 | 291 | if (DecryptionBufferInUse) { // process leftover padding buffer to keep CryptoStream happy 292 | BlockDecrypt(DecryptionBuffer, 0, outputBuffer, 0); 293 | DecryptionBufferInUse = false; 294 | outputOffset = 16; 295 | } 296 | 297 | for (var i = 0; i < inputCount; i += 16) { 298 | BlockDecrypt(inputBuffer, inputOffset + i, outputBuffer, outputOffset + i); 299 | } 300 | 301 | return RemovePadding(outputBuffer, PaddingMode); 302 | 303 | } 304 | } 305 | 306 | private readonly byte[] DecryptionBuffer; 307 | private bool DecryptionBufferInUse; 308 | 309 | private static byte[] RemovePadding(byte[] outputBuffer, PaddingMode paddingMode) { 310 | if (paddingMode == PaddingMode.PKCS7) { 311 | var padding = outputBuffer[^1]; 312 | if (padding is < 1 or > 16) { throw new CryptographicException("Invalid padding."); } 313 | for (var i = outputBuffer.Length - padding; i < outputBuffer.Length; i++) { 314 | if (outputBuffer[i] != padding) { throw new CryptographicException("Invalid padding."); } 315 | } 316 | var newOutputBuffer = new byte[outputBuffer.Length - padding]; 317 | Buffer.BlockCopy(outputBuffer, 0, newOutputBuffer, 0, newOutputBuffer.Length); 318 | return newOutputBuffer; 319 | } else if (paddingMode == PaddingMode.Zeros) { 320 | var newOutputLength = outputBuffer.Length; 321 | for (var i = outputBuffer.Length - 1; i >= Math.Max(outputBuffer.Length - 16, 0); i--) { 322 | if (outputBuffer[i] != 0) { 323 | newOutputLength = i + 1; 324 | break; 325 | } 326 | } 327 | if (newOutputLength == outputBuffer.Length) { 328 | return outputBuffer; 329 | } else { 330 | var newOutputBuffer = new byte[newOutputLength]; 331 | Buffer.BlockCopy(outputBuffer, 0, newOutputBuffer, 0, newOutputBuffer.Length); 332 | return newOutputBuffer; 333 | } 334 | } else if (paddingMode == PaddingMode.ANSIX923) { 335 | var padding = outputBuffer[^1]; 336 | if (padding is < 1 or > 16) { throw new CryptographicException("Invalid padding."); } 337 | for (var i = outputBuffer.Length - padding; i < outputBuffer.Length - 1; i++) { 338 | if (outputBuffer[i] != 0) { throw new CryptographicException("Invalid padding."); } 339 | } 340 | var newOutputBuffer = new byte[outputBuffer.Length - padding]; 341 | Buffer.BlockCopy(outputBuffer, 0, newOutputBuffer, 0, newOutputBuffer.Length); 342 | return newOutputBuffer; 343 | } else if (paddingMode == PaddingMode.ISO10126) { 344 | var padding = outputBuffer[^1]; 345 | if (padding is < 1 or > 16) { throw new CryptographicException("Invalid padding."); } 346 | var newOutputBuffer = new byte[outputBuffer.Length - padding]; 347 | Buffer.BlockCopy(outputBuffer, 0, newOutputBuffer, 0, newOutputBuffer.Length); 348 | return newOutputBuffer; 349 | } else { // None 350 | return outputBuffer; 351 | } 352 | } 353 | 354 | private const int BlockSize = 128; //number of bits per block 355 | private const int Rounds = 16; //default number of rounds for 128/192/256-bit keys 356 | private const int MaxKeyBits = 256; //max number of bits of key 357 | 358 | private const int InputWhiten = 0; 359 | private const int OutputWhiten = (InputWhiten + BlockSize / 32); 360 | private const int RoundSubkeys = (OutputWhiten + BlockSize / 32); 361 | private const int TotalSubkeys = (RoundSubkeys + 2 * Rounds); 362 | 363 | private readonly DWord[] Key; 364 | private readonly DWord[]? IV; 365 | private readonly DWord[] SBoxKeys; 366 | private readonly DWord[] SubKeys; 367 | 368 | private const int SubkeyStep = 0x02020202; 369 | private const int SubkeyBump = 0x01010101; 370 | private const int SubkeyRotateLeft = 9; 371 | 372 | private void ReKey() { 373 | BuildMds(); //built only first time it is accessed 374 | 375 | var k32e = new DWord[Key.Length / 2]; 376 | var k32o = new DWord[Key.Length / 2]; //even/odd key dwords 377 | 378 | var k64Cnt = Key.Length / 2; 379 | for (var i = 0; i < k64Cnt; i++) { //split into even/odd key dwords 380 | k32e[i] = Key[2 * i]; 381 | k32o[i] = Key[2 * i + 1]; 382 | SBoxKeys[k64Cnt - 1 - i] = ReedSolomonMdsEncode(k32e[i], k32o[i]); //compute S-box keys using (12,8) Reed-Solomon code over GF(256) 383 | } 384 | 385 | var subkeyCnt = RoundSubkeys + 2 * Rounds; 386 | var keyLen = Key.Length * 4 * 8; 387 | for (var i = 0; i < subkeyCnt / 2; i++) { //compute round subkeys for PHT 388 | var A = F32((DWord)(i * SubkeyStep), k32e, keyLen); //A uses even key dwords 389 | var B = F32((DWord)(i * SubkeyStep + SubkeyBump), k32o, keyLen); //B uses odd key dwords 390 | B = B.RotateLeft(8); 391 | SubKeys[2 * i] = A + B; //combine with a PHT 392 | SubKeys[2 * i + 1] = (A + 2 * B).RotateLeft(SubkeyRotateLeft); 393 | } 394 | } 395 | 396 | internal void BlockEncrypt(byte[] inputBuffer, int inputOffset, byte[] outputBuffer, int outputBufferOffset) { 397 | var x = new DWord[BlockSize / 32]; 398 | for (var i = 0; i < BlockSize / 32; i++) { //copy in the block, add whitening 399 | x[i] = new DWord(inputBuffer, inputOffset + i * 4) ^ SubKeys[InputWhiten + i]; 400 | if ((CipherMode == CipherMode.CBC) && (IV != null)) { x[i] ^= IV[i]; } 401 | } 402 | 403 | var keyLen = Key.Length * 4 * 8; 404 | for (var r = 0; r < Rounds; r++) { //main Twofish encryption loop 405 | var t0 = F32(x[0], SBoxKeys, keyLen); 406 | var t1 = F32(x[1].RotateLeft(8), SBoxKeys, keyLen); 407 | 408 | x[3] = x[3].RotateLeft(1); 409 | x[2] ^= t0 + t1 + SubKeys[RoundSubkeys + 2 * r]; //PHT, round keys 410 | x[3] ^= t0 + 2 * t1 + SubKeys[RoundSubkeys + 2 * r + 1]; 411 | x[2] = x[2].RotateRight(1); 412 | 413 | if (r < Rounds - 1) { //swap for next round 414 | (x[2], x[0]) = (x[0], x[2]); 415 | (x[3], x[1]) = (x[1], x[3]); 416 | } 417 | } 418 | 419 | for (var i = 0; i < BlockSize / 32; i++) { //copy out, with whitening 420 | var outValue = x[i] ^ SubKeys[OutputWhiten + i]; 421 | outputBuffer[outputBufferOffset + i * 4 + 0] = outValue.B0; 422 | outputBuffer[outputBufferOffset + i * 4 + 1] = outValue.B1; 423 | outputBuffer[outputBufferOffset + i * 4 + 2] = outValue.B2; 424 | outputBuffer[outputBufferOffset + i * 4 + 3] = outValue.B3; 425 | if ((CipherMode == CipherMode.CBC) && (IV != null)) { IV[i] = outValue; } 426 | } 427 | } 428 | 429 | internal void BlockDecrypt(byte[] inputBuffer, int inputOffset, byte[] outputBuffer, int outputBufferOffset) { 430 | var x = new DWord[BlockSize / 32]; 431 | var input = new DWord[BlockSize / 32]; 432 | for (var i = 0; i < BlockSize / 32; i++) { //copy in the block, add whitening 433 | input[i] = new DWord(inputBuffer, inputOffset + i * 4); 434 | x[i] = input[i] ^ SubKeys[OutputWhiten + i]; 435 | } 436 | 437 | var keyLen = Key.Length * 4 * 8; 438 | for (var r = Rounds - 1; r >= 0; r--) { //main Twofish decryption loop 439 | var t0 = F32(x[0], SBoxKeys, keyLen); 440 | var t1 = F32(x[1].RotateLeft(8), SBoxKeys, keyLen); 441 | 442 | x[2] = x[2].RotateLeft(1); 443 | x[2] ^= t0 + t1 + SubKeys[RoundSubkeys + 2 * r]; //PHT, round keys 444 | x[3] ^= t0 + 2 * t1 + SubKeys[RoundSubkeys + 2 * r + 1]; 445 | x[3] = x[3].RotateRight(1); 446 | 447 | if (r > 0) { //unswap, except for last round 448 | t0 = x[0]; x[0] = x[2]; x[2] = t0; 449 | t1 = x[1]; x[1] = x[3]; x[3] = t1; 450 | } 451 | } 452 | 453 | for (var i = 0; i < BlockSize / 32; i++) { //copy out, with whitening 454 | x[i] ^= SubKeys[InputWhiten + i]; 455 | if ((CipherMode == CipherMode.CBC) && (IV != null)) { 456 | x[i] ^= IV[i]; 457 | IV[i] = input[i]; 458 | } 459 | outputBuffer[outputBufferOffset + i * 4 + 0] = x[i].B0; 460 | outputBuffer[outputBufferOffset + i * 4 + 1] = x[i].B1; 461 | outputBuffer[outputBufferOffset + i * 4 + 2] = x[i].B2; 462 | outputBuffer[outputBufferOffset + i * 4 + 3] = x[i].B3; 463 | } 464 | } 465 | 466 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 467 | private static DWord F32(DWord x, DWord[] k32, int keyLen) { 468 | if (keyLen >= 256) { 469 | x.B0 = (byte)(P8x8[P_04][x.B0] ^ k32[3].B0); 470 | x.B1 = (byte)(P8x8[P_14][x.B1] ^ k32[3].B1); 471 | x.B2 = (byte)(P8x8[P_24][x.B2] ^ k32[3].B2); 472 | x.B3 = (byte)(P8x8[P_34][x.B3] ^ k32[3].B3); 473 | } 474 | if (keyLen >= 192) { 475 | x.B0 = (byte)(P8x8[P_03][x.B0] ^ k32[2].B0); 476 | x.B1 = (byte)(P8x8[P_13][x.B1] ^ k32[2].B1); 477 | x.B2 = (byte)(P8x8[P_23][x.B2] ^ k32[2].B2); 478 | x.B3 = (byte)(P8x8[P_33][x.B3] ^ k32[2].B3); 479 | } 480 | if (keyLen >= 128) { 481 | x = MdsTable[0][P8x8[P_01][P8x8[P_02][x.B0] ^ k32[1].B0] ^ k32[0].B0] 482 | ^ MdsTable[1][P8x8[P_11][P8x8[P_12][x.B1] ^ k32[1].B1] ^ k32[0].B1] 483 | ^ MdsTable[2][P8x8[P_21][P8x8[P_22][x.B2] ^ k32[1].B2] ^ k32[0].B2] 484 | ^ MdsTable[3][P8x8[P_31][P8x8[P_32][x.B3] ^ k32[1].B3] ^ k32[0].B3]; 485 | } 486 | 487 | return x; 488 | } 489 | 490 | private const uint P_01 = 0; 491 | private const uint P_02 = 0; 492 | private const uint P_03 = (P_01 ^ 1); // "extend" to larger key sizes 493 | private const uint P_04 = 1; 494 | 495 | private const uint P_11 = 0; 496 | private const uint P_12 = 1; 497 | private const uint P_13 = (P_11 ^ 1); 498 | private const uint P_14 = 0; 499 | 500 | private const uint P_21 = 1; 501 | private const uint P_22 = 0; 502 | private const uint P_23 = (P_21 ^ 1); 503 | private const uint P_24 = 0; 504 | 505 | private const uint P_31 = 1; 506 | private const uint P_32 = 1; 507 | private const uint P_33 = (P_31 ^ 1); 508 | private const uint P_34 = 1; 509 | 510 | private static readonly byte[][] P8x8 = new byte[][] { 511 | new byte[] { 512 | 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 513 | 0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38, 514 | 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, 515 | 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 516 | 0xF2, 0xD0, 0x8B, 0x30, 0x84, 0x54, 0xDF, 0x23, 517 | 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82, 518 | 0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 519 | 0xA6, 0xEB, 0xA5, 0xBE, 0x16, 0x0C, 0xE3, 0x61, 520 | 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B, 521 | 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 522 | 0xE1, 0xE6, 0xBD, 0x45, 0xE2, 0xF4, 0xB6, 0x66, 523 | 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7, 524 | 0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 525 | 0xEA, 0x77, 0x39, 0xAF, 0x33, 0xC9, 0x62, 0x71, 526 | 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8, 527 | 0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 528 | 0xA1, 0x1D, 0xAA, 0xED, 0x06, 0x70, 0xB2, 0xD2, 529 | 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90, 530 | 0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 531 | 0x9E, 0x9C, 0x52, 0x1B, 0x5F, 0x93, 0x0A, 0xEF, 532 | 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B, 533 | 0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 534 | 0x2A, 0xCE, 0xCB, 0x2F, 0xFC, 0x97, 0x05, 0x7A, 535 | 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A, 536 | 0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 537 | 0xB8, 0xDA, 0xB0, 0x17, 0x55, 0x1F, 0x8A, 0x7D, 538 | 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72, 539 | 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 540 | 0x6E, 0x50, 0xDE, 0x68, 0x65, 0xBC, 0xDB, 0xF8, 541 | 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4, 542 | 0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 543 | 0x6F, 0x9D, 0x36, 0x42, 0x4A, 0x5E, 0xC1, 0xE0 544 | }, 545 | new byte[] { 546 | 0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 547 | 0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B, 548 | 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1, 549 | 0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, 550 | 0x5E, 0xBA, 0xAE, 0x5B, 0x8A, 0x00, 0xBC, 0x9D, 551 | 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5, 552 | 0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 553 | 0xB2, 0x73, 0x4C, 0x54, 0x92, 0x74, 0x36, 0x51, 554 | 0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96, 555 | 0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, 556 | 0x13, 0x95, 0x9C, 0xC7, 0x24, 0x46, 0x3B, 0x70, 557 | 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8, 558 | 0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 559 | 0x03, 0x6F, 0x08, 0xBF, 0x40, 0xE7, 0x2B, 0xE2, 560 | 0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9, 561 | 0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, 562 | 0x66, 0x94, 0xA1, 0x1D, 0x3D, 0xF0, 0xDE, 0xB3, 563 | 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E, 564 | 0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 565 | 0x81, 0x88, 0xEE, 0x21, 0xC4, 0x1A, 0xEB, 0xD9, 566 | 0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01, 567 | 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, 568 | 0x4F, 0xF2, 0x65, 0x8E, 0x78, 0x5C, 0x58, 0x19, 569 | 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64, 570 | 0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 571 | 0xCE, 0xE9, 0x68, 0x44, 0xE0, 0x4D, 0x43, 0x69, 572 | 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, 573 | 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, 574 | 0x22, 0xC9, 0xC0, 0x9B, 0x89, 0xD4, 0xED, 0xAB, 575 | 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9, 576 | 0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 577 | 0x16, 0x25, 0x86, 0x56, 0x55, 0x09, 0xBE, 0x91 578 | } 579 | }; 580 | 581 | private static readonly DWord[][] MdsTable = new DWord[4][] { new DWord[256], new DWord[256], new DWord[256], new DWord[256] }; 582 | private static bool MdsTableBuilt; 583 | private static readonly object SyncRootBuildMds = new(); 584 | 585 | private static void BuildMds() { 586 | lock (SyncRootBuildMds) { 587 | if (MdsTableBuilt) { return; } 588 | 589 | var m1 = new byte[2]; 590 | var mX = new byte[2]; 591 | var mY = new byte[4]; 592 | 593 | for (var i = 0; i < 256; i++) { 594 | m1[0] = P8x8[0][i]; /* compute all the matrix elements */ 595 | mX[0] = (byte)Mul_X(m1[0]); 596 | mY[0] = (byte)Mul_Y(m1[0]); 597 | 598 | m1[1] = P8x8[1][i]; 599 | mX[1] = (byte)Mul_X(m1[1]); 600 | mY[1] = (byte)Mul_Y(m1[1]); 601 | 602 | MdsTable[0][i].B0 = m1[1]; 603 | MdsTable[0][i].B1 = mX[1]; 604 | MdsTable[0][i].B2 = mY[1]; 605 | MdsTable[0][i].B3 = mY[1]; //SetMDS(0); 606 | 607 | MdsTable[1][i].B0 = mY[0]; 608 | MdsTable[1][i].B1 = mY[0]; 609 | MdsTable[1][i].B2 = mX[0]; 610 | MdsTable[1][i].B3 = m1[0]; //SetMDS(1); 611 | 612 | MdsTable[2][i].B0 = mX[1]; 613 | MdsTable[2][i].B1 = mY[1]; 614 | MdsTable[2][i].B2 = m1[1]; 615 | MdsTable[2][i].B3 = mY[1]; //SetMDS(2); 616 | 617 | MdsTable[3][i].B0 = mX[0]; 618 | MdsTable[3][i].B1 = m1[0]; 619 | MdsTable[3][i].B2 = mY[0]; 620 | MdsTable[3][i].B3 = mX[0]; //SetMDS(3); 621 | } 622 | 623 | MdsTableBuilt = true; 624 | } 625 | } 626 | 627 | private const uint RS_GF_FDBK = 0x14D; 628 | 629 | private static DWord ReedSolomonMdsEncode(DWord k0, DWord k1) { 630 | var r = new DWord(); 631 | for (var i = 0; i < 2; i++) { 632 | r ^= (i > 0) ? k0 : k1; //merge in 32 more key bits 633 | for (var j = 0; j < 4; j++) { //shift one byte at a time 634 | var b = (byte)(r >> 24); 635 | var g2 = (byte)((b << 1) ^ (((b & 0x80) > 0) ? RS_GF_FDBK : 0)); 636 | var g3 = (byte)(((b >> 1) & 0x7F) ^ (((b & 1) > 0) ? RS_GF_FDBK >> 1 : 0) ^ g2); 637 | r.B3 = (byte)(r.B2 ^ g3); 638 | r.B2 = (byte)(r.B1 ^ g2); 639 | r.B1 = (byte)(r.B0 ^ g3); 640 | r.B0 = b; 641 | } 642 | } 643 | return r; 644 | } 645 | 646 | 647 | private static uint Mul_X(uint x) { 648 | return Mx_X(x); 649 | } 650 | private static uint Mul_Y(uint x) { 651 | return Mx_Y(x); 652 | } 653 | 654 | private static uint Mx_X(uint x) { 655 | return (x ^ LFSR2(x)); //5B 656 | } 657 | 658 | private static uint Mx_Y(uint x) { 659 | return (x ^ LFSR1(x) ^ LFSR2(x)); //EF 660 | } 661 | 662 | 663 | private const uint MDS_GF_FDBK = 0x169; //primitive polynomial for GF(256) 664 | 665 | private static uint LFSR1(uint x) { 666 | return ((x >> 1) ^ (((x & 0x01) > 0) ? MDS_GF_FDBK / 2 : 0)); 667 | } 668 | 669 | static private uint LFSR2(uint x) { 670 | return ((x >> 2) ^ (((x & 0x02) > 0) ? MDS_GF_FDBK / 2 : 0) 671 | ^ (((x & 0x01) > 0) ? MDS_GF_FDBK / 4 : 0)); 672 | } 673 | 674 | private struct DWord 675 | { 676 | public byte B0; 677 | public byte B1; 678 | public byte B2; 679 | public byte B3; 680 | 681 | private uint Value; 682 | 683 | private DWord(uint value) : this() { 684 | Value = value; 685 | } 686 | 687 | internal DWord(byte[] buffer, int offset) : this() { 688 | B0 = buffer[offset + 0]; 689 | B1 = buffer[offset + 1]; 690 | B2 = buffer[offset + 2]; 691 | B3 = buffer[offset + 3]; 692 | } 693 | 694 | public static explicit operator uint(DWord expr) { 695 | return expr.Value; 696 | } 697 | 698 | public static explicit operator DWord(int value) { 699 | return new DWord((uint)value); 700 | } 701 | 702 | public static explicit operator DWord(uint value) { 703 | return new DWord(value); 704 | } 705 | 706 | 707 | public static DWord operator +(DWord expr1, DWord expr2) { 708 | expr1.Value += expr2.Value; 709 | return expr1; 710 | } 711 | 712 | public static DWord operator *(uint value, DWord expr) { 713 | expr.Value = value * expr.Value; 714 | return expr; 715 | } 716 | 717 | 718 | public static DWord operator |(DWord expr1, DWord expr2) { 719 | expr1.Value |= expr2.Value; 720 | return expr1; 721 | } 722 | 723 | public static DWord operator ^(DWord expr1, DWord expr2) { 724 | expr1.Value ^= expr2.Value; 725 | return expr1; 726 | } 727 | 728 | public static DWord operator <<(DWord expr, int count) { 729 | expr.Value <<= count; 730 | return expr; 731 | } 732 | 733 | public static DWord operator >>(DWord expr, int count) { 734 | expr.Value >>= count; 735 | return expr; 736 | } 737 | 738 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 739 | public DWord RotateLeft(int n) { 740 | return (DWord)((Value << n) | (Value >> (32 - n))); 741 | } 742 | 743 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 744 | public DWord RotateRight(int n) { 745 | return (DWord)((Value >> n) | (Value << (32 - n))); 746 | } 747 | } 748 | } 749 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Twofish/Twofish.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 829fc27d22454e41833c7820d169d278 3 | timeCreated: 1709901703 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Twofish/TwofishEncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | using System.Text; 3 | using DevsDaddy.Shared.CryptoLibrary.Core; 4 | 5 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.Twofish 6 | { 7 | [System.Serializable] 8 | public class TwofishEncryptionOptions : IProviderOptions 9 | { 10 | public Encoding encoding = Encoding.UTF8; 11 | public int keySize = 256; 12 | public CipherMode mode = CipherMode.CBC; 13 | public PaddingMode padding = PaddingMode.PKCS7; 14 | 15 | public byte[] IV; 16 | public byte[] cryptoKey; 17 | } 18 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Twofish/TwofishEncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9b2b25c8068142dab8bd78dec03a4d3e 3 | timeCreated: 1709901440 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Twofish/TwofishProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Security.Cryptography; 4 | using DevsDaddy.Shared.CryptoLibrary.Core; 5 | 6 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.Twofish 7 | { 8 | /// 9 | /// Twofish Crypto Provider 10 | /// 11 | public class TwofishProvider : ICryptoProvider 12 | { 13 | // Provider Options and Worker 14 | private TwofishEncryptionOptions _options; 15 | private Twofish _worker; 16 | 17 | /// 18 | /// Twofish Provider 19 | /// 20 | /// 21 | public TwofishProvider(TwofishEncryptionOptions options) { 22 | _options = options ?? new TwofishEncryptionOptions(); 23 | _worker = new Twofish() { 24 | KeySize = _options.keySize, 25 | Mode = _options.mode, 26 | Padding = _options.padding 27 | }; 28 | 29 | if (_options.IV == null) 30 | _worker.GenerateIV(); 31 | else 32 | _worker.IV = _options.IV; 33 | 34 | if (_options.cryptoKey == null) 35 | _worker.GenerateKey(); 36 | else 37 | _worker.Key = _options.cryptoKey; 38 | } 39 | 40 | /// 41 | /// Decrypt text encrypted by Twofish Provider 42 | /// 43 | /// 44 | /// 45 | public string DecryptString(string encryptedString) { 46 | byte[] decrypted = DecryptBinary(Convert.FromBase64String(encryptedString)); 47 | return _options.encoding.GetString(decrypted); 48 | } 49 | 50 | /// 51 | /// Decrypt binary data using Twofish Provider to byte array 52 | /// 53 | /// 54 | /// 55 | public byte[] DecryptBinary(byte[] encryptedBinary) { 56 | var inputData = new MemoryStream(encryptedBinary); 57 | var outputData = new MemoryStream(); 58 | using var decryptor = _worker.CreateDecryptor(); 59 | using (var csRead = new CryptoStream(inputData, decryptor, CryptoStreamMode.Read)) { 60 | csRead.CopyTo(outputData); 61 | } 62 | 63 | var bytes = outputData.ToArray(); 64 | return bytes; 65 | } 66 | 67 | /// 68 | /// Encrypt Plain-Text using Twofish Provider 69 | /// 70 | /// 71 | /// 72 | public string EncryptString(string decryptedString) { 73 | byte[] encrypted = EncryptBinary(_options.encoding.GetBytes(decryptedString)); 74 | return Convert.ToBase64String(encrypted); 75 | } 76 | 77 | /// 78 | /// Encrypt byte array using Twofish Provider 79 | /// 80 | /// 81 | /// 82 | public byte[] EncryptBinary(byte[] decryptedBinary) { 83 | var dataStream = new MemoryStream(decryptedBinary); 84 | using var encryptor = _worker.CreateEncryptor(); 85 | var outDataStream = new MemoryStream(); 86 | using (var csWrite = new CryptoStream(outDataStream, encryptor, CryptoStreamMode.Write)) { 87 | dataStream.CopyTo(csWrite); 88 | } 89 | 90 | var bytes = outDataStream.ToArray(); 91 | return bytes; 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/Twofish/TwofishProvider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ec782e50b2934c9db2613df7db503844 3 | timeCreated: 1709901431 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/XOR.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1c083b5bf28e4653bde6d14e07c7463a 3 | timeCreated: 1709889653 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/XOR/XOREncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.XOR 5 | { 6 | [System.Serializable] 7 | public class XOREncryptionOptions : IProviderOptions 8 | { 9 | public string cryptoKey = "123456"; 10 | public Encoding encoding = Encoding.UTF8; 11 | } 12 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/XOR/XOREncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d3cbc6cea79441a9a6b055fdceeff21d 3 | timeCreated: 1709895502 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/XOR/XORProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using DevsDaddy.Shared.CryptoLibrary.Core; 4 | 5 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.XOR 6 | { 7 | /// 8 | /// XOR Crypto Provider 9 | /// Works only with strings 10 | /// 11 | public class XORProvider : ICryptoProvider 12 | { 13 | // Provider Options 14 | private XOREncryptionOptions _options; 15 | 16 | /// 17 | /// XOR-Provicer 18 | /// 19 | /// 20 | public XORProvider(XOREncryptionOptions options = null) { 21 | _options = options ?? new XOREncryptionOptions(); 22 | } 23 | 24 | /// 25 | /// Decrypt String (Warning! For XOR Encrypt/Decrypt methods are similar, because it is XOR) 26 | /// 27 | /// 28 | /// 29 | public string DecryptString(string encryptedString) { 30 | return EncryptOrDecrypt(encryptedString, _options.cryptoKey); 31 | } 32 | 33 | /// 34 | /// Decrypt Binary (Not Supported for XOR) 35 | /// 36 | /// 37 | /// 38 | /// 39 | public byte[] DecryptBinary(byte[] encryptedBinary) { 40 | throw new Exception($"Failed to decrypt byte array, because {this.GetType()} support only plain text."); 41 | } 42 | 43 | /// 44 | /// Encrypt String (Warning! For XOR Encrypt/Decrypt methods are similar, because it is XOR) 45 | /// 46 | /// 47 | /// 48 | public string EncryptString(string decryptedString) { 49 | return EncryptOrDecrypt(decryptedString, _options.cryptoKey); 50 | } 51 | 52 | /// 53 | /// Encrypt Binary (Not Supported for XOR) 54 | /// 55 | /// 56 | /// 57 | /// 58 | public byte[] EncryptBinary(byte[] decryptedBinary) { 59 | throw new Exception($"Failed to decrypt byte array, because {this.GetType()} support only plain text."); 60 | } 61 | 62 | 63 | /// 64 | /// Encrypt / Decrypt via XOR-mechanism 65 | /// 66 | /// 67 | /// 68 | /// 69 | private string EncryptOrDecrypt(string text, string key) 70 | { 71 | var result = new StringBuilder(); 72 | for (int c = 0; c < text.Length; c++) 73 | { 74 | char character = text[c]; 75 | uint charCode = (uint)character; 76 | int keyPosition = c % key.Length; 77 | char keyChar = key[keyPosition]; 78 | uint keyCode = (uint)keyChar; 79 | uint combinedCode = charCode ^ keyCode; 80 | char combinedChar = (char)combinedCode; 81 | result.Append(combinedChar); 82 | } 83 | return result.ToString(); 84 | } 85 | } 86 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/XOR/XORProvider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0fb964da15ef495dbb5b96bd88f8c84d 3 | timeCreated: 1709895421 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/xxHash.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 618e4675bfe348f8ba8a86b4f9ba16fc 3 | timeCreated: 1709889657 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/xxHash/xxHashEncryptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.xxHash 5 | { 6 | [System.Serializable] 7 | public class xxHashEncryptionOptions : IProviderOptions 8 | { 9 | public uint PRIME32_1 = 2654435761U; 10 | public uint PRIME32_2 = 2246822519U; 11 | public uint PRIME32_3 = 3266489917U; 12 | public uint PRIME32_4 = 668265263U; 13 | public uint PRIME32_5 = 374761393U; 14 | 15 | public int length = 16; 16 | public uint seed = 0; 17 | 18 | public Encoding encoding = Encoding.UTF8; 19 | } 20 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/xxHash/xxHashEncryptionOptions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 70b4efc0212345a2b0c82cd1fbfcc225 3 | timeCreated: 1709895026 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/xxHash/xxHashProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using DevsDaddy.Shared.CryptoLibrary.Core; 3 | 4 | namespace DevsDaddy.Shared.CryptoLibrary.Modules.xxHash 5 | { 6 | /// 7 | /// xxHash Provider 8 | /// Can Hash for plain text or byte array 9 | /// 10 | public class xxHashProvider : ICryptoProvider 11 | { 12 | // Provider Options 13 | private xxHashEncryptionOptions _options; 14 | 15 | /// 16 | /// xxHash Provider 17 | /// 18 | /// 19 | public xxHashProvider(xxHashEncryptionOptions options = null) { 20 | _options = options ?? new xxHashEncryptionOptions(); 21 | } 22 | 23 | /// 24 | /// Decrypt String (Not Supported for hashing) 25 | /// 26 | /// 27 | /// 28 | /// 29 | public string DecryptString(string encryptedString) { 30 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 31 | } 32 | 33 | /// 34 | /// Decrypt Binary (Not Supported for hashing) 35 | /// 36 | /// 37 | /// 38 | /// 39 | public byte[] DecryptBinary(byte[] encryptedBinary) { 40 | throw new Exception($"Failed to decrypt text, because {this.GetType()} doesn't support decryption."); 41 | } 42 | 43 | /// 44 | /// Get xxHash for plain-text 45 | /// 46 | /// 47 | /// 48 | public string EncryptString(string decryptedString) { 49 | return Convert.ToString(EncryptBinary(_options.encoding.GetBytes(decryptedString))); 50 | } 51 | 52 | /// 53 | /// Get xxHash for byte array 54 | /// 55 | /// 56 | /// 57 | public byte[] EncryptBinary(byte[] decryptedBinary) { 58 | uint h32; 59 | int index = 0; 60 | 61 | if (_options.length >= 16) 62 | { 63 | int limit = _options.length - 16; 64 | uint v1 = _options.seed + _options.PRIME32_1 + _options.PRIME32_2; 65 | uint v2 = _options.seed + _options.PRIME32_2; 66 | uint v3 = _options.seed; 67 | uint v4 = _options.seed - _options.PRIME32_1; 68 | 69 | do 70 | { 71 | uint read_value = (uint)(decryptedBinary[index++] | decryptedBinary[index++] << 8 | decryptedBinary[index++] << 16 | decryptedBinary[index++] << 24); 72 | v1 += read_value * _options.PRIME32_2; 73 | v1 = (v1 << 13) | (v1 >> 19); 74 | v1 *= _options.PRIME32_1; 75 | 76 | read_value = (uint)(decryptedBinary[index++] | decryptedBinary[index++] << 8 | decryptedBinary[index++] << 16 | decryptedBinary[index++] << 24); 77 | v2 += read_value * _options.PRIME32_2; 78 | v2 = (v2 << 13) | (v2 >> 19); 79 | v2 *= _options.PRIME32_1; 80 | 81 | read_value = (uint)(decryptedBinary[index++] | decryptedBinary[index++] << 8 | decryptedBinary[index++] << 16 | decryptedBinary[index++] << 24); 82 | v3 += read_value * _options.PRIME32_2; 83 | v3 = (v3 << 13) | (v3 >> 19); 84 | v3 *= _options.PRIME32_1; 85 | 86 | read_value = (uint)(decryptedBinary[index++] | decryptedBinary[index++] << 8 | decryptedBinary[index++] << 16 | decryptedBinary[index++] << 24); 87 | v4 += read_value * _options.PRIME32_2; 88 | v4 = (v4 << 13) | (v4 >> 19); 89 | v4 *= _options.PRIME32_1; 90 | 91 | } while (index <= limit); 92 | 93 | h32 = ((v1 << 1) | (v1 >> 31)) + ((v2 << 7) | (v2 >> 25)) + ((v3 << 12) | (v3 >> 20)) + ((v4 << 18) | (v4 >> 14)); 94 | } 95 | else 96 | { 97 | h32 = _options.seed + _options.PRIME32_5; 98 | } 99 | 100 | h32 += (uint)_options.length; 101 | 102 | while (index <= _options.length - 4) 103 | { 104 | h32 += (uint)(decryptedBinary[index++] | decryptedBinary[index++] << 8 | decryptedBinary[index++] << 16 | decryptedBinary[index++] << 24) * _options.PRIME32_3; 105 | h32 = ((h32 << 17) | (h32 >> 15)) * _options.PRIME32_4; 106 | } 107 | 108 | while (index < _options.length) 109 | { 110 | h32 += decryptedBinary[index] * _options.PRIME32_5; 111 | h32 = ((h32 << 11) | (h32 >> 21)) * _options.PRIME32_1; 112 | index++; 113 | } 114 | 115 | h32 ^= h32 >> 15; 116 | h32 *= _options.PRIME32_2; 117 | h32 ^= h32 >> 13; 118 | h32 *= _options.PRIME32_3; 119 | h32 ^= h32 >> 16; 120 | 121 | return BitConverter.GetBytes(h32); 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Modules/xxHash/xxHashProvider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6a8f06d0ba9342b4a7586df87c38df6b 3 | timeCreated: 1709895032 -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DevsDaddy/UnityCrypto/e39f5852c0e6b7bd8d9bfaa44a9e1931adb2d9b5/DevsDaddy/Shared/CryptoLibrary/Preview.png -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/Preview.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ccbed3d5bb66003438818e7ea5bc4537 3 | TextureImporter: 4 | internalIDToNameTable: [] 5 | externalObjects: {} 6 | serializedVersion: 12 7 | mipmaps: 8 | mipMapMode: 0 9 | enableMipMap: 0 10 | sRGBTexture: 1 11 | linearTexture: 0 12 | fadeOut: 0 13 | borderMipMap: 0 14 | mipMapsPreserveCoverage: 0 15 | alphaTestReferenceValue: 0.5 16 | mipMapFadeDistanceStart: 1 17 | mipMapFadeDistanceEnd: 3 18 | bumpmap: 19 | convertToNormalMap: 0 20 | externalNormalMap: 0 21 | heightScale: 0.25 22 | normalMapFilter: 0 23 | flipGreenChannel: 0 24 | isReadable: 0 25 | streamingMipmaps: 0 26 | streamingMipmapsPriority: 0 27 | vTOnly: 0 28 | ignoreMipmapLimit: 0 29 | grayScaleToAlpha: 0 30 | generateCubemap: 6 31 | cubemapConvolution: 0 32 | seamlessCubemap: 0 33 | textureFormat: 1 34 | maxTextureSize: 2048 35 | textureSettings: 36 | serializedVersion: 2 37 | filterMode: 1 38 | aniso: 1 39 | mipBias: 0 40 | wrapU: 1 41 | wrapV: 1 42 | wrapW: 0 43 | nPOTScale: 0 44 | lightmap: 0 45 | compressionQuality: 50 46 | spriteMode: 1 47 | spriteExtrude: 1 48 | spriteMeshType: 1 49 | alignment: 0 50 | spritePivot: {x: 0.5, y: 0.5} 51 | spritePixelsToUnits: 100 52 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 53 | spriteGenerateFallbackPhysicsShape: 1 54 | alphaUsage: 1 55 | alphaIsTransparency: 1 56 | spriteTessellationDetail: -1 57 | textureType: 8 58 | textureShape: 1 59 | singleChannelComponent: 0 60 | flipbookRows: 1 61 | flipbookColumns: 1 62 | maxTextureSizeSet: 0 63 | compressionQualitySet: 0 64 | textureFormatSet: 0 65 | ignorePngGamma: 0 66 | applyGammaDecoding: 0 67 | swizzle: 50462976 68 | cookieLightType: 0 69 | platformSettings: 70 | - serializedVersion: 3 71 | buildTarget: DefaultTexturePlatform 72 | maxTextureSize: 2048 73 | resizeAlgorithm: 0 74 | textureFormat: -1 75 | textureCompression: 1 76 | compressionQuality: 50 77 | crunchedCompression: 0 78 | allowsAlphaSplitting: 0 79 | overridden: 0 80 | ignorePlatformSupport: 0 81 | androidETC2FallbackOverride: 0 82 | forceMaximumCompressionQuality_BC6H_BC7: 0 83 | - serializedVersion: 3 84 | buildTarget: Standalone 85 | maxTextureSize: 2048 86 | resizeAlgorithm: 0 87 | textureFormat: -1 88 | textureCompression: 1 89 | compressionQuality: 50 90 | crunchedCompression: 0 91 | allowsAlphaSplitting: 0 92 | overridden: 0 93 | ignorePlatformSupport: 0 94 | androidETC2FallbackOverride: 0 95 | forceMaximumCompressionQuality_BC6H_BC7: 0 96 | spriteSheet: 97 | serializedVersion: 2 98 | sprites: [] 99 | outline: [] 100 | physicsShape: [] 101 | bones: [] 102 | spriteID: 5e97eb03825dee720800000000000000 103 | internalID: 0 104 | vertices: [] 105 | indices: 106 | edges: [] 107 | weights: [] 108 | secondaryTextures: [] 109 | nameFileIdTable: {} 110 | mipmapLimitGroupName: 111 | pSDRemoveMatte: 0 112 | userData: 113 | assetBundleName: 114 | assetBundleVariant: 115 | -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "com.devsdaddy.cryptolibrary", 3 | "displayName": "Unity Crypto Library", 4 | "version": "1.0.0", 5 | "unity": "2019.3", 6 | "description": "Unity Crypto Library is a set of free and open source cross-platform tools for using cryptographic and hash functions in your games. It also contains helper classes for handling files and web requests for your convenience.", 7 | "documentationUrl": "https://github.com/DevsDaddy/UnityCrypto/", 8 | "changelogUrl": "https://github.com/DevsDaddy/UnityCrypto/releases", 9 | "licensesUrl": "https://github.com/DevsDaddy/UnityCrypto/blob/main/LICENSE", 10 | "keywords": [ 11 | "unity", 12 | "crypto", 13 | "library", 14 | "framework", 15 | "cryptography", 16 | "aes", 17 | "des", 18 | "sha", 19 | "md5" 20 | ], 21 | "author": { 22 | "name": "DevsDaddy", 23 | "email": "blackfixltd@gmail.com", 24 | "url": "https://devsdaddy.hashnode.dev/" 25 | }, 26 | "category": "Unity" 27 | } 28 | -------------------------------------------------------------------------------- /DevsDaddy/Shared/CryptoLibrary/package.json.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6de878aef5b7bd2468e63dffd913b81b 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Elijah Brown 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity Crypto Library 2 | Unity Crypto Library
3 | **Unity Crypto Library** is a set of free and open source **cross-platform tools** for using **cryptographic and hash functions** in your games. 4 | It also contains helper classes for handling files and web requests for your convenience. 5 | 6 | I periodically update possible algorithms based on the latest research in crypto-graphy. 7 | 8 | > **Note!** This library supports only Unity 2021+ versions 9 | 10 | ## Get Started 11 | **Unity Crypto Library** is designed for your application and games security and using only default API's like **System** and **UnityEngine**. 12 | 13 | **Installation process:** 14 | - Download and import latest release from this page; 15 | - See usage examples below; 16 | 17 | **Or Using Unity Package Manager:**
18 | ```https://github.com/DevsDaddy/UnityCrypto.git?path=/DevsDaddy/Shared/CryptoLibrary/``` 19 | 20 | ## Usage 21 | **You can use a simple controller for fast encryption/decryption:** 22 | ```csharp 23 | // Setup Default Crypto-Provider 24 | CryptoController.SetDefaultProvider(new AESProvider(new AESEncryptionOptions { 25 | cryptoKey = "myCryptoKey" 26 | })); 27 | 28 | // Simple Plain-Text Encryption-Decryption 29 | string encryptedText = CryptoController.Encrypt("MyTextToEncrypt"); 30 | string decryptedText = CryptoController.Decrypt(encryptedText); 31 | 32 | // Or with in-line crypto-provider 33 | string encryptedText2 = CryptoController.Encrypt("TextToEncrypt", new AESProvider(new AESEncryptionOptions { 34 | cryptoKey = "myCryptoKey" 35 | })); 36 | ``` 37 | 38 | **Or initial crypto providers manually:*** 39 | ```csharp 40 | // Create your provider 41 | AESProvider provider = new AESProvider(new AESEncryptionOptions { 42 | cryptoKey = "myCryptoKey" 43 | }); 44 | 45 | // Work with provider 46 | string encryptedText = provider.EncryptString("MyTextToEncrypt"); 47 | string decryptedText = provider.DecryptString(encryptedText); 48 | ``` 49 | 50 | **Also you can read/write files using util class (similar like CryptoController just for files):*** 51 | ```csharp 52 | // Setup Default Crypto-provider for Files 53 | CryptoFile.SetDefaultProvider(new AESProvider(new AESEncryptionOptions { 54 | cryptoKey = "key" 55 | })); 56 | 57 | // Read and Decrypt File 58 | string decryptedText = CryptoFile.ReadText("path_to_encrypted_file"); 59 | 60 | // Encrypt plain-text and save to file 61 | bool writtenFile = CryptoFile.WriteText("path_to_encrypted_file", decryptedText); 62 | ``` 63 | 64 | ## Crypto-Algorithms 65 | **Library contains popular crypto modules:** 66 | - **AES** (Recommended); 67 | - **Triple DES** (Recommended); 68 | - **BlowFish** (Recommended); 69 | - **Twofish** (Recommended); 70 | - **RSA** (Recommended for Web); 71 | - **DES**; 72 | - **Base64**; 73 | - **XOR** (Only strings support, no byte array); 74 | 75 | **Code Sample:** 76 | ```csharp 77 | string encrypted = CryptoController.Encrypt("TextToEncrypt", new AESProvider(new AESEncryptionOptions { 78 | cryptoKey = "myCryptoKey" 79 | })); 80 | 81 | // Return Encrypted 82 | Debug.Log(encrypted); 83 | ``` 84 | 85 | ## Hashing 86 | **Library contains popular hashing functions:** 87 | - **SHA1** / **SHA256** / **SHA512** (Recommended); 88 | - **PBKDF2** (Recommended); 89 | - **MD5**; 90 | - **xxHash**; 91 | - **RIPEMD-160**; 92 | - **CRC32**; 93 | 94 | **Code Sample:** 95 | ```csharp 96 | string hashed = CryptoController.Encrypt("TextToEncrypt", new MD5Provider(new MD5EncryptionOptions { })); 97 | 98 | // Return Hashed 99 | Debug.Log(hashed); 100 | ``` 101 | 102 | ## Examples 103 | See framework usage examples in other projects: 104 | * Game Shield - Unity Game Security and Anti-Cheat Toolkit; 105 | 106 | ## Coming Soon (TO-DO) 107 | **I plan to add the following functionality in the near future:** 108 | - Sending and accepting encrypted HTTP requests and working with encrypted data on sockets with server examples on NodeJS / CSharp; 109 | - Read / Write files using chunks for large amounts of data; 110 | - New Hashing Modules: Argon2, BCrypt, SCrypt, Whirlpool; 111 | - New Encryption Modules like SPHINCS+; 112 | 113 | ## Join Community 114 | - Discord Community 115 | 116 | ## Support Me 117 | **You can support the development and updating of libraries and assemblies by dropping a coin:** 118 | 119 | 120 | 121 | 122 |
Bitcoin (BTC)bc1qef2d34r4xkrm48zknjdjt7c0ea92ay9m2a7q55
Etherium (ETH)0x1112a2Ef850711DF4dE9c432376F255f416ef5d0
Boostyhttps://boosty.to/devsdaddy
123 | --------------------------------------------------------------------------------